Friday, 24 July 2009

Java Certification Question 0043

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 code executes from the current class up the hierarchy to the ancestor class



Answer 2: constructors cannot be overriden

Overloading constructors is a key technique to allow multiple ways of initialising classes. By definition constructors have no return values so option 3 makes no sense. Option 4 is the inverse of what happens as constructor code will execute starting 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.


No comments:

Post a Comment