• 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

Difference between Super and this in Java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can some explains difference between Super and this in Java with an example?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the super key word
Using this keyword in Java
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
]basic differrence :
using super one can refer to the member of superclass
using this one can refer to the member of current class( the class in which this is used.)

important difference :
using this one can refer to the instance current object. wheres super cannot refer to instance of any class



 
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super is used for parent class things handling
this is used for current class
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Super and this should always be first statement if called from constructor.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats true thing that first statement within the constructor
 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using 'this' we can implement "Instance Variable Hiding"
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniket S. Kulkarni wrote:Using 'this' we can implement "Instance Variable Hiding"


Can you explain more about this?
using this would help us to access the instance variables associated with the instance.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohamed Sanaulla,
If your instance variable & method's variable(i.e.local to method) are same then this is used to avoid ambiguity.

you can find it with example
visit: Instance variable hiding instance variable hiding

I hope this will correct your doubt.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniket S. Kulkarni wrote:Mohamed Sanaulla,
If your instance variable & method's variable(i.e.local to method) are same then this is used to avoid ambiguity.


Its something called as Shadowing variables. Atleast thats what SCJP Guide uses in their explanation.

Aniket S. Kulkarni wrote: Using 'this' we can implement "Instance Variable Hiding"


"Instance Variable Hiding" - These words (in the above quote context) mean that instance variable is being hidden.
But what you have stated above is that "this" is used to resolve the ambiguity in referring to the variables with same name for local as well as the instance level- which is correct. And its not same as "Instance variable hiding".

The term being used is conveying a different meaning as the context in which you are using the term is different from that used in the article you linked in previous post. So I was trying to clarify this so that it doesn't confuse other readers who might be new to the keywords and terminology.

 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mohamed Sanaulla.
 
reply
    Bookmark Topic Watch Topic
  • New Topic