aspose file tools*
The moose likes Beginning Java and the fly likes this and super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this and super" Watch "this and super" New topic
Author

this and super

Sameera Abeysinghe
Ranch Hand

Joined: Nov 15, 2004
Posts: 104
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Mar 13, 2004
Posts: 1272


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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Thanks Nick, I fixed it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this and super