public class Robot { public Robot() { System.out.println(\"Tom Servo\"); } publi
ID: 3723013 • Letter: P
Question
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 Cyberman("Bob");
Explanation / Answer
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 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)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.