Monday, 30 January 2017

SUPER KEYWORD IN JAVA

Super is a java keyword used to access super class members and constructors from sub class members and constructors. It has two syntaxes. They are –
  • ·      super() – used to call super class constructors from subclass constructors.
  • ·  super – used to call super class variables and methods from sub class member and constructors.

super() is used to invoke super class constructor from sub class constructors to provide memory for super class fields in sub class object and also to initialize them with the initialization logic given by super class developer.

Who places super() in constructors ?
Compiler places super() call in constructors at the time of compilation, provided there is no explicit super() or this() call placed by developer. It must be placed only in constructor as first statement.

Who initializes super class non-static variables ?
The particular class constructor which is called by super() does this. It is necessary to initialize all fields, therefore all constructors must be called. The java compiler automatically inserts the necessary constructor calls  in the process of constructor chaining, or developer can do it explicitly.

Example –
class Example1{
          int x=10;
          int y=20;
          Example1()
          {
                   System.out.println("Example no-args");
          }
}
public class Test2 extends Example1{
          int x=40;
          int y=60;
          Test2()
          {
                   super();
                   System.out.println("Test2 no-arg");
          }
          public static void main(String[] args) {
                   Test2 t = new Test2();                            
          }
}

Output –
Example no-args
Test2 no-arg


Example –
class Example1{
          Example1()
          {
                   System.out.println("Example no-args");
          }
          Example1(int a)
          {
                   System.out.println("Example int-args");
          }
}
public class Test2 extends Example1{
          Test2()
          {
                   // by default here super class constructor is called
                   System.out.println("Test2 no-arg");
          }
          Test2(int a)
          {
                   super(50);
                   System.out.println("Test2 int-args");
          }
          public static void main(String[] args) {
                   Test2 t = new Test2();        
                   Test2 t1 = new Test2(10);
          }
}

Output –
Example no-args
Test2 no-arg
Example int-args
Test2 int-args

When should developer place super() call explicitly ?
Developer should place super() call for calling parameterized constructor in two cases.
  • ·    If super class does not have no-args constructor or is declared as private.
  • · If developer wants to initialize super class non-static variable s with parameterized constructor.

In the above two access developer should define constructor in sub class with explicit super() call, by passing argument of type same as super class constructor parameter type.


Note – Inheritance can only be implemented if super class has a visible constructor. It means a non-private constructor if sub class is created in same package, only protected and public constructor in case sub vlasses is created in other package.

No comments:

Post a Comment