• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

a extends b , which constructor is called in a?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Sun Certified Programmer For Java 6 Study Guide on page 205;



new House("x "); sends a String argument to the House Constructor,
but, as is the way of classes that are extended, the superclass constructor has to fire before the subclasses constructor will fire, my question is;

Is the default constructor always called when extends is used?
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chadd Franck:
Is the default constructor always called when extends is used?


If you don't explicitly call other super constructors in the child class constructors, then yes the default constructor will be call.

If the super class doesn't have an accessible default constructor, you will have to explicitly call another constructor in the child class constructor. I.e.

[ December 10, 2008: Message edited by: Duc Vo ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chadd Franck:


Is the default constructor always called when extends is used?



Yes, unless:

1) you explicitly call another constructor via a call to super that references a constructor other than the default constructor.
2) a default constructor does not exist (because you wrote a constructor that takes an argument and did not also write a no-args constructor. But in this case you must call a super constructor in your code or you won;t compile. See code below.



Here the Building's default constructor is not called because it does not exist. But we must specify a super in the House's constructors or House will not compile.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the default constructor of the super class will always get called when you explicitly called any sub class constructor..Even by default when you create an object for the subclass the super class empty constructor will get called.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is the superclass constructor always called when the sub-class instance is created?
This is because the instance of the subclass may depend on some of the values from the superclass which the subclass inherits.
Hence whenever the subclass instance is created the superclass constructor runs and completes before the subclass constructor's evaluation.



this makes the default constructor of the class itself to be called.
A call to super explicitly calls the superclass constructor,with/without arguments as the super call.

Hope this helps.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, great many ways of seeing it. So in my own words, no matter what parameter is being passed to the constructor of the subclass, the parameter will not be passed along to the superClass constructor unless there is a call to the super(parameter). Therefor, if no super is written in the constructor of the subclass and no this, the default constructor for the superClass will be called to complete the inheritance tree. (assuming that a suitable superClass constructor is found).

So that must mean that the subclass constructor fires checks the first line of code for Super() or This(), if is sees neither, it fires the default constructor (if there is one) of the superClass?
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are correct.
And if you have a constructor with arguments in the super class and no default constructor then make sure you make the appropriate call to the superclass constructor.
If you have a parameterized constructor in the superclass then the complier would not insert a default constructor which it would do in case you have not written any constructor at all in the super class.

And for the rest of it you have got it right.

Hope this helps.
 
Marshal
Posts: 75857
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SJCP related question. Moving.
 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The K&B book also shows a funny example where two constructors call each other. In that case no constructor of the super class will be called when you create an instance of the sub-class



When you instantiate Sub class, you will get a StackOverflowError...
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In 'a class'(House class) parametrize constructor will call First.

in that you are using this()."this" key word represents current class objects,constructors,instance variables.so automatically 'a class'(house class)class default constructor called.But first Base class(building) constructor called,then derived class default constructor.after that remaining code in parametrized constructor of 'a class' will execute.

note:super class default constructor is always available to subclass but parametrized one not available,to make it available you need use super keyword.so default constructor will always called until you use super keyword.
i think you got the concept
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah thats obvious because the control is switching between only constructors.Unless a constructor or method doesnt return, for each call to a constructor or a method ,memory continues to be allocated till stack runs out of memory
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic