8

I saw some Java examples that using this keyword to get superclass methods. Example: this.superClassMethod(). In usual case we would use super. Could someone help to clarify with example why the developer used this instead of super? Thank you.

gjman2
  • 912
  • 17
  • 28

4 Answers4

21

There is no difference between this.method() and super.method() until the said method() gets overridden in the caller's class.

For example, with

class SuperClass {

    public void method() {
        System.out.println("SuperClass");
    }

}

class SubClass extends SuperClass {

    public SubClass() {
        method();
        this.method();
        super.method();
    }

}

Calling

new SubClass();

Prints

SuperClass
SuperClass
SuperClass

While with

class SuperClass {

    public void method() {
        System.out.println("SuperClass");
    }

}

class SubClass extends SuperClass {

    @Override
    public void method() {
        System.out.println("SubClass");
    }

    public SubClass() {
        method();
        this.method();
        super.method();
    }

}

Calling

new SubClass();

Prints

SubClass
SubClass
SuperClass

In parallel, there is no difference between this.field and super.field until the said field gets hidden in the caller's class.

For example, with

class SuperClass {

    protected String field = "SuperClass";

}

class SubClass extends SuperClass {

    public SubClass(String field) {
        System.out.println(field);
        System.out.println(this.field);
        System.out.println(super.field);
    }

}

Calling

new SubClass("parameter");

Prints

parameter
SuperClass
SuperClass

While with

class SuperClass {

    protected String field = "SuperClass";

}

class SubClass extends SuperClass {

    private String field = "SubClass";

    public SubClass(String field) {
        System.out.println(field);
        System.out.println(this.field);
        System.out.println(super.field);
    }

}

Calling

new SubClass("parameter");

Prints

parameter
SubClass
SuperClass

Side note: methods() get overriden while fields get hidden.

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 2
    +1 for the concise key statement "there is no difference between this.method() and super.method() until the said method get overridden in the caller's class." – TheBlastOne Apr 17 '13 at 14:56
  • 4
    All that is true, but can be shortened to: If you want to call the superclassmethod, use `super`, otherwise do nothing as by default (without using `this`) always the "most current implementation" will be used. – Thorsten Dittmar Apr 17 '13 at 15:16
5

Using this does not invoke the superclass method. It is actually superfluous, because it specifically invokes this instance's method. It may be relevant if you want to call another constructor of the same instance, but otherwise it's the same as just calling the method.

It may be useful for variable scoping (for example when there's a local variable with the same name as an instance variable) to make sure the instance variable is used, but it makes no difference when calling methods.

Personally I'd think that the developer wanted to take advantage of code completion and the IDE shows possible method names after entering this. :-)

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
3

super is used to access methods of the base class, while this is used to access methods of the current class.

Few references
1) usage of this
2) critique on super on SO

Extending the notion, if you write super(), it refers to constructor of the base class, and if you write this(), it refers to the constructor of the very class where you are writing this code.

class Animal {
  void eat() {
    System.out.println("animal : eat");
  }
}

class Dog extends Animal {
  void eat() {
    System.out.println("dog : eat");
  }
  void anotherEat() {
    super.eat();
  }
}

public class Test {
  public static void main(String[] args) {
    Animal a = new Animal();
    a.eat();
    Dog d = new Dog();
    d.eat();
    d.anotherEat();
  }
}

The output is going to be

animal : eat
dog : eat
animal : eat

The third line is printing "animal:eat" because we are calling super.eat(). If we called this.eat(), it would have printed as "dog:eat".

Community
  • 1
  • 1
Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78
  • The "critique on super" reference actually pertains specifically to calling `super()` from a constructor, not the general notion of calling `super.`. – rob Apr 17 '13 at 14:52
  • Why not add the last example you mentioned to your `Test` class and the output? ;) – rob Apr 17 '13 at 14:53
0

A lot of the oracle/sun java code uses this. when it does not need to example see java\awt\Event.java #translate

public void translate(int dx, int dy) {
    this.x += dx;
    this.y +=     
}

(if you extract src.zip and search for this. in *.java will see many instances like this where the there is no overriding local variable/ param but still they use this style.

Nothing to do with params or auto completion (its just x) I think its a coding standard to make it clear that we are talking about a method or field in current/ this class.

Method of base class :Even if not yet over ridden in our class, it can be at a later date, so its future ready. Till the method is overridden it works fine, and if it is over ridden its accurate.

So I think its a best practice coding convention for clarity. It wont bloat the code as not writing is only syntax sugar and the compiler will add it in the class code.

tgkprog
  • 4,493
  • 4
  • 41
  • 70