super() as well as this() both are used to make constructor calls. super() is used to call Base class’s constructor coz they are Parent’s class while this() is used to call current class’s constructor.We can use super() as well this() only once inside constructor.
Example super() :
class Parent { Parent()
{ System.out.println("mindstick " + " arg constructor");
} }
class Child extends Parent {
Child()
{
super();
System.out.println("software" +
"Parent class no arg const");
}
public static void main(String[] args)
{
new Child();
System.out.println("Inside Main");
}
}
Example
this() :
class MM { MM()
{
this(10);
System.out.println("Flow comes back from " +
"MM class's 1 arg const");
}
MM(int a)
{
System.out.println("MM class's 1 arg const");
}
public static void main(String[] args)
{
new MM();
System.out.println("Inside Main");
}
}
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Difference between super() and this() in java:
super() as well as this() both are used to make constructor calls. super() is used to call Base class’s constructor coz they are Parent’s class while this() is used to call current class’s constructor.We can use super() as well this() only once inside constructor.
Example super() :
class Parent {
Parent()
{ System.out.println("mindstick " + " arg constructor"); }
}
class Child extends Parent {
Child()
{
super();
System.out.println("software" +
"Parent class no arg const"); }
public static void main(String[] args)
{
new Child();
System.out.println("Inside Main");
}
}
Example this() :
class MM {MM()
{ this(10);
System.out.println("Flow comes back from " + "MM class's 1 arg const");
}
MM(int a)
{
System.out.println("MM class's 1 arg const");
}
public static void main(String[] args)
{
new MM();
System.out.println("Inside Main");
}
}