The Artima Developer Community
Sponsored Link

Java Answers Forum
super man! Always be the first statements.

4 replies on 1 page. Most recent reply: Jun 26, 2007 12:41 AM by Reza Fathzadeh

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Reza Fathzadeh

Posts: 4
Nickname: rfathzadeh
Registered: May, 2007

super man! Always be the first statements. Posted: Jun 24, 2007 6:01 AM
Reply to this message Reply
Advertisement
Hi guys,

why must super( ) always be the first statement executed inside a subclass' constructor?

Is it because in a multiple class hierachy, the order in which constructors are executed is as the derivation order? (i.e. Class -> Subclass -> SubSubclass -> ...)

Thanks
Reza


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: super man! Always be the first statements. Posted: Jun 24, 2007 2:47 PM
Reply to this message Reply
As you may be aware, when a class has no constructor then an "implicit" constructor is inserted into the class when it is compiled (which takes no arguments and is empty (well almost empty - see below).

Similarly when the first line of a constructor does not explicitly contain a call to another constructor, either of the parent class or within the same class, then an implicit "no arguments" call to the constructor of the parent class is also inserted.

The purpose of all this is to ensure that all the internal components of an object are constructed in the correct order (from the most general high level object in the object's hierarcy to the most specific). This is necessary because it is possible for a class to reference components in the parent hierarchy during its construction, therefore those components need to be created first.

If the call to a super constructor (explicit or implicit, directly or via another constructor in the same class) was not the first thing that happened in the constructor then it would not be possible to guarantee the existance of the parent object for use by whatever else might otherwise be the first line of the construtor.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: super man! Always be the first statements. Posted: Jun 24, 2007 11:33 PM
Reply to this message Reply
But there are some dirty ways to trick the compiler.

If the super-constructor expects a variable, you can just call a static method which returns a variable of the given type.

Also, if you assign a value to class variables where they are declared, it gets set before the constructor is called.
You can call static methods here.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: super man! Always be the first statements. Posted: Jun 25, 2007 6:10 AM
Reply to this message Reply
True. Static components of a class do not require an instance of that class to exist before they can be called, so there are doubless ways they be manipulated subvert the compiler. Not sure it's the sort of thing a student wants to complicate their tutorial answer with though.

Reza Fathzadeh

Posts: 4
Nickname: rfathzadeh
Registered: May, 2007

Re: super man! Always be the first statements. Posted: Jun 26, 2007 12:41 AM
Reply to this message Reply
thanks friends!
Points you mentioned were really subtle.

Flat View: This topic has 4 replies on 1 page
Topic: super man! Always be the first statements. Previous Topic   Next Topic Topic: Java Certified Professional

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use