Need some information regarding the work of a constructur .
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Suvendu_Mishra
Posted On:   Sunday, June 3, 2001 04:15 AM

Why a constructor first statement must be a call to another constructor or a call to super class constructor ? Why not we call a constructor after the first statement ?

Re: Need some information regarding the work of a constructur .

Posted By:   Michael_Herrmann  
Posted On:   Wednesday, June 6, 2001 02:28 AM

I always thought that this behaviour has a more technical reason: Java always calls the constructor of the super-class to reserve the memory needed for all the fields of the instance. Of course this has to be done before any instructions can make changes to the fields of the instance. So if you don't make an explicit call to the constructor of the super-class in the first line, Java makes a call of the standard-constructor to reserve the memory for the fields declared in the super-class.

Re: Need some information regarding the work of a constructur .

Posted By:   Christopher_Schultz  
Posted On:   Tuesday, June 5, 2001 11:10 AM

This is kind of a cop-out answer, but it's that way because "those are the rules."



No, really, though. Java is a very rabidly Object-Oriented, dogmatic language. The designers wanted to be as perversely object-oriented as possible. One was to ensure this is to create a compiler- (and VM-) enforced rule that classes should always call their superclass constructors.



This is because in the OO world, where subclass relationships are sometimes called 'is-a' relationships, your class should not just 'be-a' superclass, but also 'act-like-a' superclass. The notion is that if the superclass constructor isn't called, then there's a pretty good chance that the superclass functionality of your derived class will fall apart, thus breaking the OO paradigm.

Hope that helps,

-chris
About | Sitemap | Contact