开发者

What's the difference between this and super keywords in java?

Check my coolMethod, babes:

package zoo;

public class Zoo {

    public String coolMethod() {
        return "Wow baby!";
    }

}

My Moo Class, that extends my Zoo class, full of ways to do the same thing, which is calling my coolMethod.

package zoo;

public class Moo extends Zoo {

    public void useAZoo(){

        Zoo z = new Zoo();

        System.out.println("A Zoo says: "+ z.coolMethod());
    }

    public void useMyCoolMethod(){

        System.out.println("My cool method 开发者_C百科says: " + super.coolMethod());

    }

     public void useMyCoolMethodAgain(){

        System.out.println("My cool method says: " + this.coolMethod()+ " (again)");

    }

    public void inheritMyMethod(){
        System.out.println("My inhertied method says: " + coolMethod());
    }

}

And my main, that calls my Moo class functionalities.

package javacert1;

import zoo.Moo;

public class Main {


    public static void main(String[] args) {

        Moo moo = new Moo();

        moo.useAZoo();

        moo.useMyCoolMethod();

        moo.useMyCoolMethodAgain();

        moo.inheritMyMethod();
    }

}

As you can see, all those four calls get the same results. I'm learning Java and I practiced with an example that used the "this" but I had seen the use of "super" elsewhere so I tested it and the results where the same. Why is this? What's the major difference between a this and super keyword?


Since you have only one coolMethod, it is called in each case, regardless of qualifiers. However, if you override your method in Moo e.g. like this

public class Moo extends Zoo {
    public String coolMethod() {
        return "Yeah dude!";
    }
    // the rest is the same as above...
}

you will notice the difference between the calls to coolMethod() and this.coolMethod() versus the call to super.coolMethod().


this can be pointing to current object

super can be used for accessing Super class metods & variables

this() can be used to invoke a constructor of the same class

super() can be used to invoke a super class constructor

Difference with examples


Cool example, overmann :)

Here is another example ... has 2 source files - LevelSub.java which extends LevelSup.java

Save the 2 in some folder, compile using "javac LevelSub.java" (compiler will know from "extends" that it has to compile "LevelSup.java", too), and run "java LevelSub". The comments in the source code should tell you the difference.

this and super are used when there is a name clash for variables or methods (e.g., same name is used in super/sub classes or in a method body, or you want to call a method from the superclass that has been overridden). "this" refers to the current object instance and "super" refers to the one it is inheriting from. For no name clashes, the this or super prefix are redundant.

public class LevelSub extends LevelSup {
    protected String myName;

    public LevelSub (String myName) {
        super ("SuperClass");
        this.myName = myName;       // sets up instance variable
    }

    public void print () {
        System.out.println ("Hi, this is " + myName); // refers to this.myName
        System.out.println ("I am inherited from " + super.myName);
        System.out.println ("also known as " + defaultName); // gets from superclass
        super.print();           // overridden method of superclass
    }

    public static void main (String[] args) {
        new LevelSub("SubClass").print();
}}

and the other source ...

public class LevelSup {
    // cannot be private if accessed by subclass
    protected String myName, defaultName = "TopLevel";

    public LevelSup (String name) {
        myName = name;      // this not required, no name clash
    }

    // cannot be private if accessed by subclass
    public void print () {
        System.out.println ("Hi, this is " + myName);
    }
}


You are not overriding the coolMethod and you are inheriting it.
As a result it calls the same method.
Override coolMethod and see what happens.
super will call the method of base.
this will call method in this object


"this" refers to the object you're in. "super" refers to the parent of this object. I think you're missing the difference because of some other things you have going on.

For example, this code


public class Zoo {

    public String coolMethod() {
        return "Wow baby!";
    }

}



public class Moo extends Zoo {

    public void useAZoo(){

        Zoo z = new Zoo();

        System.out.println("A Zoo says: "+ z.coolMethod());
    }

    public void useMyCoolMethod(){

        System.out.println("My cool method says: " + super.coolMethod());

    }

     public void useMyCoolMethodAgain(){

        System.out.println("My cool method says: " + this.coolMethod()+ " (again)");

    }

    public void inheritMyMethod(){
        System.out.println("My inhertied method says: " + coolMethod());
    }

}

The method "coolMethod" is defined in Zoo, but it's a public method so it is naturally inherited by Moo. So, when you're within your Moo class and you call this.coolMethod or super.coolMethod, you end up with exactly the same thing. Where things get far more interesting is if you override the coolMethod in your subclass, like this:


public class Zoo {
    public String coolMethod() {
        return "Wow baby!";
    }
}

public class Moo extends Zoo {

    @Override
    public String coolMethod() {
        return "Moo baby!";
    }

    public void doIt() {
        System.out.println("Using super: " + super.coolMethod());
        System.out.println("Using this: " + this.coolMethod());
    }
}

Try executing "doIt" in that case and I think you'll see the difference much easier.


In this case, they will do nothing different.

However, if you were to override coolMethod in the Moo class, the call to the super version will call the version in the Zoo class, but all the other versions will call the new overridden one.


To see the difference, overwrite the coolMethod in your subclass Moo:

public String coolMethod() {
    return "Moo baby!";
}


I suggest you, to create a coolMethod in your Moo class, that print something different, from the one in the Zoo class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜