• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do constructors are really not inheited???

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sun's Inheritance tutorial says
"A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members,
so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. "

and the following code compiles


But if we make the A() constructor private the compiler says it can not find the symbol and
also if both are in different packages then also compiler complains...

By observing all these things it intuitively doesn't appear that constructors are not inherited,
Then why such discrimination with protected ??
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If its private- then its not visible to any other External class. Did you try extending a class in another package. Like with the current code you provided- try having Class B in a separate package.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i have tried keeping constructors in default access level ..
Then also compiler error happened but error is different
" A() is not public in pack1.A; cannot be accessed from outside package"
pack1 is the package name..
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:Yeah i have tried keeping constructors in default access level ..
Then also compiler error happened but error is different
" A() is not public in pack1.A; cannot be accessed from outside package"
pack1 is the package name..



Did you try it with the constructor at 'protected' level?
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,
I have already tried all possible combinations...
My question is simple..
constructors are not inherited and if this statement is true then
why we are able to call super class constructor from child class constructor..
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors are not inherited- Instead you can just invoke the Super class constructors. Take an example:


 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code does not compile...

TestClass.java:12: cannot find symbol
symbol : constructor B(java.lang.String)
location: class B
B myB = new B("My String");
^
1 error
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:The code does not compile...

TestClass.java:12: cannot find symbol
symbol : constructor B(java.lang.String)
location: class B
B myB = new B("My String");
^
1 error



That's is what I have mentioned in the comment I have provided in the code (just below the same line). If the constructors were inherited- it should have worked. But they are not.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If the constructors were inherited- it should have worked. But they are not.


How is it possible, even if the constructors are inherited we are calling B's constructor
why would it invoke A's constructor...

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:

If the constructors were inherited- it should have worked. But they are not.


How is it possible, even if the constructors are inherited we are calling B's constructor
why would it invoke A's constructor...


In the same code I have given an example of how Inheritance actually should work- by using an myPrint method. So considering that- we cannot think of Inheritance for Constructors.

And as you have mentioned above- We do need to provide a overloaded constructor for B and it will not inherit A's constructor. But with the inheritance principle- B should be able to use the constructor in A (that's what the whole point of inheritance is- Sub class being able to make use of super class methods,fields without declaring them again in Subclass).

 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i agree with you...
everything goes right but how we are able to use the super class constructor without
even inheriting it and why don't they call constructors are inherited

It must be an exception ...

Thank you Mohamed for your replies with so much patience...
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor is being invoked just like any other method. And yes- they seem to look like they are inherited, but for something to be inherited the behavior should be as I mentioned in my previous post. So as the constructors dont behave that way- They aren't called as being inherited.
reply
    Bookmark Topic Watch Topic
  • New Topic