• Post Reply Bookmark Topic Watch Topic
  • New Topic

"Passing the buck" (of abstract method implementation)  RSS feed

 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

this is a slightly modified version of the code snippet in the certification study guide from sierra/bates for java 6 (page 43):




Now since Car extends Vehicle it has the abstract method goUpHill()... but it nevertheless states the same method declaration again (the commented line... think of it as uncommented).
Does Car have now 2 goUpHill() methods? 1 from Vehicle and 1 declared within Class?
And if no: why? (what is the code in the commented line then good for)

Thanks in advance
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:What does it actually mean if you uncomment it... that class Car "overrides" the abstract method of vehicle?
Or is this line simply superfluous and not good for anything (since it complies fine without it as well)?

Good question.

The answer is that, no, it isn't useless; although in the specific case of your Vehicle class it is (or would appear to be) redundant - not quite the same thing.

The main reason is that you can override an implemented method with an abstract one, providing it's not final. This is sometimes used to force developers to implement a method that was given a "default" implementation at some higher level - although it has to be said that I've never used the technique myself (or if I did, I've forgotten ).

HIH. And if you need more explanation, just shout.

Winston
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer.

If I rethink my question though:

Car can't have 2 goUpHill() methods, since then Mini (the first concrete subclass which has to implement ALL abstract methods up the inheritance tree) couldn't compile (since then both abstract methods would have to be implemented).

So there is only one goUpHill() method (the one declared way up in vehicle).

Since Car inherits it from Vehicle one wouldn't actually have to write the commented line in Car... doing so is just redundant I think, as you said... (like typing "abstract public" in front of interface method declarations)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bora Sabrioglu wrote:Since Car inherits it from Vehicle one wouldn't actually have to write the commented line in Car... doing so is just redundant I think, as you said...

Well thought out. I suspect you have the makings of a good programmer, because its NOT all about code.

Just one possibility for something like that might be a subclass that was developed separately from the superclass(es), and the developer wanted to make sure that his/her clients were forced to implement that method.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!