GeeCON Prague 2014*
The moose likes Java in General and the fly likes Constructors are not inherited. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructors are not inherited." Watch "Constructors are not inherited." New topic
Author

Constructors are not inherited.

srini arumugam
Greenhorn

Joined: Nov 02, 2009
Posts: 14
Constructors are not inherited.
First line of a constructor is the call to its superclass constructor.
Eg:
Let's consider class "A" which does not have any default constructor.


Consider another class named "B" which extends "A",



At this point we will get a compilation error asking us to add constructor in Class "B". "Implicit super constructor A() is undefined for default constructor. Must define an explicit constructor".

The reason we get this message is , Class "B" has only one constructor, i.e. the default constructor supplied by the compiler. Like below,



Because Class "A" does not have empty constructor, the call inside Class "B" default constructor is not correct.
That is why we get a message to create constructor explicitly in class "B".

We can solve this issue in two ways,
1) Add default constructor in Class "A". So we can instantiate Class "B" only like,

2) Add either one parameterized constructor in Class "B" or both
i.e.


So we can instantiate Class "B" only like,

Because Class "B" does not have any default constructor, we cannot instantiate Class B like,

Please let me know if my understanding is correct. Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14196
    
  20

Welcome to the Ranch!

Completely correct


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
srini arumugam
Greenhorn

Joined: Nov 02, 2009
Posts: 14
Thank you . .
Chris Zaremba
Ranch Hand

Joined: Nov 22, 2010
Posts: 54

You could do this though.




SCJA, OCPJP, OCMJD
srini arumugam
Greenhorn

Joined: Nov 02, 2009
Posts: 14
To create object we need constructor, In constructor first line sould be call to any super class constructor.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
... unless there is an accessible no-arguments (including default) superclass constructor, in which case the compiler will implicitly add super(); as the first line of the constructor.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

And if you omit that line, the compiler adds a call to super() for you.

Edit: congrats Campbell, you beat me!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
srini arumugam
Greenhorn

Joined: Nov 02, 2009
Posts: 14
That makes the statement complete and perfect , , Thanks Campbell.
Chris Zaremba
Ranch Hand

Joined: Nov 22, 2010
Posts: 54

One more point. A constructor could also call another constructor in the same class instead of a super constructor, but the last called constructor in the class (B) must have a call (implicitly or explicitly) to a super constructor.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
Rob Spoor wrote:. . .. Edit: congrats Campbell, you beat me!
By 40 seconds thank you.

It's usually Rob who beats me to a reply.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

50. Learn to count!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
50 seconds?? That takes the fun out of it. That's nearly a whole minute
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructors are not inherited.