QnA || How to use Constructors in Abstract class?

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

  1. An abstract class would be declared using the ‘abstract’ keyword.
  2. The creation of an object is not possible in an abstract class.
  3. It may contain data members, methods, abstract methods, constructors.
  4. Data members can’t be abstract.
  5. It must be inherited by the subclass(es) using the ‘extends’ keyword.
  6. 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

  1. An abstract method can be placed within an abstract Class only.
  2. It would be declared using the ‘abstract’ keyword.
  3. The abstract method should be declared only without any implementation.
  4. It can be implemented only in the subclass(concrete class) using ‘extends’ or ‘implements’.

Usage of Constructor in Abstract Class

  1. We can add constructor as we do normally without any return type.
  2. 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)
  3. 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.
  4. 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


package OOPS;
public abstract class Sample {
	// Data member
	int a;
	// Constructor to initialize data member
	Sample(int 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.");


package OOPS;
public class Sample2 extends Sample{
	// Mandatory constructor for child class 
	Sample2(int x) 
		// Invoked constructor from Parent Class
		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
	// Override abstract method from abstract class
	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.

Leave a Comment

Your email address will not be published. Required fields are marked *

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.