February 2, 2011

super keyword in Java

The super keyword can be used to invoke an overridden method in the superclass from the overriding method in the subclass

public class SuperClass {
   public void m1() {
      System.out.println("In SuperClass");
   }
}

public class SubClass extends SuperClass {
   public void m1() {
      super.m1();
      System.out.println("In SubClass");
   }
}

It can also be used to invoke a superclass constructor from the subclass constructor.


public class SuperClass {
   public SuperClass() {
      System.out.println("In SuperClass constructor");
   }
}

public class SubClass extends SuperClass {
   public SubClass() {
      super();
      System.out.println("In SubClass Constructor");
   }
}

1 comment:

  1. Hello,
    A sentence in this explanation is incorrectly worded "The super keyword can you used to invoke"

    ReplyDelete