Can constructor be inherited through a subclass that calls the superclass constructor?

Constructor is a block of statements that permits you to create an object of specified class and has similar name as class with no explicit  or specific return type.

No, constructor cannot be inherited in java.

In case of inheritance, child/sub class inherits the state (data members) and behavior (methods) of parent/super class. But it does not inherits the constructor because of the following reason:

If parent class constructor is inherited in child class, then it can not be treated as constructor because constructor name must be same as of class name. It will be treated as a method but now the problem is, method should have explicit return type which inherited parent class constructor can not have.
Example

class SuperClass { 
    public SuperClass() 
    { 
        System.out.println("SuperClass constructor.");
    } 
 
    public void show() 
    { 
         System.out.println("SuperClass method.");
    } 
} 
 
public class SubClass extends SuperClass { 
 
    public static void main(String[] args) 
    { 
        SubClass object1 = new SubClass(); // allowed 
        object1.show(); // allowed
        object1.SuperClass(); // not allowed
        SubClass object2 = new SuperClass(); // not allowed 
    } 
}

Output

SubClass.java:27: error: cannot find symbol
        object1.SuperClass(); // not allowed
               ^
  symbol:   method SuperClass()
  location: variable object1 of type Main
SubClass.java:28: error: incompatible types: SuperClass cannot be converted to Main
        SubClass object2 = new SuperClass(); // not allowed 
                       ^
2 errors

Java interview questions on constructor

  • Default constructor
  • Does constructor return any value in java?
  • Is constructor inherited in java?
  • Can you make a constructor final in java?
  • Difference between constructor and method in java?
  • How to copy values from one object to another java?
  • How to overload constructor in java?
  • can you create an object without using new operator in java?
  • Constructor chaining in java
  • Parameterized constructor in java
  • Can we call subclass constructor from superclass constructor?
  • What happens if you keep return type for a constructor?
  • What is the use of private constructor in java?
  • Can a constructor call another constructor java?

Can constructor be inherited through a subclass that calls the superclass constructor?

We prefer inheritance to reuse the code available in existing classes. In Java, Inheritance is the concept in which one class inherits the properties of another class. In the below example there are two classes Programming and DP while Programming is Parent class and DP is child class. From the main class, we have created an object of DP i.e. child class as it also allows us to access the methods from its parent class, but if we create an object of Parent class(Programming) then we cannot access the methods or objects from its child class.

After creating an object of child class we have first called a method from child class and then called a method from the parent class. This doesn’t matter as we can call the objects in any order.

Java

class Programming {

    public void m1()

    {

      System.out.println("Programming");

    }

}

class DP extends Programming {

    public void m2()

    {

      System.out.println("DP");

    }

}

public class GFG {

    public static void main(String[] args)

    {

        DP obj = new DP();

        obj.m2();

        obj.m1();

    }

}

Output :

DP
Programming

Constructor Inheritance

In the below example, we have created a parent and child class. Here, we have created an object of the child class, as the constructor will call itself on creating an object we need not mention anything.

After creating an object of child class the constructor is expected to print the output from its own class, but from the output, we can identify that Parent class got executed and then child class got executed, this is because we have created a constructor for inherited class and every class contains a super() by default, as we are calling an inherited class it contains super() in its first line and calls the Parent class.

Java

class Programming {

    public Programming()

    {

        System.out.println("Programming");

    }

}

class DP extends Programming {

    public DP() { System.out.println("DP"); }

}

public class GFG {

    public static void main(String[] args)

    {

        DP obj = new DP();

    }

}

Constructor Overloading With Super

In the below example we have used the constructor overloading concept, and we have created an object of child class and after calling the constructor of child class the first line in it is super(10, 20) which says that call the matching constructor from the parent class, if we do not mention that line, by default it calls the super() with no parameterized constructor from Parent class.

Java

class Programming {

    public Programming()

    {

        System.out.println("Programming");

    }

    public Programming(int i, int j)

    {

        System.out.println("Programming + +");

    }

}

class DP extends Programming {

    public DP()

    {

        super(10, 20);

        System.out.println("DP");

    }

    public DP(int i, int j)

    {

        System.out.println("DP + +");

    }

}

public class GFG {

    public static void main(String[] args)

    {

        DP obj = new DP();

    }

}

Output:

Programming + +
DP

Analogy

Let’s look at the below snippet,

  • In the below code we have created an object of the child class, and we are passing the value of 10 from the object line itself and after going to the specific constructor it first calls super() by default and prints “Programming” from the Parent class.
  • The point to note is here we are calling a parameterized constructor from the object creation line but it will call super() by default as will be available by default.
  • In child class, we can also give super() with parameters to call a specific constructor from Parent class.

Java

class Programming {

    public Programming()

    {

        System.out.println("Programming");

    }

    public Programming(int i, int j)

    {

        System.out.println("Programming + +");

    }

}

class DP extends Programming {

    public DP() { System.out.println("DP"); }

    public DP(int i) { System.out.println("DP +"); }

    public DP(int i, int j)

    {

        System.out.println("DP + +");

    }

}

public class GFG {

    public static void main(String[] args)

    {

        DP obj = new DP(10);

    }

}

Output :

Programming
DP +

Can we call a superclass constructor from the class constructor?

To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.

Can constructors be inherited through?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

Can superclass inherit subclass method?

No, it is not possible to call sub class method inside super class. Though it is possible to call different implementations of the same method in a client code while you have a variable with a super class type and instantiate it with either super class or sub class objects. It is called polymorphism.