The Wayback Machine - https://web.archive.org/web/20240726235220/https://www.geeksforgeeks.org/constructor-in-java-abstract-class/
Open In App

Constructor in Java Abstract Class

Last Updated : 02 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will give a default constructor to the abstract class. If we want to know how to define user define constructors like constructors with argument or any kind of constructor inside the abstract class then you should follow the given procedure.

Note: An abstract class is a class declared with an abstract keyword.

Properties of an abstract class:

  • An abstract class can have an abstract and a non-abstract method.
  • It must be declared with an abstract keyword.
  • It can have a constructor, static method.
  • It can have a final method that prevents child class of abstract class not to change the body of the method
  • The abstract method contains no-body or in simple words, you can say that you can’t define an abstract method inside an abstract class. We can define an abstract method inside the derived class of its abstract class.
  • The object of the abstract class can’t be instantiated it means you can’t create an abstract class object directly but you can create its object by reference to its child class.

Procedure:

  • If you define your own constructor without arguments inside an abstract class but forget to call your own constructor inside its derived class constructor then JVM will call the constructor by default.
  • So if you define your single or multi-argument constructor inside the abstract class then make sure to call the constructor inside the derived class constructor with the super keyword.

Implementation: Here in this program, we are going to multiply two numbers by using the following above approach as mentioned.

Step 1: We create an abstract class named ‘Content’ and define a user define a constructor with one argument, variable with name ‘a’, and an abstract method named as ‘multiply’

Step 2: We create a class that must be derived from this abstract class ‘Content’ named ‘GFG’. Inside GFG class we are going to define a constructor and inside the method call the parent class constructor by using the super keyword and define the abstract method of its parent class in it.

Step 3: Now in the main class of our function that is ‘GeeksforGeeks’ here, where we will create an object of abstract class ‘Content’ by reference to its derived class object. Then we call the method of the abstract class by its object.

Step 4: Inside the method, we multiply both the value stored in the different variable names where one of the variables is the variable of an abstract class. We can access the variable of the abstract class by its derived class object.

Example:

Java




// Java Program to Illustrate Concept of Constructors
// in Abstract Class
  
// Class 1
// Helper Abstract class
// Parent class
abstract class Content {
  
    int a;
  
    // Constructor of abstract class
    public Content(int a)
    {
  
        // This keyword refers to current instance itself
        this.a = a;
    }
  
    // Abstract method of abstract class
    abstract int multiply(int val);
}
  
// Class 2
// Helper class extending above Class1
// Child class of Abstract class
class GFG extends Content {
  
    // Constructor of Child class GFG
    GFG()
    {
  
        // Super keyword refers to parent class
        super(2);
    }
  
    // Defining method the abstract method
    public int multiply(int val)
    {
  
        // Returning value of same instance
        return this.a * val;
    }
}
  
// Class 3
// Main class
public class GeeksforGeeks {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Creating reference object of abstract class
        // using it child class
        Content c = new GFG();
  
        // Calling abstract method of abstract class
        System.out.println(c.multiply(3));
    }
}


Output

6


Similar Reads

Difference Between Abstract Class and Abstract Method in Java
Abstract is the modifier applicable only for methods and classes but not for variables. Even though we don't have implementation still we can declare a method with an abstract modifier. That is abstract methods have only declaration but not implementation. Hence, abstract method declaration should compulsory ends with semicolons. Illustration: publ
3 min read
Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
Prerequisite: Interface and Abstract class in Java. A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created. Why interfaces can not have the constructor? An Interface is a complete abstraction of class. All data members present in the interface are by de
4 min read
Why a Constructor can not be final, static or abstract in Java?
Prerequisite: Inheritance in Java Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a method but it has the same name as its class and a
6 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Java | Abstract Class and Interface | Question 1
Which of the following is FALSE about abstract classes in Java (A) If we derive an abstract class and do not implement all the abstract methods, then the derived class should also be marked as abstract using 'abstract' keyword (B) Abstract classes can have constructors (C) A class can be made abstract without any abstract method (D) A class can inh
1 min read
Java | Abstract Class and Interface | Question 2
Which of the following is true about interfaces in java. 1) An interface can contain following type of members. ....public, static, final fields (i.e., constants) ....default and static methods with bodies 2) An instance of interface can be created. 3) A class can implement multiple interfaces. 4) Many classes can implement the same interface. (A)
1 min read
Java | Abstract Class and Interface | Question 3
Predict the output of the following program. abstract class demo { public int a; demo() { a = 10; } abstract public void set(); abstract final public void get(); } class Test extends demo { public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj =
1 min read
Can We Instantiate an Abstract Class in Java?
Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used. Example 1 Java Code // Java program to demonstrate abstract class // cannot have instance public abstract cla
3 min read
Implement Interface using Abstract Class in Java
Interface contains only abstract methods that can't be instantiated and it is declared by keyword interface. A class that is declared with the abstract keyword is known as an abstract class in Java. This is a class that usually contains at least one abstract method which can't be instantiated and It is also possible for the class to have no methods
3 min read
Abstract Class in Java
In Java, abstract class is declared with the abstract keyword. It may have both abstract and non-abstract methods(methods with bodies). An abstract is a Java modifier applicable for classes and methods in Java but not for Variables. In this article, we will learn the use of abstract classes in Java. What is Abstract Class in Java?Java abstract clas
10 min read
three90RightbarBannerImg