Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
super()
this()

super() vs. this()

Suleman

First of all, this super() is different from the super keyword in Java.

The super keyword in Java is used to access the parent members of a class.

class parent{
 int a = 100;
 int b = 200;
}
class child extends parent{
  
void print(){
   System.out.println("a="+super.a+" b="+super.b);
  }
}

class Driver {
    public static void main( String args[] ) {
      child obj = new child ();
      obj.print();
    }
}

super(), with parenthesis, is used to call the parent class constructor.

public class parent{
  parent(){
    super();
  }
}

super() can only be used in the first statement of the constructor.

The class here seems not to have inherited any class, but in Java, there is always a base class from which the child class inherits. It is the Object class.

The compiler will automatically insert the code for inheritance. For example, the person class will look like this after the compiler has inserted the code.

public class parent extends java.lang.Object{
  parent(){
    super();
  }
}

In the case that the class has a parameterized constructor:

public class parent{
int length; 
 parent(int length){    
    this.length = length;
  }
}

The child class has to pass the parameters or it won’t compile.

public class parent{
int length; 
 parent(int length){  // Parameter length required
    super();
    this.length = length;
  }
}
public class child extends parent{
 child(){
   super(1);  //Passing the required parameter
  }
}

The compiler also adds super() calls in every first statement of the constructor, but here it requires a parameter, so that won’t work. For that, you have to explicitly pass it.

The first statement of every constructor is a call to another constructor. We have seen it using super(), but we can also use this().

this() with parenthesis is different from the this keyword.

The this keyword is used to access the members of the current class. It can also be used to access the members of the parent class that are accessible from the child class, e.g., inheritance.

class parent{
    int length = 10;
    void printLength(){
        System.out.println(this.length);
    }
}
class child extends parent{
    int width = 5;
    int getArea(){
       return this.length * width;
    }
}

class Driver {
    public static void main( String args[] ) {
        child obj = new child();

        System.out.println( "Area="+obj.getArea() );
    }
}

this() with parenthesis is used to call the current class constructor. It should also be used in the first line of the constructor.

public class parent{
parent(){
  this(10);
  }
parent(int a){
 System.out.print(a);
 }
}

Code

class parent{
    parent(){
        System.out.println("Parent constructor called.");
    }
}
class child extends parent{
    child(){
        super();
        System.out.println("Child constructor called.");
    }
}

class Driver {
    public static void main( String args[] ) {
        child obj = new child();
    }
}

You can only use super() and this() once inside the constructor. If we use super() followed by this() or this() followed by super(), the code won’t compile.

RELATED TAGS

java
communitycreator
super()
this()
RELATED COURSES

View all Courses

Keep Exploring