49.7k views
3 votes
Public class Robot

{

public Robot() { System.out.println("Tom Servo"); }

public Robot(String name) { System.out.println(name); }

}

public class Cyborg extends Robot

{

public Cyborg() { System.out.println("Robocop");

public Cyborg(String name) { System.out.println(name);

}

public class Cyberman extends Cyborg

{

public Cyberman() { super(); System.out.println("Cyberman"); }

public Cyberman(String name) { super(name); System.out.println(name);

}

Given the class hierarchy above, what output would be generated with the following statement: Cyberman one = new Cyberman("Bob");

User Moses Xu
by
4.7k points

1 Answer

3 votes

Answer:

Tom Servo

Bob (prints inside Cyborg constructor)

Bob (prints inside Cyberman constructor)

Step-by-step explanation:

The above type of inheritance is multilevel inheritance where Robot is super class and Cyborg and Cyberman are sub classes.

So in above statement

Cyberman one = new Cyberman("Bob"); it calls the Cyberman(String name) in which super(name) calls Cyborg(String name) but before executing the body inside Cyborg constructor it first calls super class default constructor (i.e., Robot()) so the output of the above statement is

Tom Servo

Bob (prints inside Cyborg constructor)

Bob (prints inside Cyberman constructor)

User Tsunaze
by
5.6k points