• 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

A doubt on overriding

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a small program I wrote to check my understanding about overriding and inheritance.



While results of line 1 and 3 are in line with my expectations, I'm wondering why line 2 gives the string "thingA chair" instead of "XYZA chair". In class Chair it's not defined a getName() method, so it's used the one defined in superclass, OK. But in Chair it is defined the variable name. So, why don't simply and plainly use it?
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In class Chair it's not defined a getName() method, so it's used the one defined in superclass, OK. But in Chair it is defined the variable name. So, why don't simply and plainly use it?



Because polymorphism doesn't apply to instance variables. For methods, a jump table is created, to make sure that the latest version of the method is called. For instance variables, there is no such technique. The latest version is determined at compile time -- and at compile time of the Thing class, the latest version of the name instance variable is the one in the Thing class.

Henry
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where is polymorphism here?
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paolo Dina wrote:

Where is polymorphism here?




<Jaded Response> I am pretty sure I am going to regret answering this question, as the response will be used to setup a "strawman" argument for something else </Jaded Response>

Basically, polymorphism will make sure that the latest inherited method, for the actual object will be called. In this case, since it is a Chair object, the latest methods are the getName() method in the Thing class, and the verboseName() method in the Chair class.

Henry
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learned that polymorphism was only about the possibility for a subtype to be used (or to appear) instead of one of its supertypes. For me it was something like:



Maybe I was seeing only one half of the coin, but I never thought that polymorphism could apply even when no type/subtype substitution occour.

Thanks for your explanation, it makes sense to me (at least, I hope so!)
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






LINE1:In this line there is no polymorphism.simply it is calling getName and verboseName methods in Thing class.

LINE2:Inheritance works in this case.it is calling getName() from Thing and verboseName() from Chair class.
important thing is instance variables are not inherited..so when getName() method is executed it uses the instance var with in that class..so it is displaying thingA chair..

LINE3:polymorphism works here.getName() is executed from Thing and verboseName() is executed with in Chair because of overriding mechanism..
 
Paolo Dina
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganeshkumar, thanks for your answer.

Ganeshkumar cheekati wrote:
LINE2:Inheritance works in this case.it is calling getName() from Thing and verboseName() from Chair class.
LINE3:polymorphism works here.getName() is executed from Thing and verboseName() is executed with in Chair because of overriding mechanism..



I don't think that polymorphism works here and inheritance works there. If there is inheritance there is polymorphism, because the latter is "enabled" by the former, and they work together; so in line 2 AND 3 both inheritance AND polymorphism work. But if it's true that Thing and Chair define their own instance variable 'name', it's also true that polymorphic resolution is bypassed for static variable, static methods and instance variable. They are resolved just once and at compile time. And

Henry Wong wrote:at compile time of the Thing class, the latest version of the name instance variable is the one in the Thing class.

 
Are you okay? You look a little big. Maybe this tiny ad will help:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic