-12

Can any one please tell me what is the difference between super() call and this() call in java constructors?

Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56

7 Answers7

2

super() means the super class (parent) and this() means the current class.

Henrik
  • 1,797
  • 4
  • 22
  • 46
2

this() calls another constructor for the same class. In this case the 0 argument one.

super() calls the constructor for the super class.

Colin D
  • 5,641
  • 1
  • 23
  • 35
2

super() calls no-argument constructor from superclass and this() calls no-arg constructor from the current class.

Jakub H
  • 2,130
  • 9
  • 16
1

super() calls the parent constructor of the class and this() calls the constructor defined within the class.

//Example of super()
class parent
{
  parent()
  {

  }
}
class child()
{
   child()
   {
      super();   //Go to parent class constructor
   }
}


//Example of this    
class test
{
    test()
    {
       this("a");  //go to test one argument constructor within the test class
    }
    test(String a)
    {

    }

}
Crystal Meth
  • 589
  • 1
  • 4
  • 16
T8Z
  • 691
  • 7
  • 17
1

super() refers to the base/parent class. Can be used in constructor to invoke parent constructor but must be done in the declaration of the constructor.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
1

It means you are delegating part of the object construction to another constructor, being super() a constructor defined in a superclass and this() a constructor defined in the same class.

renke
  • 1,180
  • 2
  • 12
  • 27
0

The super() is for calling superclass' constructor . this() refers to the current class .

Here are good SO-links.

this and super in java

Difference between "this" and"super" keywords in Java

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216