Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do constructors are really not inheited??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Do constructors are really not inheited???" Watch "Do constructors are really not inheited???" New topic
Author

Do constructors are really not inheited???

Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

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 ??
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 09, 2011
Posts: 173

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Constructors are not inherited- Instead you can just invoke the Super class constructors. Take an example:


Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 09, 2011
Posts: 173

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 09, 2011
Posts: 173

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do constructors are really not inheited???