• 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

when initialize an object, how far will the chained super class construtor call go ?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the title, when we instantiate an object of a class by calling its construtor, eventually, it will ends calling its superclass constructor before initialize its own instance members and executing its own construtor body. The question is in the superclass, will the same thing happen? Will the superclass call its own superclass constructor before initializing the instance members and executing the construtor body of its own class? And will this go all the way up until the constructor of the Object class is called, and then start fall down?
Thanks.
Judy
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear judy ! hi!
please write your question with a small code coz there are two things 1) if you call the super() in the chain of the class comstructors .
2) let the compiler call the default class constructor
in the second case in my opinion it goes up to that level of the class which is not made abstract !
but in the first case it all depends on the type of constructor you are calling ( constructor overloaded) so if the last subclass ( last in the class hierarchy) calls a specials kind of constructor then the compiler also looks for the same kind of constroctors in the base classes !
everyone please correct me if i am wrong!
anurag
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anurag,
As I understand, construtors of super class won't be inherited, but will be called. Default constructor will be called to the top of the inheritance chain (Object).
If not specified, the compiler will automatically insert a call to super() at the begining of the constructor of your current class.

Class X doesn't have a default constructor (the compiler doesn't generate one for X since it has the one-arg constructor), and the constructor in Y trying to call super() resulting in an compile error.
"Main.java": Error #: 300 : constructor X() not found in class test4.X at line 13, column 3
Hope this will help.
Hungson Le
P.S. Just to clarify the purpose of calling super: the designers want to make sure variable in the super class in a valid state before any operation of your class takes place.
[This message has been edited by Hungson Le (edited January 31, 2001).]
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea judy! all the way up. if there are these many classes in the hierarchy...
class A{}
class B{}
class C{}
an instantiation of class C will call the constructor of B which will call the constructor for A which in turn will call the constructor of Object.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the way up to Object, with any set of parameters along the way.
Ex. b extends a b () { super (1,2)}
a extends base a(int i, int j) { super ("oh boy")}
base base("String s") { super( "name", "address") } etc. etc.
reply
    Bookmark Topic Watch Topic
  • New Topic