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"
Question
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"
Solution
The correct answer is "The car moves".
Here's the step-by-step explanation:
-
The
Vehicleclass has a method calledmove()that prints "The vehicle moves". -
The
Carclass extendsVehicleand overrides themove()method to print "The car moves". -
In the
mainmethod, aVehiclereference is created but it's pointing to aCarobject. This is called upcasting. -
When the
move()method is called on thevehiclereference, the overridden method in theCarclass is called, not the one in theVehicleclass. This is because Java uses dynamic method dispatch, which means it calls the overridden method in the actual object's class, not the reference type's class. -
Therefore, "The car moves" is printed to the console.
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
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
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?class Parent { String name = "parent"; String message() { return "from parent"; }}class Child extends Parent { String name = "child"; String message() { return "from child"; }}public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name + " " + p.message()); }}Options"child from child""parent from parent""parent from child""child from parent"
Select the correct answerWhat will be the output of the given Java program? abstract class java { int x; abstract void display(); } class method extends java { int y; void display() { System.out.println(y); } } class Abstract_demo { public static void main(String args[]) { method obj = new method(); obj.y = 1; obj.display(); } }OptionsRuntime Error10Compilation 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.