How do you call a constructor of a superclass constructor in Java?
The super keyword in java is a reference variable that is used to refer to parent class objects. An understanding of Inheritance and Polymorphism is needed in order to understand the super keyword. The keyword “super” came into the picture with the concept of Inheritance. It is majorly used in the following contexts: Show
1. Use of super with variablesThis scenario occurs when a derived class and base class has the same data members. In that case, there is a possibility of ambiguity for the JVM. We can understand it more clearly using this code snippet:
Java
In the above example, both base class and subclass have a member maxSpeed. We could access maxSpeed of base class in subclass using super keyword. 2. Use of super with methodsThis is used when we want to call the parent class method. So whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword. This code snippet helps to understand the said usage of the super keyword. Java
Output This is student class This is person class In the above example, we have seen that if we only call method message() then, the current class message() is invoked but with the use of the super keyword, message() of superclass could also be invoked. 3. Use of super with constructorsThe super keyword can also be used to access the parent class constructor. One more important thing is that ‘super’ can call both parametric as well as non-parametric constructors depending upon the situation. Following is the code snippet to explain the above concept: Java
Output Person class Constructor Student class Constructor In the above example, we have called the superclass constructor using the keyword ‘super’ via subclass constructor. Important Points to Remember while using Super Keyword
This article is contributed by Vishwajeet Srivastava. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. Can we call subclass constructor from superclass constructor in Java?A subclass can call a constructor defined by its superclass by use of the following form of super: super(parameter-list); Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.
Can a constructor call another constructor in Java?Constructor Calling form another Constructor
The calling of the constructor can be done in two ways: By using this() keyword: It is used when we want to call the current class constructor within the same class. By using super() keyword: It is used when we want to call the superclass constructor from the base class.
Where can you place a call to the superclass constructor?Use the following syntax: super. myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor.
Is the superclass constructor called first?Invocation of a superclass constructor must be the first line in the subclass constructor.
|