Knowee
Questions
Features
Study Tools

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"

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is "The car moves".

Here's the step-by-step explanation:

  1. The Vehicle class has a method called move() that prints "The vehicle moves".

  2. The Car class extends Vehicle and overrides the move() method to print "The car moves".

  3. In the main method, a Vehicle reference is created but it's pointing to a Car object. This is called upcasting.

  4. When the move() method is called on the vehicle reference, the overridden method in the Car class is called, not the one in the Vehicle class. 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.

  5. Therefore, "The car moves" is printed to the console.

This problem has been solved

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

1/3

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.