• 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

Why I can't call the parent class constructor?

 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun, in your code, you are calling a B(String s) constructor, which you have not defined.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors are not inherited.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, for example:
reply
    Bookmark Topic Watch Topic
  • New Topic