Java || Abstract class with Constructor.

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

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.

1582total visits,5visits today

Leave a Reply

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