TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Abstract Class

Use the abstract keyword, creating classes that derive from an abstract class.
Abstract. This keyword is used on classes. It does not indicate a style of art. It indicates a "template-only" class, one that cannot be created directly.
Class details. Most classes can be instantiated with "new." An abstract class cannot be. Instead we must create an instance of a derived class.

Abstract: We decorate the Page class with the abstract keyword. We also use an abstract method, one with no body.

Class

Extends: Article and Post are derived from the Page class. They are not abstract. Neither are their open methods.

Here: We create instances of Article and Post, but use them with a Page reference. We call open(). The derived open methods are used.

Java program that uses abstract class abstract class Page { public abstract void open(); } class Article extends Page { public void open() { System.out.println("Article.open"); } } class Post extends Page { public void open() { System.out.println("Post.open"); } } public class Program { public static void main(String[] args) { // We cannot directly create a Page. Page page = new Article(); page.open(); Page page2 = new Post(); page2.open(); } } Output Article.open Post.open
Cannot instantiate. We cannot create a new abstract class with new. This will result in a "cannot instantiate the type" error. The program cannot be run.
Java program that causes abstract error abstract class Ghost { } public class Program { public static void main(String[] args) { Ghost ghost = new Ghost(); } } Output Cannot instantiate the type Ghost
Some notes. The term "abstraction" implies something indirect and more general than a concrete object. The word "animal" is an abstraction for a dog, cat or fish.
In Java, we use an abstract class to create a type that can only be used when added to other classes. This is a powerful feature of object-oriented programming.
A summary. A website can have many types of Pages. The Page class is an abstract class for the more derived documents. With abstract classes we unlock a powerful feature of Java.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf