2011-12-08

What is super method in java ?

super() method is used to call super class constructor from subclass constructor to create super class object .You might get a doubt that who does place this super() method in all constructors. Compiler only places the super method call in all constructors at the time of compilation.Even though we don't place super() method in constructor compiler places the super() method call in all constructors explicitly.


//Example.java
class Example extends Object
{
   Example(){
      //super();This method is placed by constructor implicitly to call object class constructor
      System.out.println("Example No-arg constructor");
   }
}

//Sample.java
class Sample extends Example
{
   Sample(){
      //super(); This method is placed by constructor implicitly
      System.out.println("Sample No-arg constructor");
   }
   public static void main(String[] args){
      Sample s = new Sample();
   }
}

Object class is nothing but the superclass of all classes it contains 11 methods. In the above program compiler automatically extends the object class in Example program even if you don't extend the object class
When super() method should be placed by developer ?
Developer should place super() method call in below two cases
1. If super class does not contain no-arg constructor that is if it contains parameterized constructor.  
2. If developer wants to create super class object with parameterized constructor

In the above two cases developer should define constructor in sub class with explicit super() method call by passing argument of type same as super class constructor type.Check the below program.


//Example.java

class Example
{
   Example(){
      //super(); Compiler placed super method call
      System.out.println("Example no-arg constructor");
   }
   Example(int a){
      //super(); Compiler placed super method call
      System.out.println("Example int arg constructor");
   }
}

//Sample.java

class Sample extends Example
{
   Sample(){
      //super(); Compiler placed super method call
      System.out.println("Sample no-arg constructor");
   }
   Sample(int a){
      //super(); Compiler placed super method call
      System.out.println("Sample int-arg constructor");
   }
   public static void main(String[] args)
   {
      Sample s1 = new Sample();
      Sample s2 = new Sample(10);
   }
}   

Output:

Example no-arg constructor
Sample no-arg constructor
Example no-arg constructor
Sample int-arg constructor


If we do not place super method call by passing a value in parameterized constructor,compiler places default super() method call ,then Example no-arg constructor is called shown in the above program. So if we want to call super class parameterized constructor developer need to place super method call explicitly with some value in subclass parameterized constructor by creating subclass object by passing some value. The below program shows the importance of super method call that should be placed explicitly.


//Example.java
class Example
{
   Example(){
      //super(); Compiler placed super method call
      System.out.println("Example no-arg constructor");
   }
   Example(int a){
      //super(); Compiler placed super method call
      System.out.println("Example int arg constructor");
   }
}


//Sample.java
class Sample extends Example
{
   Sample(){
      //super(); Compiler placed super method call
      System.out.println("Sample no-arg constructor");
   }
   Sample(int a){
      super(10);
      System.out.println("Sample int-arg constructor");
   }
   public static void main(String[] args)
   {
      Sample s1 = new Sample();
      Sample s2 = new Sample(10);
   }
}   


Output:


Example no-arg constructor
Sample no-arg constructor
Example int arg constructor
Sample int-arg constructor


Loading

Enter your Email here: