Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Constructors are not inherited.

 
Greenhorn
Posts: 14
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

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



 
srini arumugam
Greenhorn
Posts: 14
  • 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.
 
Marshal
Posts: 69740
277
  • Likes 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.
 
Sheriff
Posts: 21971
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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
  • 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 Spring Linux
  • Likes 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
Marshal
Posts: 69740
277
  • 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
Posts: 21971
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
50. Learn to count!
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Likes 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
 
Always look on the bright side of life. At least this ad is really tiny:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic