super keyword for calling constructor function of the parent class in a multilevel hierarchy of level 3

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 28SA
icon
Related questions
Question

Explain the use of super keyword for calling constructor function of the parent class in a multilevel hierarchy of level 3

Expert Solution
Step 1

super keyword:

The "super" keyword in Java allows accessing the constructor of the immediate parent class (or superclass). 

In multilevel inheritance, the super keyword is used in subsequent subclasses to call their respective superclass constructor. We need to create an object of the lowest subclass in order to invoke its constructor only. The constructor of the remaining superclasses will be invoked using super keyword.

Consider the following multilevel hierarchy of level 3:

class A {

A() { } // class A constructor

}

class B extends A {

B() {

super(); //it will call constructor of class A that is parent class of B

}

}

class C extends B {

C() {

super(); //it will call constructor of class B that is parent class of C

}

}

//create object of class C to invoke a constructor of class C

C obj = new C();

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning