Home Tutorial Java Scjp Part4 SCJP Module-4 Question-10

 
 

SCJP Module-4 Question-10
Posted on: July 9, 2010 at 12:00 AM
The program given below tests your knowledge of Inheritance in Java and prepares you for SCJP examination.

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.

Answer :

(A)

Explanation :

Car object cannot be cast to Bike (error at line number 19).

 

Related Tags for SCJP Module-4 Question-10:


Ask Questions?

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.