Wednesday, August 6, 2014

Java's super and this keywords

Java's super Keyword
Call Non-Private Methods of Parent Class
When used inside of a method the super keyword allows you to call the non-private methods of the class you're deriving from. Typically you'll use this when you need to override a method but still want to keep the implementation that's in the parent class.
public class Parent {

   public void fooBar()  {
      System.out.println("Hello World");
   }
}
which you then override in the following Child class.
public class Child extends Parent {

   public void fooBar() {
      super.fooBar();
      System.out.println("Goodbye World");
   }

   public static void main(String[] args) {
      new Child().fooBar();
   }
}
Running the Child class will result in the following output:
Hello World
Goodbye World

Check out the post I did on overloading and overriding a method in Java.
Call Constructor of Parent Class
The super keyword can also be used to call a constructor that's in the parent class. When used it has to be the first statement in the constructor.
public class Parent {

   public Parent(String str) {
      System.out.println("Parent: " + str);
   }
}
public class Child extends Parent {

   public Child() {
      super("Bonjour");
      System.out.println("Child: Bonjour");
   }

   public static void main(String[] args) {
      new Child();
   }
}
Output
Parent: Bonjour
Child: Bonjour
Note: It's a compiler error if you don't use the super keyword and the Parent class doesn't have a default (no parameter) constructor.
public class Child extends Parent {

   public Child() { // compiler error
      System.out.println("Child: Bonjour");
   }

   public static void main(String[] args) {
      new Child();
   }
}
Java always calls the Parent's default constructor when instantiating a Child class unless you tell it to use a different constructor with the super keyword.
public class Parent {

   public Parent() {
      System.out.println("Parent's default constructor");
   }
}
public class Child extends Parent {

   public Child() {
      System.out.println("Child's default constructor");
   }
}
When a new Child object is instantiated, Java will output the following...
Parent's default constructor
Child's default constructor
Java's this Keyword
Reference Object Method Is Called On
The this keyword is a reference to the object a method is being called on. Sometimes you'll see programmers return this so that they can chain method calls.
public class Klass {

   public Klass fooBar() {
      System.out.println("fooBar called");

      return this;
   }

   public static void main(String[] args) {
      new Klass().fooBar().fooBar();
   }
}
Access Shadowed Fields
Since the this keyword is a reference to the current object it can be used to access the fields of a class that have been shadowed in a method or constructor.
public class Klass {
   private boolean flag;

   public Klass(Boolean flag) {
      this.flag = flag;
   }
}
Reference Enclosing Outer-Class Object
public class EnclosingOuter {

   public EnclosingOuter() {
      System.out.println("(outer) this = " + this);
      new NestedInner();
   }

   public class NestedInner {

      public NestedInner() {
         System.out.println("(inner) this = " + this);
         System.out.println("(inner) Outer.this = " + EnclosingOuter.this);
      }
   }
}
Example Result
(outer) this = EnclosingOuter@780adb3f
(inner) this = EnclosingOuter$NestedInner@34a8a271
(inner) Outer.this = EnclosingOuter@780adb3f
Call Overloaded Constructor
public class Klass {
   public Klass() {
      this("Hello World");
   }

   public Klass(String str) {
      System.out.println("str = " + str);
   }

   public static void main(String[] args) {
      new Klass();
   }
}
The this() method must be the first statement in the constructor. Therefore, you cannot use both the super() and this() methods in the same constructor.
public class SubClass extends SuperClass {
   public SubClass() {
      super("Hello World");
      this("Yes"); // Compiler Error: Call to 'this' must be first statement in constructor body.
   }

   public SubClass(String str) {
      super(str);
   }
}
Also, the this() method can only be used one time in the constructor.
Reference
Cay Horstman's book Big Java pages 68, 307, 439, and 443.

Java Programming : Advanced Topics (first edition) by Joe wigglesworth and Paula McMillan, pages 168 - 170, 216 - 218.

No comments:

Post a Comment