• 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

"this " & "super"

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain with example wer "this " and "super" are used .
when to use it and how to use it explain it pls with the example u give with it.
bcoz i have got therotical ans i need explanation with example
can any one help me out.
this may be a trivial question ,but plz do give me the explanation...
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key word this is used to refer any members in the current. Like
this.empno = 10; this.print();
super key word is used to refer or call any thing i the parent class. Suppose you have overloaded a method and you want the super class version to be called first and the overrided version should be executed then you use 'super'

The very good example for this is calling the super class constructors from the child class constructor.


Ps :- Pleae try to post in english and avoid words like Bcoz , plz etc .. as this forum is for international readership


[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this some kind of homework support you are asking for, Harish?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the code below. Run it and you will know what is the different between super and this.

public class Test19 extends Test18{
public static void main(String args[]) {
Test19 t = new Test19();
t.print();
}

void print(){
this.printss();
super.printss();
}

void printss(){
System.out.println("Test19.printss");
}
}

class Test18{
void printss(){
System.out.println("Test18.printss");
}
}
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivasa,

i think in your explanation you made little mistake: the method is not overloaded but overridden
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh!! .. I'm Sorry .. I have changed it
[ October 17, 2005: Message edited by: Srinivasa Raghavan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic