1

I'm new to java. Recently I saw some code which was similiar to this:

class A {
    protected int myInt;

    public static void main(String[] args) {
        B b = new B();
        b.myFunction();
    }
}

class B extends A {
    public void myFunction() {
        this.myInt = 10;
    }
}

As far as I know, when creating a subclass instance, an instance of its parent is created as well. All protected and public members of base class are accessible from the subclass. If I override myInt there will be a difference between this.myInt to super.myInt because each class will have its own myInt (B will have access to both).

So, my question is: if I don't override myInt, which form is preferable, this.myInt or super.myInt?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • You don't override fields. And a `B` is also an `A`, but only one instance is created - not two. – Elliott Frisch Sep 16 '16 at 22:00
  • 2
    possible duplicate http://stackoverflow.com/questions/4023344/difference-between-this-andsuper-keywords-in-java – Dev. Joel Sep 16 '16 at 22:01
  • Clarification of comment by @ElliottFrisch: If you also declare `myInt` in `B`, you're not *overriding* the field, you're *hiding* the field of same name in `A`. See [The Java™ Tutorials - Hiding Fields](https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html). – Andreas Sep 16 '16 at 22:59

3 Answers3

2

You only need to use this or super when need to specify which scope are you using/referring to. In your case, I'll prefer to omit the this to simplify the readability.

super is used to represents the current instante of a parent class while this is used to represents the current class. You only need to used this or super if some variable or method overlaps (Have the same name) with one in a wide scope.

eg. If you have define a method parameter with the same name as class attribute, you need to use this to indicate that you are using the class attribute and not the method parameter.

public class A {

    public int myInt = 1;

    public static void main(String[] args) {
        B b = new B();
        b.myFunction(3);
    }
}

class B extends A {

    public int myInt = 2;

    public void myFunction(int myInt){

        System.out.println(myInt); // The parameter
        System.out.println(this.myInt); // myInt from the current class (B)
        System.out.println(super.myInt); // myInt from the parent class (A)
    }
}

This example will print:

3
2
1

If you don't have this kind of collission, the use of this is optional:

public void myFunction2(){
    System.out.println(myInt);       // Both refers to the same 
    System.out.println(this.myInt);  // variable myInt from class B
}
Manuel Vieda
  • 296
  • 5
  • 12
  • 1
    *Opinion:* In this case, I'll prefer to use `super.` to document that an inherited field is being referenced. Similarly, I'll prefer to use `this.` on non-inherited fields to document that a field is being referenced, not a variable/parameter. It is a Code Style decision that clarifies the code to casual readers, which I find better than "simple", less clear code. – Andreas Sep 16 '16 at 22:50
1

It's a matter of taste and the project's standards/guidelines more than anything else.

Personally, I wouldn't use either, and would just write myInt = 10.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Only one instance is created. If you instantiate a derived object, the parents constructor is called, but only one object is created. Also, the term this is more so used when there are different variables with the same name being referenced in a class. For example a simple constructor:

class SupClass{
    public int a = 1;
    int incA(){
        return ++a;
    }
}
class MyClass extends SupClass {
    public int a = 10;
    public int b = 20;
    MyClass() {};
    MyClass(int a, int b){
        this.a = a;
        this.b = b;
    }

    int incA(){
        return ++a;
    }
  public static void main(String args[])
  {
      SupClass d = new MyClass();
      System.out.println(d.a); //1, members known of type SupClass at compile-time, 
      System.out.println(d.incA()); //11, methods are virtual, decided at run-time
  }
}

Only use the super method when you want to explicitly use the value that is in the super class. To answer your question, only methods can be overwritten, member variables can not.

Maurice Abney
  • 223
  • 3
  • 14