This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

superclass and subclass

Jello Yao

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 = c;;

Prints out:
Vehicle : drive
Car : drive
Car : drive
Prints out:
Vehicle : drive
Car : drive
Vehicle : drive
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

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: superclass and subclass
It's not a secret anymore!