How can a subclass call a method or a constructor defined in a superclass?

class SuperClass{ SuperClass(){ System.out.println("SuperClass constructor"); SubClass(); } } public class SubClass extends SuperClass { SubClass (){ System.out.println("Subclass constructor"); } public static void main(String[] args) { System.out.println("Constructor test"); } }

Output

SubClass.java:11: error: cannot find symbol
        SubClass();
        ^
  symbol:   method Main()
  location: class SuperClass
1 error

SubClass.java:11: error: cannot find symbol SubClass(); ^ symbol: method Main() location: class SuperClass 1 error

Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and must use the public accessor and mutator methods. And subclasses do not inherit constructors from the superclass.

So, how do you initialize the superclass’ private variables if you don’t have direct access to them in the subclass? In Java, the superclass constructor can be called from the first line of a subclass constructor by using the special keyword super() and passing appropriate parameters, for example super(); or super(theName); as in the code below. The actual parameters given to super() are used to initialize the inherited instance variables, for example the name instance variable in the Person superclass.

public class Employee extends Person
{
    public Employee()
    {
        super(); // calls the Person() constructor
    }
    public Employee(String theName)
    {
        super(theName); // calls Person(theName) constructor
    }
}

Coding Exercise

The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class to set the name.

Try creating another Employee object in the main method that passes in your name and then use the get methods to print it out. Which class constructor sets the name? Which class constructor sets the id?

If a class has no constructor in Java, the compiler will add a no-argument constructor. A no-argument constructor is one that doesn’t have any parameters, for example public Person().

If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a

class MPoint
{
   private int myX; // coordinates
   private int myY;

   public MPoint( )
   {
      myX = 0;
      myY = 0;
   }

   public MPoint(int a, int b)
   {
      myX = a;
      myY = b;
   }

   // ... other methods not shown

}

public class NamedPoint extends MPoint
{
   private String myName;
   // constructors go here
   // ... other methods not shown
}

//  Proposed constructors for this class:
I.   public NamedPoint()
     {
        myName = "";
     }
II.  public NamedPoint(int d1, int d2, String name)
     {
        myX = d1;
        myY = d2;
        myName = name;
     }
III. public NamedPoint(int d1, int d2, String name)
     {
        super(d1, d2);
        myName = name;
     }
0 call as the first line in a constructor. So, be sure to provide no-argument constructors in parent classes or be sure to use an explicit call to
class MPoint
{
   private int myX; // coordinates
   private int myY;

   public MPoint( )
   {
      myX = 0;
      myY = 0;
   }

   public MPoint(int a, int b)
   {
      myX = a;
      myY = b;
   }

   // ... other methods not shown

}

public class NamedPoint extends MPoint
{
   private String myName;
   // constructors go here
   // ... other methods not shown
}

//  Proposed constructors for this class:
I.   public NamedPoint()
     {
        myName = "";
     }
II.  public NamedPoint(int d1, int d2, String name)
     {
        myX = d1;
        myY = d2;
        myName = name;
     }
III. public NamedPoint(int d1, int d2, String name)
     {
        super(d1, d2);
        myName = name;
     }
0 as the first line in the constructors of subclasses.

Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.

The constructors of the subclass can initialize only the instance variables of the subclass. Thus, when a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass.

Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.

Example

In the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).

Now, you create an object of the subclass and call the method demo().

class SuperClass{
   public void demo() {
      System.out.println("demo method");
   }
}
public class SubClass extends SuperClass {
   public static void main(String args[]) {
      SubClass obj = new SubClass();
      obj.demo();
   }
}

Output

demo method

Super class’s Constructor in inheritance

In inheritance constructors are not inherited. You need to call them explicitly using the super keyword.

If a Super class have parameterized constructor. You need to accept these parameters in the sub class’s constructor and within it, you need to invoke the super class’s constructor using “super()” as −

public Student(String name, int age, String branch, int Student_id){
   super(name, age);
   this.branch = branch;
   this.Student_id = Student_id;
}

Example

Following java program demonstrates how to call a super class’s constructor from the constructor of the sub class using the super keyword.

How does a subclass call a superclass method?

Subclass methods can call superclass methods if both methods have the same name. From the subclass, reference the method name and superclass name with the @ symbol.

How is it possible to call the super class constructors or methods from the sub class in Python?

Accessing data member and member functions: They can be accessed by dot(“.”) operator with the object of their respective class. For example, if the object is car and we want to access the function called drive, then we will have to write car. drive() .

Does subclass automatically call superclass constructor?

subclass implicitly call even default constructor present in super class which is non parameterised. We have to call explicitly when we pass parameters to the constructor.

Can you call a superclass with a subclass?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).