Abstract Class in Java with Example Program
1. Java Abstract Class – Objective
In our last Java tutorial, we studied Java Packages. Today, we are going to learn about abstract class in Java, how we can use Java Abstract Class in our program. Moreover, we will discuss, a difference between abstract class in Java and abstract class in C++. At last, we will cover Java Abstract Class example.
So, let’s start with Abstract Class in Java Programming.
2. Abstract Class in Java Vs C++
The major difference in Java abstract class and abstract class in C++ is that in C++ the class needs to have at least one viral function while in Java we use a separate keyword ‘abstract’ to define a class abstract.
Do you know the Difference Between Abstraction & Encapsulation in Java
Example of Abstract Class in Java-
// An example abstract class in Java abstract class Shape { int color; // An abstract function (like a pure virtual function in C++) abstract void draw(); }
3. Some Important Points to Ponder
These are some points to remember:
i. Unlike C++, in Java we cannot have instances for an object we have references for an object.
Example
abstract class Base { abstract void fun(); } class Derived extends Base { void fun() { System.out.println("Derived fun() called"); } } class Main { public static void main(String args[]) { // Uncommenting the following line will cause compiler error // Base b = new Base(); // We can have references of Base type. Base b = new Derived(); b.fun(); } }
ii. Like C++, Java can also have constructors, it is created when an instance of a Java abstract class is created.
Read more about Java Constructors
Example
// An abstract class with constructor abstract class Base { Base() { System.out.println("Base Constructor Called"); } abstract void fun(); } class Derived extends Base { Derived() { System.out.println("Derived Constructor Called"); } void fun() { System.out.println("Derived fun() called"); } } class Main { public static void main(String args[]) { Derived d = new Derived(); } }
iii. In Java we can have an abstract class without the need of abstract method, this limits us as we cannot instantiate the created class but only inherit them.
Example
// An abstract class without any abstract method abstract class Base { void fun() { System.out.println("Base fun() called"); } } class Derived extends Base { } class Main { public static void main(String args[]) { Derived d = new Derived(); d.fun(); } }
iv. Java Abstract class can have methods that cannot be overridden i.e. final methods.
Read more about Method overriding in Java
Example
// An abstract class with a final method abstract class Base { final void fun() { System.out.println("Derived fun() called"); } } class Derived extends Base {} class Main { public static void main(String args[]) { Base b = new Derived(); b.fun(); } }
So, this was all about Java Abstract Class. Hope you like our explanation.
4. Conclusion
Hence, in this tutorial, we learned what is Java abstract class and the difference between abstract class in Java and C++ . Moreover, we discussed Java Abstract Class examples.
See Also- Abstraction in Java & Packages in Java