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(); }}
Question
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(); }}
Solution
The given code is written in Java. It's an example of multilevel inheritance where the class FourWheeler is the parent class, Car is the child class which extends FourWheeler, and Audi is the grandchild class which extends Car.
Here's a step-by-step explanation of what happens when you run the Driver class:
-
An object of the
Audiclass is created in the main method of theDriverclass. -
When the
Audiobject is created, its constructor is called. SinceAudiextendsCar, before executing theAudiconstructor, it calls theCarconstructor using thesuper()keyword. -
The
Carconstructor is called. Since `
Similar Questions
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
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
B A class A { String name="A"; public String getName() { return name; } String greeting() { return "class A"; }}class B extends A { String name="B"; String greeting() { return "class B"; } }public class Test { public static void main(String arg[]) { A a=new A(); A b=new B(); System.out.println(a.greeting()+" has name "+a.getName()); System.out.println(b.greeting()+" has name "+b.getName()); }} Place the names "A" and "B" in the following output. class has name class has name
Add the method to Car class to complete correctly.class FourWheeler {protected FourWheeler getObject(){ //logic with return statement}}class Car extends FourWheeler{ blank}class Driver{public static void main(String args[]){ FourWheeler object = new Car(); object.getObject();}} private FourWheeler getObject() {} protected Car getObject() {} Car getObject() {} protected void getObject() {}
10. class FourWheeler {11. blank void display() { }12. }13. class Car extends FourWheeler {14. blank void display() { }15. }Which method at line 14, will correctly complete class Car?protected private
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.