• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super keyword analysis

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ritchi for pointing the spelling mistake.
I hav modified it to reduce confusion
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic