This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why I can't call the parent class constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why I can Watch "Why I can New topic
Author

Why I can't call the parent class constructor?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
I need to fix my baisics, pl hlp me.
Ihave two classes as given below. and not able to compile class B, though I am assuming that when a class extends other it also gets all the constructor of its parent class. Am I wrong?
If so than why it compiles and call the parent class default constructor, if I write B x = new B() instead?
pl help me to understand this.
regards,
Arun


[ September 18, 2002: Message edited by: arun mahajan ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Arun, in your code, you are calling a B(String s) constructor, which you have not defined.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Constructors are not inherited.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks for your replies.
Yes I am not defining the B(String s) assuming that it will call the parent constructor and Thomas if constructors are not inherited than why it calls the parent default constructor. What I mean if I say B x = new B() it output the print value of class A's default constructor. What's the OOPs concept here? Is it say that it will only go for the default one nothing more
regards,
Arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The first thing every constructor must do is either invoke another constructor of the same class or invoke a super constructor. An instance of the superclass must be created before an instance of the subclass is created. (Nevermind about what the first thing that a constructor of Object does).
If the programmer does not include an explicit invocation of a super constructor, then the compiler adds that line of code for you (so to speak). The constructor that the compiler chooses by default is the default (no arguments) constructor.
Does that clear things up a bit?
[ September 19, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Try running this bit of code and see if it starts to make sense:

[ September 19, 2002: Message edited by: Thomas Paul ]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Thomas for your reply. What I could understand now from yours and Dirk answer is that constrcutors can not be inherited( as you said earlier). As a matter of fact I tried modifying the code in following manners and answers were surprising to me but perhaps it make this sense only:
1. If I want to call any parameterised constructor of a super class I should use super method for that.
2.Otherwise it calls the default only.
Am I right?
regards,
Arun
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
You are right, for example:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why I can't call the parent class constructor?
 
Similar Threads
super().this()!
default constructor must inside the Parent class. why?
Code confusion
Constructors
Inheritance