aspose file tools*
The moose likes Beginning Java and the fly likes superclass and subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "superclass and subclass" Watch "superclass and subclass" New topic
Author

superclass and subclass

Jello Yao
Greenhorn

Joined: Jan 05, 2003
Posts: 13
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

Joined: Aug 05, 2001
Posts: 2545
I think it is:
Vehicle : drive
Car : drive
Car : drive
kishor rao
Ranch Hand

Joined: Dec 10, 2002
Posts: 35
i agree with don. thats the output because of the default polymorphic feature of java
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Mhh, why didn't you just try it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: superclass and subclass