wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes extending a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending a class" Watch "extending a class" New topic

extending a class

Thesigan Pillay

Joined: Jul 04, 2001
Posts: 5
class Car
public int gearRatio = 8;
public String accelarate () { return "Accelarate : Car";}

class SportsCar extends Car

public int gearRatio = 9;
public String accelarate () { return "Accelarate : SportsCar";}

public static void main (String[] args)
Car c = new SportsCar ();
System.out.println (c.gearRatio+" "+ c.accelarate ());
When I run this program, I get the following results
" 8 Accelarate : SportsCar "
I assumed that the result would be " 9 Accelarate : SportsCar "
Can someone please xplain this to me.
Kind Regards,
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hi, Thesigan!
Car c = new SportsCar();
When you call a method on c, Java looks in the current OBJECT that c refers to for the method.
When you call a variable on c, Java looks in the CLASS that the variable c is declared from.
So, since c is of class Car, c.gearRatio = 9, and since c refers to an object of class SportsCar, c.accelarate() runs the method in class SportsCar.
Hope this helps!
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hmm... I mean that c.gearRatio should be 8 since c is of class Car. Sorry for not being very obvious...
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4447

The "technical" explanation is that methods participate in polymorphism while member variables do not. That is, the decision as to which particular method to call is done by the JVM at runtime (called dynamic binding) based on the actual class of the object whereas the decision as to which member variable to access is made by the compiler at compile time (static binding) based on the declared class.
In your case, c is declared as a Car, therefore the gearRatio accessed would be for Car, not for SportsCar. On the other hand, c is actually a SportsCar (because you wrote c = new SportsCar()). Once created an object will never change its actual type, although you can refer to or think of it as a different, compatible type. Hence, c.accelarate() [sic] will be a polymorphic call to SportsCar.accelarate().

Junilu - [How to Ask Questions] [How to Answer Questions]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I think that you are just confusing yourself. If you want a car declare a car.
Car c = new Car() ;
If you want a sportscar declare a sportscar.
SportsCar c = new SportsCar() ;
What you did was declare a Car but name the variable SportsCar. All this does is confuse the issue.

Please ignore post, I have no idea what I am talking about.
Thesigan Pillay

Joined: Jul 04, 2001
Posts: 5
Thank you. Your help is most appreciated.
I agree. Here's the link: http://aspose.com/file-tools
subject: extending a class
Similar Threads
Variable Shadowing
Super-Sub classes
clarification in overriding/shadowing reqd.
Which one is the main calling?
Confused with output