Question 6 Consider the following code snippet: Vehicle aVehicle = new Auto(); a
ID: 3685542 • Letter: Q
Question
Question 6
Consider the following code snippet:
Vehicle aVehicle = new Auto();
aVehicle.moveForward(200);
If the Auto class inherits from the Vehicle class, and both classes have an implementation of the moveForward method with the same set of parameters and the same return type, which statement is correct?
The moveForward method of the Auto class will be executed.
The moveForward method of the Vehicle class will be executed.
You must specify in the code which class's moveForward method is to be used.
It is not possible to determine which class's method is called.
The moveForward method of the Auto class will be executed.
The moveForward method of the Vehicle class will be executed.
You must specify in the code which class's moveForward method is to be used.
It is not possible to determine which class's method is called.
Explanation / Answer
Ans:
The moveForward method of the Auto class will be executed.
It is case of method overriding in java.Method overriding is used to provide specific implementation of a method in child class that is already provided by its super/parent class.
For exapmle:
1) if we run this program:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
Output will be:Vehicle is running
2) If we run this program:
class Vehicle{
void run(){System.out.println("class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running safely");}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
} ");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
Output will be :Bike is running safely
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.