File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors are not inherited.

 
srini arumugam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Completely correct
 
srini arumugam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you . .
 
Chris Zaremba
Ranch Hand
Posts: 54
Eclipse IDE Linux Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do this though.



 
srini arumugam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create object we need constructor, In constructor first line sould be call to any super class constructor.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you omit that line, the compiler adds a call to super() for you.

Edit: congrats Campbell, you beat me!
 
srini arumugam
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes the statement complete and perfect , , Thanks Campbell.
 
Chris Zaremba
Ranch Hand
Posts: 54
Eclipse IDE Linux Spring
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
50. Learn to count!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
50 seconds?? That takes the fun out of it. That's nearly a whole minute
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic