Given below the sample code :
1. class Vehicle {
2.
public void printSound() {
3.
System.out.print("vehicle");
4.
}
5.
}
6.class Car extends Vehicle {
7.
public void printSound() {
8.
System.out.print("car");
9.
}
10.}
11.
class Bike extends Vehicle {
12.public void printSound() {
13.System.out.print("bike");
14.}
15.}
16.public class Test1 {
17.public static void main(String[] args) {
18.Vehicle v = new Car();
19.Bike b = (Bike) v;
20.v.printSound();
21.b.printSound();
22.}
23.}
What will be the output ?
A) Compilation fails.
B) "car" is printed.
C) "vehiclecar" is printed.
D) "vehiclebike" is printed.
(A)
Car object cannot be cast to Bike (error at line number 19).
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.