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

Why I can't call the parent class constructor?

 
arun mahajan
Ranch Hand
Posts: 305
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun, in your code, you are calling a B(String s) constructor, which you have not defined.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are not inherited.
 
arun mahajan
Ranch Hand
Posts: 305
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, for example:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic