Example for an Abstract class and its child classes.
In the below example we have an AbstractClass that contains 2 abstract methods sayHello and sayHi and one non-abstract method as welcome.
Child1 is also an abstract class that extends an AbstractClass. Since this class is also an abstract class it’s not mandatory to add implementation for all abstract methods. We can implement one or no methods in this class.
Child2 is a non-abstract class that extends Child1. Since this class is non-abstract it must include the implementation for remaining abstract methods as shown.
Child1 can also have its own abstract methods.
Both Child1 and Child2 can have their own non-abstract methods.
Since AbstractClass and Child1 both are declared as abstract we can not create objects for them. We can only create an object for Child2 and can call the methods of AbstractClass and Child1 using Child2’s object.
Can an abstract class have a constructor?
Yes, an abstract class can have a constructor. Consider the below example for the abstract class constructor. This constructor is called when a child class object is created.