• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clarification on Inheritance Concept  RSS feed

 
Antonio Subasic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In currently reading a book for beginner Java, and I can't across something (in regards to inheritance) which I would like clarified, please.

The book says: "instance variables cannot be overridden (although they can be redefined in the subclass, but that's not the same thing, and there's almost never a need to do it."

It would be fantastic if someone could clarify:

1) what's the difference between overwriting something and redefining it?

2) why is there almost never a need to redefine an inherited instance variable?

Thank you very much!
Antonio

Note: first time using these forums and I'm on a mobile device, my apologies if I did something wrong.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Why does the Car class want a speed field? It already has one in the Vehicle class (although private members are not actually inherited). It already has to String methods, getSpeed, slowDown, etc. Why does it need different methods, unless the mechanism for slowing down is different? Note I marked getSpeed final so you cannot even override it. If you designed the Vehicle class well, you only need a constructor and the Vehicle class will take care of everything the Car class needs.What good is the speed field in the Car class? The getSpeed method is in Vehicle and it is final so you must use the field from Vehicle. In fact the field in Car will show 0 for ever. If you try to use it in toString you can do something like this:-Reember: overriding applies to non‑private instance methods, non‑private instance methods and non‑private instance methods. Nothing else. Not fields, which are bound statically. Not static methods which are bound statically (that is probably why they are called static in the first place). Only non‑private instance methods are bound dynamically. Fields are redefined; if you have a field in the subclass there are two fields with the same name. Static methods are hidden, and you can cause yourself no end of confusion if you hide methods or redefine fields.

If you find a copy of Java Puzzlers by Bloch and Gafter, there are several examples of that sort of problem
 
Antonio Subasic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Campbell, if possible, could you please clarify one thing about your response for me?

I'm just curious about this example:

Vehicle myCar = new Car(...);
myCar.speedUp(99);
System.out.println(myCar); // A Car travelling at 0mph.

Assuming the System.out.println method printed the speed of the object referenced by the myCar variable, why would it print 0mph?

Didn't we just change the speed with myCar.speedUp(99)?

I'm pretty sure this is because the speed of the car isn't inherited, and therefore to print the real speed that the car is moving at after changing it, we'd need to use a public getter in the vehicle class, to access the private speed variable in the vehicle class.

Also, in the example you provided, wouldn't accessing the private variable speed (not inherited) return an error at compile time?

Could you please confirm/clarify?

Thank you very much!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Campbell's example is missing something. As it stands, printing the myCar variable would not print out anything to do with the speed no matter what value it has. It will use the Object.toString method.

However the point Campbell is making is that changing the speed in Vehicle does not change the unrelated speed in Car. So if Car where to define a toString method that prints the Car.speed variable it would print 0, even if you had changed the Vehicle.speed variable.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
minhua guo wrote:. . . The nunchuk serves as the flight stick, and you tilt and twist appropriately to steer. . . .


Should have left that post unghosted
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I think Campbell's example is missing something. . . .
It is. I did say
. . . the field in Car will show 0 for ever. If you try to use it in toString . . .
That means the Car class would have a toString method like this:-Then System.out.println(myCar); will reliably display 0mph. I was presuming that you would write the toString method in Vehicle.If you don't override that toString method in Vehicle, all will be well.
Now you can see that the speed field I have incorrectly added to the Car class is never used and retains its default value for ever. If you incorrectly write this sort of constructor in Car, you can still see that its speed field is never used.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to think it like this:





This code is self explanatory of your doubts.numberOfPower and costumeColor are defined by the specific superhero accordingly.
All the super hero must have the same aim of their life and they define special power accordingly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!