What will be the output of the following program ?class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}Select one:a.Class AudiClass CarClass FourWheelerb.Compile Time Errorc.Class FourWheelerClass CarClass Audid.Exception occurs
Question
What will be the output of the following program ?class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}Select one:a.Class AudiClass CarClass FourWheelerb.Compile Time Errorc.Class FourWheelerClass CarClass Audid.Exception occurs
Solution
The output of the program will be:
Class FourWheeler Class Car Class Audi
So, the correct answer is c. Class FourWheeler Class Car Class Audi
This is because when a new instance of a subclass is created, the constructor of the superclass is called first. In this case, when a new Audi object is created, the constructors of the Car and FourWheeler classes are called first, before the Audi constructor.
Similar Questions
class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3
Select the correct answerWhat is the output of the following Java program?public class Vehicle { public void move() { System.out.println("The vehicle moves"); }}public class Car extends Vehicle { public void move() { System.out.println("The car moves"); }}public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); }}Options"The vehicle moves"The code does not compileNone of these"The car moves"
What will be the output of the following code?class A { void method() { System.out.println("Class A"); }}class B extends A { void method() { super.method(); System.out.println("Class B"); }}public class Main { public static void main(String[] args) { B obj = new B(); obj.method(); }}
Select the correct answerWhat will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options102Compilation Error
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.