1

What is the difference between these two constructors.

public Students(String name, String address) {
    super();
    this.name = name;
    this.address = address;
}

/*Here is constructor without super function call.*/
public Students(String name, String address) {
    this.name = name;
    this.address = address;
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
M.ArslanKhan
  • 3,640
  • 8
  • 34
  • 56

5 Answers5

3

If there is no explicit call to a super class constructor, the compiler will generate a call to super(). Because of this there is no difference between your examples.

Joni
  • 108,737
  • 14
  • 143
  • 193
2

No Difference, Its just a matter of explicit and implicit. In the second case, it implicitly calls the super constructor of this Students class if it is inherited from a parent class. Read more on this. You can find more info if you google it.

public class A {
    //there is a hidden constructor. Even if you explicitly write it  
    //public A(){
    //}
}

public class B extends A {
 private int i;


public B(int x){
   this.i = x;
}

}

when you create an Object form B you first call A's constructor implicitly. You can explicitly specify it though.

as

public B(int x){
       super();
       this.i = x;
}
diyoda_
  • 5,274
  • 8
  • 57
  • 89
1

what is difference between a construct having super function and without it.

Well, it can be easily tested. They are actually the same for this case. More importantly, you probably want to know when and how to use super.

Running the following codes:

public class Test
{             
    public static void main(String[] args) 
    {       
        new Students("a", "b");
    } 
}

class Parent
{
    public Parent(){
        System.out.println("Parent class constructor invoked");
    }
}       

class Students extends Parent
{
    String name;
    String address;

    public Students(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }
}

OUTPUT: Parent class constructor invoked

Now we remove the super();

class Students extends Parent
{
    String name;
    String address;

    public Students(String name, String address) {
        this.name = name;
        this.address = address;
    }
}

OUTPUT: Parent class constructor invoked

Just like how classes are implicitly extending to Object even when you don't extend them to something. "By default" a super(); will be called in every constructor if one is not given by you.

user3437460
  • 17,253
  • 15
  • 58
  • 106
0

super() just calls the parent constructor of the function and lets you either pass arguments such as super(someArgument) or can be used to call methods of the parent constructor such as super.someMethod().

super() alone just calls the parent constructor without passing any arguments or referencing any methods of the parent. The actual decision on when to call super() depends on the purpose of the overridden method and also the behaviour expected by the base class when you are overriding a certain method.

Check out The Java™ Tutorials or JavaTPoint to learn more.

MrCrowly
  • 168
  • 2
  • 13
0

Java implicitly calls the constructor of the super class, if you haven't called it explicitly (constructor chaining - search this term on google for more info). Even if your class is not explicitly inherited from any class, java implicitly makes it inherit the Object class (and makes a call to the constructor of the Object class).

So, there's no difference between them.

Wololo
  • 841
  • 8
  • 20