Do we always need to call the superclass constructor when we instantiate a subclass object?


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.

class Person{    public String name;    public int age;    public Person(String name, int age){       this.name = name;       this.age = age;    }    public void displayPerson() {       System.out.println("Data of the Person class: ");       System.out.println("Name: "+this.name);       System.out.println("Age: "+this.age);    } } public class Student extends Person {    public String branch;    public int Student_id;    public Student(String name, int age, String branch, int Student_id){       super(name, age);       this.branch = branch;       this.Student_id = Student_id;    }    public void displayStudent() {       System.out.println("Data of the Student class: ");       System.out.println("Name: "+this.name);       System.out.println("Age: "+this.age);       System.out.println("Branch: "+this.branch);       System.out.println("Student ID: "+this.Student_id);    }    public static void main(String[] args) throws CloneNotSupportedException {       Person person = new Student("Krishna", 20, "IT", 1256);       person.displayPerson();    } }

Output

Data of the Person class: Name: Krishna Age: 20

Do we always need to call the superclass constructor when we instantiate a subclass object?

Updated on 01-Aug-2019 12:09:16

  • Related Questions & Answers
  • How to call one constructor from another in Java?
  • How to call Private Constructor in Java
  • Can we call a constructor directly from a method in java?
  • Java Program to Call One Constructor from another
  • How to call the constructor of a parent class in JavaScript?
  • How to call a static constructor or when static constructor is called in C#?
  • Order of Constructor/ Destructor Call in C++
  • How to declare a constructor in Java?
  • What are the rules for calling the superclass constructor C++?
  • Can we call methods of the superclass from a static method in java?
  • Can we call a method on "this" keyword from a constructor in java?
  • How to create a parameterized constructor in Java?
  • How to create a default constructor in Java?
  • How to explicitly call base class constructor from child class in C#?
  • What is the purpose of a constructor in java?

Main Content

Superclass Relation to Subclass

Subclasses can override superclass methods to support the greater specialization defined by the subclass. Because of the relationship that a subclass object is a superclass object, it is often useful to call the superclass version of the method before executing the specialized subclass code.

How to Call Superclass Methods

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.

This is the syntax for calling a superMethod defined by MySuperClass:

superMethod@MySuperClass(obj,superMethodArguments)

For example, a subclass can call a superclass disp method to implement the display of the superclass part of the object. Then the subclass adds code to display the subclass part of the object:

classdef MySub < MySuperClass methods function disp(obj) disp@MySuperClass(obj) ... end end end

How to Call Superclass Constructor

If you create a subclass object, MATLAB® calls the superclass constructor to initialize the superclass part of the subclass object. By default, MATLAB calls the superclass constructor without arguments. If you want the superclass constructor called with specific arguments, explicitly call the superclass constructor from the subclass constructor. The call to the superclass constructor must come before any other references to the object.

The syntax for calling the superclass constructor uses an @ symbol:

obj = obj@MySuperClass(SuperClassArguments)

In this class, the MySub object is initialized by the MySuperClass constructor. The superclass constructor constructs the MySuperClass part of the object using the specified arguments.

classdef MySub < MySuperClass methods function obj = MySub(arg1,arg2,...) obj = obj@MySuperClass(SuperClassArguments); ... end end end

See Subclass Constructors for more information.

  • Modify Inherited Methods

Does a subclass have to call the superclass constructor?

As mentioned above, you only have to invoke a super constructor if there isn't a default constructor in the parent class.

When you create an object of a subclass the constructor of the superclass calls the constructor of the subclass True or false?

(*2) A subclass constructor always calls the constructor of its superclass, and so on up to the Object constructor, to initialize all the aspects of the instance that the subclass inherits from its superclass.

Is it necessary to call super constructor in Java?

It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor. The parameterized super() must always be the first statement in the body of the constructor of the subclass, otherwise, we get a compilation error.

Is super class constructor always called?

Super class constructor is always called during construction process and it's guaranteed that super class construction is finished before subclass constructor is called.