Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using "this" keyword to get superclass methods

Tags:

java

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.

like image 713
gjman2 Avatar asked Apr 17 '13 14:04

gjman2


People also ask

What is the use of the super keyword in a class?

It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance. It can be use to access the data members of parent class when both parent and child have member with same name.

What is the use of Super with methods in Java?

Use of super with methods: This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword.

How to use super keyword with constructors in Java?

Use of super with constructors: super keyword can also be used to access the parent class constructor. One more important thing is that, ‘’super’ can call both parametric as well as non parametric constructors depending upon the situation. Following is the code snippet to explain the above concept:

What is the use of this keyword in Java?

There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Here is given the 6 usage of java this keyword. this can be used to refer current class instance variable. this () can be used to invoke current class constructor. this can be passed as an argument in the method call.


3 Answers

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.

like image 125
sp00m Avatar answered Oct 25 '22 18:10

sp00m


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. :-)

like image 26
Thorsten Dittmar Avatar answered Oct 25 '22 17:10

Thorsten Dittmar


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".

like image 3
Srujan Kumar Gulla Avatar answered Oct 25 '22 16:10

Srujan Kumar Gulla