wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the use of SUPER call in the derived class constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use of SUPER call in the derived class constructor?" Watch "What is the use of SUPER call in the derived class constructor?" New topic
Author

What is the use of SUPER call in the derived class constructor?

Pavan Kumar Dittakavi
Ranch Hand

Joined: Feb 12, 2011
Posts: 104

hi all,

Let us say that we have a class A as follows.

Class A
{
private int a;
public int b;
}

And class B, which extends class A.

Class B extends A
{
int c;
}

Now, if I create an object for B, then will it be having the member variable 'b' as we have inherited?
The answer is YES.

But if that is the case, generally why do we use a call to SUPER in the B class' constructor?..Obviously super creates an instance of class A. But why?

Thanks,
Pavan.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1162
    
    1

B class calls super in its constructor to create class A object. This is required because class B extends class A. Which essentially means that class B inherit some characteristics of class A. Now to make it possible, it is required that class B object should contain class A object also.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

It's misleading to say that class B object contains a class A object - it doesn't. It is a class A object. And it's also not true that super() creates an instance of class A.

I'd put it like this. Constructors don't create objects, they initialise them. A class B object IS-A class A object, and so the parts of it that it's inherited from class A need to be initialised as well. That's why the super-class constructor must always be called.

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1162
    
    1

thanks Matthew for replying to this post.
Yes you are correct. Agreed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of SUPER call in the derived class constructor?