SCJP Module-4 Question-10

The program given below tests your knowledge of Inheritance in Java and prepares you for SCJP examination.

 
 

Share on Google+Share on Google+

SCJP Module-4 Question-10

Ads

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).

 

Advertisements

Ads

Posted on: July 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles