• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super call, subclasses and inheritance  RSS feed

 
Steve Alpine
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im just curious what does super(); do in the following method, I understand its uses to access variables belonging to the superclass but i am unsure of what that one line does. Here is a sample constructor..

 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super() is used to chain the super class constructor. In this particular case, you don't really need to give it explicitly. It will be added by the compiler. Similarly, you can use this() to chain to some other constructor of the same class.

Only time you would need to add super() would be when, your super class doesn't have a 0-arg constructor (which you should really try to put), in which case, you have to explicitly chain to parameterized constructor using - super(arg1, arg2, ..), because the compiler will only add super() to chain to 0-arg constructor.

Chaining a super class constructor is required to initialize the state of the object in the super classes. Remember, the state of an object consists of all the fields defined in that class, and in all of it's superclasses. One more thig - if you're putting the super() or this() in your constructor, make sure that is the first statement in the constructor. You can either put super() or this(), but not both, for the reason just stated.

You might also want to go through the JLS Section 12.5 - Creation of New Class Instance
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case you don't know..... In the above explanation by Jain, super class means the 'parent class'. So, that line you refer to is used to call the default constructor of the parent (from child, which is CreditCard).
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:that line you refer to is used to call the default constructor of the parent

<pedantic>
Actually it's used to call the no-args constructor of the parent - this might be the default constructor (the no-args constructor added by the compiler, if there are no explicit constructors) but it could also be an explicitly added no-args constructor.
</pedantic>
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:In this particular case, you don't really need to give it explicitly...

@Steve: But I strongly suggest that you do. ALWAYS. Indeed, many IDEs will include it when they generate constructors themelves.

Failure to do so can result in errors because the compiler added the wrong constructor, so you get an error for code that you didn't write, which can be very confusing.

HIH

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Failure to do so can result in errors because the compiler added the wrong constructor, so you get an error for code that you didn't write, which can be very confusing.

I don't understand what would explicitly adding super() call gain us. Default constructor added by the compiler is always a 0-arg constructor. And it always chains to a 0-arg super class constructor only. May be I'm not completely understanding your comment.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:I don't understand what would explicitly adding super() call gain us. Default constructor added by the compiler is always a 0-arg constructor. And it always chains to a 0-arg super class constructor only. May be I'm not completely understanding your comment.

I think so. What I'm saying is a 0-arg call is NOT always what you want, but it IS what will be added if you don't explicitly put one in yourself. Therefore, you can get a compiler error for something that you didn't write - or even worse, have the wrong constructor called with no error because you left it up to the compiler.

There are a few things like this in Java. For example, I always (or almost always; I have been known to forget ) add:
extends Object
to every applicable class definition I write, simply as a reminder that, yes, this class is actually a direct subclass of Object. It might seem trivial, but it's also easily forgotten.

And don't get me started on the "package private" default...

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
I think so. What I'm saying is a 0-arg call is NOT always what you want, but it IS what will be added if you don't explicitly put one in yourself. Therefore, you can get a compiler error for something that you didn't write.

Yeah, exactly. Just to make it more clear, if we want to chain a parameterized constructor (may be because we haven't added a 0-arg one, or because we want it to initialize fields in super class), then we have to give it explicitly. But if we don't have a 0-arg constructor in super class, then not adding any super() will result in compiler error, as the compiler always chains to 0-arg constructor.

Winston Gutkowski wrote:It might seem trivial, but it's also easily forgotten.

In fact, I've seen this issue with many programmers that are new to Java. More common question is - "If we extend a class, and also a class by default extends from Object, isn't this a multiple inheritance in action?" . There are in fact few other things in Java that can make a programmer go crazy.

Winston Gutkowski wrote:And don't get me started on the "package private" default...

ROFL
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:More common question is - "If we extend a class, and also a class by default extends from Object, isn't this a multiple inheritance in action?" .

But the answer to that one is simple: NO.

All you are saying by adding an extends clause is that this class does not extend Object directly. However, I take your point.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Therefore, you can get a compiler error for something that you didn't write - or even worse, have the wrong constructor called with no error because you left it up to the compiler.

One thing occurred to me about this stuff, that actually works against my own arguments (typical ):

I think it would be trivial to produce a compiler warning in the absence of an explicit superclass constructor call (and possibly, a related Annotation). Unfortunately, IDEs that add their own would kind of defeat this because they might add the wrong one.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!