This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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.
 
What's a year in metric? Do you know this metric stuff tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic