In this lesson, you will learn
Let’s understand using an example of when constructors are executed.
package ch9.l5;
class A {
A() {
System.out.println("I am in Constructor A");
}
}
class B extends A {
B (){
System.out.println("I am in Constructor B");
}
}
class C extends B {
C (){
System.out.println("I am in Constructor C");
}
}
public class CallingConstructor {
public static void main(String[] args) {
C objC=new C();
}
}
I am in Constructor A
I am in Constructor B
I am in Constructor C
Constructors are called in order of their derivation from superclass to subclass only in the case of the default constructor.
Questions: How to call the parameterized constructor in inheritance using subclass objects?
Answer: Using Super Keyword.
Super has the following 2 uses in inheritance.

A subclass can call the constructor defined by its superclass by use of the following form of super
super(parameter list);
Here, arg-list specifies any arguments needed by the constructor in the superclass.
Note: super( ) must always be the first statement executed inside a subclass constructor.
Let’s understand the use of super() to call the superclass constructor through the below figure.

package ch9.l5;
class First{
int a;
First(int a){
this.a=a;
}
void displayA() {
System.out.println("A: "+a);
}
}
class Second extends First{
int b;
Second(int a, int b){
super(a);//Calls the parameterized constructor of class 'First'
this.b=b;
}
void displayB() {
System.out.println("B: "+b);
}
//Calculate sum
void output() {
System.out.println("A+B = "+(a+b));
}
}
public class SuperTest {
public static void main(String[] args) {
Second objSecond=new Second(10, 20);
objSecond.displayA();
objSecond.displayB();
objSecond.output();
}
}
A: 10
B: 20
A+B = 30
The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used.
super.member;
Here the member may be a variable or method.
This second form of super is most applicable to situations where the subclass members have the same names as its subclass. Then subclass members hide the superclass members.
package ch9.l6;
//Using super to overcome name hiding.
class A {
int i;
}
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper2 {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
i in superclass: 1
i in subclass: 2
Although the instance variable i in class B hides the i in class A, super allows access to i defined in the superclass.
As you will see, super can also be used to call methods that are hidden by a subclass. That is known as Method Overriding in Java.
package ch9.l5;
class Person {
String name;
Person(String name) {
this.name = name;
System.out.println("Person name: " + this.name);
}
}
class Employee extends Person {
float salary;
Employee(String name, float salary) {
super(name); // Calls the parameterized constructor of the Person class
this.salary = salary;
System.out.println("Employee salary: " + this.salary);
}
}
public class TestSuper1 {
public static void main(String[] args) {
// This will initialize both the Person and
// the Employee objects with given values
new Employee("John Doe", 50000f);
}
}
Person name: John Doe
Employee salary: 50000.0
package ch9.l5;
class One{
int a;
One(int a){
this.a=a;
}
void displayA() {
System.out.println("A: "+a);
}
}
class Two extends One{
int b;
Two(int a, int b){
super(a);//Calls the parameterized constructor of class 'one'
this.b=b;
}
void displayB() {
System.out.println("B: "+b);
}
}
class Three extends Two{
Three(int a, int b){
super(a,b); //Calls the parameterized constructor of class 'Two'
}
//Calculate sum
void findGreatest() {
System.out.println("Greatest is: "+(a>b?a:b));
}
}
public class SuperTestMultilevel {
public static void main(String[] args) {
Three obj=new Three(15,6);
obj.displayA();
obj.displayB();
obj.findGreatest();
}
}
A: 15
B: 6
Greatest is: 15
Objective: Create a basic inheritance structure where a subclass extends a superclass. The superclass will have a constructor that initializes a name attribute, and the subclass must call this constructor.
Superclass: Person
name (String)Person(String name)Subclass: Employee
employeeId (int)Employee(String name, int employeeId)Instructions:
Person class with a constructor that initializes the name attribute.Employee class that extends Person.Employee constructor, use the super keyword to call the Person constructor to initialize the name attribute, then initialize the employeeId.
Objective: Implement a multi-level inheritance structure where each subclass calls its immediate superclass constructor, demonstrating the chain of constructor calls.
Base Class: Vehicle
brand (String)Vehicle(String brand)Intermediate Class: Car
model (String)Car(String brand, String model)Subclass: ElectricCar
batteryLife (int)ElectricCar(String brand, String model, int batteryLife)Instructions:
Vehicle class with a constructor to initialize the brand.Car class that extends Vehicle, including a constructor that calls the Vehicle constructor and initializes model.ElectricCar class that extends Car, including a constructor that calls the Car constructor and initializes batteryLife.
super with Overloaded ConstructorsObjective: Create a class hierarchy where the superclass has overloaded constructors, and the subclass calls a specific superclass constructor using super.
Superclass: Book
title (String), author (String)Book(String title)Book(String title, String author)Subclass: EBook
fileSize (double)EBook(String title, String author, double fileSize)Instructions:
Book class with two overloaded constructors: one for initializing title only, and another for initializing both title and author.EBook class that extends Book, including a constructor that calls the Book constructor with both title and author parameters using super, and then initializes fileSize.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.