In C++, if a class has at least one pure virtual function, then the class becomes abstract. Unlike C++, in Java, a separate keyword abstract is used to make a class abstract.
Following are some important observations about abstract classes in Java.
1) Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.
Derived fun() called
2) Like C++, an abstract class can contain constructors in Java. And a constructor of abstract class is called when an instance of a inherited class is created. For example, the following is a valid Java program.
Base Constructor Called Derived Constructor Called
3) In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited.
Base fun() called
4) Abstract classes can also have final methods (methods that cannot be overridden). For example, the following program compiles and runs fine.
Derived fun() called
5) For any abstract java class we are not allowed to create an object i.e., for abstract class instantiation is not possible.
Compile time error. Test is abstract; cannot be instantiated Test t=new Test();
1. Is it possible to create abstract and final class in Java?
2. Is it possible to have an abstract method in a final class?
3. Is it possible to inherit from multiple abstract classes in Java?
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Messages, aggregation and abstract classes in OOPS
- Pure Virtual Functions and Abstract Classes in C++
- abstract keyword in java
- Difference between Final and Abstract in Java
- Why can't static methods be abstract in Java?
- Abstract Methods in Java with Examples
- Difference between Abstract Class and Interface in Java
- Java | Abstract Class and Interface | Question 3
- Java | Abstract Class and Interface | Question 1
- Java | Abstract Class and Interface | Question 2
- Nested Classes in Java
- Wrapper Classes in Java
- Classes and Objects in Java
- Difference between Abstract Class and Concrete Class in Java
- Understanding Classes and Objects in Java
- Access specifiers for classes or interfaces in Java
- Primitive Wrapper Classes are Immutable in Java
- Private Constructors and Singleton Classes in Java
- Parent and Child classes having same data member in Java
- C++ Classes and Objects
Improved By : gauravmoney26