Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this and super

 
Sameera Abeysinghe
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is a very simple one
--------------------------------
class B{
void m(){
System.out.println("The m() Method in class B");
}
}

class C extends B{
void m(int a){
System.out.println("The m() Method in class C");
}
}

class D extends C{
void m(int a,int b){
System.out.println("The m() Method in class D");
}
}

class E extends D{
void m(int a,int b,int c){
System.out.println("The m() Method in class E");
((D)this).m(1,2);
}
}

class A{
public static void main(String [] as){
E e =new E();
e.m(1,2,3);
}
}
------------------------

I have used this and a cast (in bold letters)
What I want is to put this and super in a single line
I know this is not correct but just to show you what I want

super.this.m(1);
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your m() methods have different signatures. Just use the correct signature, and the right method will be called.

In other words, instead of this line:

((D)this).m(1,2);

This will work:

m(1,2);

Using super is only necessary if you overridden a method, and you need to get to the original method.

Henry
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everybody ,

I m not clear with 'this' & 'super '
could any body make it crystel clear to me .

plz dont post such type of reply that 'this' mean current object & 'super' means we r calling super class method or constructor . plz explain it in details & with example .

thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is a reference to the object that is executing. It is often used to access instance variables that are hidden by parameters or local variables of the same name, as in "this.value=value;"
super is a reference to the direct superclass of the object that is executing. It is used to access members of the superclass that are overridden (methods) or hidden (variables) by members of the class that isexecuting.

this and super are also often used in the first line of constructors.

[ December 11, 2004: Message edited by: Mike Gershman ]
[ December 12, 2004: Message edited by: Mike Gershman ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken, I believe Mike means class A to extend E, so that a super call on A will get the m in E.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nick, I fixed it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic