Hi, Question 43) Which of the following statements are true 1) constructors cannot be overloaded 2) constructors cannot be overridden 3) a constructor can return a primitive or an object reference 4) constructor invocation occurs from the current class up the hierarchy to the ancestor class Answer to Question 43) 2) constructors cannot be overriden _________________________________________________________________ The explanation to why choice 4 is wrong is Option 4 is the inverse of what happens as constructors are called from the oldest ancestor class downwards. You can test this by writing a class that inherits from a base class and getting the constructor to print out a message. When you create the child class you will see the order of constructor calling. _________________________________________________________________ My thought was the child class constructor calls the superclass constructor , superclass constructor in turn calls the sperclass constructor and so on . This is done since super() [implicitly by compiler or explicitly by programmer] or super( parameters )[explicitly by programmer] is called. So I think the constructors are called from child to parent that is from current class to ancesstor class. [This happens Exactly like method calls.] So i think choice 4 is also correct . Please somebody give suggestions. Tinu
Hi RKB, as u know that the at the begining of the constructor a call to super class constructor is made. which means that first of all the higher level constructors are invoked and executed only after that the body of a constructor is executed. so from this perspective 4) is wrong.
I think the wording option 4 is a bit vague, but I agree with the outcome. The child class ctors get a chance to specify which version of the parent ctor gets called (if there are overloaded versions), but the real work of the child ctor (i.e., of the rest of its body) won't be run until the all of its parents are constructed. So, there is an initial stop-off at the child ctor, but that's about it. IMHO, I don't like how Java handles this. It seems strange that you put the call to a parent-class ctor (or to an overloaded sibling ctor) in the ctor body, itself. I know that in C++, you have an intialization list (placed where you'd put a "throws" clause) from which you can make a call to the parent ctor, and I think that is a cleaner, more elegant way to make your explicit ctor calls clearly distinct from the rest of the ctor code in the child class.
Joined: Aug 14, 2000
Originally posted by DaB: I think the wording option 4 is a bit vague, but I agree with the outcome.
I agree with u. The option 4 is ture since the coding is 4) constructor invocation occurs from the current class up the hierarchy to the ancestor class The option 4 is wrong if the coding is 4) constructor execution is completedoccurs from the current class up the hierarchy to the ancestor class Correct me if I am wrong, Tinu [This message has been edited by RKB (edited September 01, 2000).] [This message has been edited by RKB (edited September 01, 2000).] [This message has been edited by RKB (edited September 01, 2000).] [This message has been edited by RKB (edited September 01, 2000).]
Joined: Nov 22, 2008
RKB, Well, I suppose I could go either way with the original wording, but I think your second version makes the option true! The underlying idea here is that an object is actually built from the top down. If you think about it a bit, it makes sense to construct objects that way: consider an inherited method and where it comes from (hint: a parent), and think about the dangers of calling an overridden method in a constructor... And, objects in Java are probably destroyed in the opposite order (i.e., bottom-up), but I have to check on that. Don [This message has been edited by DaB (edited September 01, 2000).]
Joined: Aug 14, 2000
Hi DaB, I made a mistake and I corrected the statement. Tinu
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com