• Post Reply Bookmark Topic Watch Topic
  • New Topic

super keyword analysis  RSS feed

 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1>Hello everyone I understand the code but my question is
super:its a keyword used to invoke base accessible members
but can you tell me its equivalent meaning i.e in case of fields its just upcast of this and then invokation of field ex:super.name in beer is equivalent to ((Animal)this).name
similarly what is its equivalence in case of method invokation using super keyword.

2>In this above code in Animal.check() when we call run(), it makes a call using this and this is pointing to Bear object so overriding method is invoked due to runtime polymorphism
but super is used to invoke base members.Though it seems like reference variable holding base object but its not true.super is not a variable not like this(final variable)
how is it able to access base object's member??what internally goes on for super keyword which isn't a variable unlike this??

thanks in advance
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roopam Samal wrote:1>... can you tell me its equivalent meaning i.e in case of fields its just upcast of this and then invokation of field ex:super.name in beer is equivalent to ((Animal)this).name
similarly what is its equivalence in case of method invokation using super keyword.

There isn't one - one of the reasons why the super keyword is required, it isn't just syntactic sugar (an easier way to do something that could be done without the keyword).

2>Though it seems like reference variable holding base object but its not true.super is not a variable not like this(final variable)
how is it able to access base object's member??what internally goes on for super keyword which isn't a variable unlike this??

this is not a variable, it is also a key word. It can be used in ways that variables can't (for example as the first line of a constructor to call another constructor in the same class before the super constructor gets called. Like all keywords they get special handling in the compiler to make sure their intended purpose is met.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always indent code and avoid long lines, like that // comment, which I shall have to break up. Please always check spellings; class Beer extends Animal strikes me as strange; maybe you meant Bear

What you are doing is overriding a method. Using super.foo() calls the superclass' foo() method rather than the current class' overridden version. It is mainly intended for use inside the overriding method. The super keyword refers to that part of the object which is inherited directly from the superclass, as opposed to that part created in the subclass. There are some diagrams in Sierra & Bates Head First Java 2nd edition, page 215 and page 228 which might help you. Also read one of our camp fire stories.
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ritchi for pointing the spelling mistake.
I hav modified it to reduce confusion
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!