views:

250

answers:

8
+4  Q: 

super() in Java

Is super() used to call the parent constructor? Please explain super().

+8  A: 

Is 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.

Heinzi
`super()` is used to call the parent constructor, `super.myMethod()` is used to call an overridden method.
seanizer
@seanizer or any other method of the superclass (including statics) if its in scope. super is just a reference to your base class.
atamanroman
I don't think super() is used to call base class methods. You can use super.method() though.
Dheeraj Joshi
@seanizer, @Dheeraj: Thanks for your feedback, I've adapted my answer.
Heinzi
+3  A: 

Yes, super() (lowercase) calls a constructor of the parent class. You can include arguments: super(foo, bar)

There is also a super keyword, that you can use in methods to invoke a method of the superclass

A quick google for "Java super" results in this

Bozho
+4  A: 

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

pakore
+2  A: 

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
aioobe
In my base class i overload the constructor with one,two,...arguments in my derived class i use super() without any argu. then what will happen whether it's automatically calls a default constructor of a base class
Mohan
Yes. If you call `super()` it will invoke the constructor of the super-class that takes no arguments. Similarly, it will invoke the 1-argument constructor if you do `super(arg1)`, and so on.
aioobe
if there was no constructor without any argu in base class then what happens if derived class calls super().
Mohan
Nothing. It will not compile. If you provide a constructor yourself, the automatic/default/no-argument-constructor will not be generated, thus `super()` will not be a valid call.
aioobe
+1  A: 

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.

Sagar V
+1  A: 

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.

Csharper
Why didn't you indent your sourcecode examples? Is there a specific reason?
erikb
NO erikb,I want to know the usage of super().Hereafter only i going to
Mohan
use it.Thanks a lot to all.
Mohan
In my base class i overload the constructor with one,two,... arguments
Mohan
but in my derived class i use super() without any argu. then what will happen whether it's automatically calls a default constructor of a base class
Mohan
A: 
Olathe