Is super()
used to call the parent constructor?
Please explain super()
.
views:
250answers:
8Is super() is used to call the parent constructor?
Yes.
Pls explain about Super().
super()
is a special use of the super
keyword where you call a parameterless parent constructor. In general, the super
keyword can be used to call overridden methods, access hidden fields or invoke a superclass's constructor.
Here's the official tutorial: http://download.oracle.com/javase/tutorial/java/IandI/super.html.
super()
calls the parent constructor with no arguments.
It can be used also with arguments. I.e. super(argument1)
and it will call the constructor that accepts 1 parameter of the type of argument1
(if extists).
Also it can be used to call methods from the parent. I.e. super.aMethod()
More info an tutorial here
Yes. super(...)
will invoke the constructor of the super-class.
Have a look at this example:
class ClassA {
public ClassA(String s) {
System.out.println("Constructor of A: " + s);
}
}
class ClassB extends ClassA {
public ClassB() {
super("super-argument!");
System.out.println("Constructor of B");
}
}
public class Test {
public static void main(String[] a) {
new ClassB();
}
}
Prints:
Constructor of A: super-argument!
Constructor of B
That is correct. Super is used to call the parent constructor. So suppose you have a code block like so
class A{
int n;
public A(int x){
n = x;
}
}
class B extends A{
int m;
public B(int x, int y){
super(x);
m = y;
}
}
Then you can assign a value to the member variable n.
1.Super keyword is used to call immediate parent.
2.Super keyword can be used with instance members i.e., instance variables and instance methods.
3.Super keyword can be used within constructor to call the constructor of parent class.
OK now let’s practically implement this points of super keyword.
Check out the difference between program 1 and 2. Here program 2 proofs our first statement of Super keyword in Java.
Program 1
class base
{
int a = 100;
}
class sup1 extends base
{
int a = 200;
void show()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup1().show();
}
}
Output : -
200
200
Now check out the program 2 and try to figure out the main difference.
Program 2
class base
{
int a = 100;
}
class sup2 extends base
{
int a = 200;
void show()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup2().show();
}
}
Output
100
200
In the program 1, the output was only of the derived class. It couldn’t print the variable of base class or parent class. But in the program 2, we used a super keyword with the variable a while printing its output and instead of printing the value of variable a of derived, it printed the value of variable a of base class. So it proofs that Super keyword is used to call immediate parent.
OK now check out the difference between program 3 and program 4
Program 3
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}
class sup3 extends base
{
int a = 200;
void show()
{
System.out.println(a);
}
public static void main(String[] args)
{
new sup3().show();
}
}
Output
200
Here the output is 200. When we called the show function, the show function of derived class was called. But what should we do if we want to call the show function of the parent class. Check out the program 4 for solution.
Program 4
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}
class sup4 extends base
{
int a = 200;
void show()
{
super.show();
System.out.println(a);
}
public static void main(String[] args)
{
new sup4().show();
}
}
Output
100
200
Here we are getting two output 100 and 200. When the show function of derived class is invoke, it first calls the show function of parent class because inside the show function of derived class we called the show function of parent class by putting the super keyword before the function name.