Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending a class

 
Thesigan Pillay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... I mean that c.gearRatio should be 8 since c is of class Car. Sorry for not being very obvious...
 
Junilu Lacar
Bartender
Pie
Posts: 7316
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thesigan Pillay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Your help is most appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic