As we know that an abstract class can have constructors, data members, and non-abstract methods as well.
This chapter will shows, how we can declare and use the Constructors in the abstract class.

Important Features of Abstract Class and Methods
Abstract Class
- An abstract class would be declared using the ‘abstract’ keyword.
- The creation of an object is not possible in an abstract class.
- It may contain data members, methods, abstract methods, constructors.
- Data members can’t be abstract.
- It must be inherited by the subclass(es) using the ‘extends’ keyword.
- The subclass should implement each and every abstract method declared in Super Class. Otherwise, the subclass would act like an abstract class that needs to be extended further.
Abstract Method
- An abstract method can be placed within an abstract Class only.
- It would be declared using the ‘abstract’ keyword.
- The abstract method should be declared only without any implementation.
- It can be implemented only in the subclass(concrete class) using ‘extends’ or ‘implements’.
Usage of Constructor in Abstract Class
- We can add constructor as we do normally without any return type.
- When we extend any abstract class into the other class. It forces us to declare a similar constructor in the child class as well. (Feature of Inheritance)
- In the constructor of the child class, it automatically calls a constructor from its Parent class(abstract class). This is done by using the ‘super’ keyword.
- Now, when we create object of child class it automatically invokes the constructor of Parent class(abstract class) via its own constructor.
Code Example
- abstract Class Sample // Parent class
- int a; // Data Member
- Sample(int x) // Constructor with body
- abstract void share(); // abstract method without body
- public void message() // Normal method with body
- Class Sample2 // Child class extends abstract class
- Sample2(int x) // Compile error- need to define constructor
- main(String[] args) // Entry point of program
- void share() // Overridden method with body
Sample.java
package OOPS;
public abstract class Sample {
// Data member
int a;
// Constructor to initialize data member
Sample(int x) {
a=x;
System.out.println("Variable a initiated in abstract class...");
}
// abstract method which will be override by child class
abstract void share();
// normal method with body
public void viewMessage()
{
System.out.println("Sample Message from Parent class.");
}
}
Sample2.java
package OOPS;
public class Sample2 extends Sample{
// Mandatory constructor for child class
Sample2(int x)
{
// Invoked constructor from Parent Class
super(15);
System.out.println("Local variable in child class: "+x);
}
public static void main(String[] args)
{
// Object creation for Child Class using the constructor
Sample2 obj=new Sample2(10);
// Accessing data member from abstract class
System.out.println("Variable from abstract class: "+obj.a);
// Accessing normal method from abstract class
obj.viewMessage();
}
// Override abstract method from abstract class
@Override
void share()
{
System.out.println("Share method called...");
}
}
Console Output
Variable a initiated in abstract class...
Local variable in child class: 10
Variable from abstract class: 15
Sample Message from Parent class.