This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending a class" Watch "extending a class" New topic
Author

extending a class

Thesigan Pillay
Greenhorn

Joined: Jul 04, 2001
Posts: 5
Hi,
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,
TP
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
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
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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().
HTH,
Junilu


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

Joined: Mar 16, 2001
Posts: 984

Right.
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
Greenhorn

Joined: Jul 04, 2001
Posts: 5
Thank you. Your help is most appreciated.
 
jQuery in Action, 2nd edition
 
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