• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

superclass and subclass

 
Jello Yao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the following code on 4test web site. Which print out is right?
**************
Given the following classes defined in separate files:
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
class Car extends Vehicle {
public void drive() {
System.out.println("Car: drive");
}
}
public class Test {
public static void main (String args []) {
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}
}

Prints out:
Vehicle : drive
Car : drive
Car : drive
or
Prints out:
Vehicle : drive
Car : drive
Vehicle : drive
*******************************
Thanks!
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is:
Vehicle : drive
Car : drive
Car : drive
 
kishor rao
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with don. thats the output because of the default polymorphic feature of java
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mhh, why didn't you just try it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic