• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

extending a class

 
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
 
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...
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • 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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic