aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor and Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Constructor and Inheritance" Watch "Constructor and Inheritance" New topic
Author

Constructor and Inheritance

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I though constructor are never inherited!!, According to me the answer should be 1), but again there is the limitation of keeping super on the first line.
Please help!!!
Read the following piece of code carefully.

Assume that the defination of Question72E begins with the line
public class Question72E extends Question72
It is required that none of the constructors of Question72E should throw any checked exception.

1) It can be achived by placing the call to the superclass with a super keyword , which is placed in a try block with a catch block to handle the IOException thrown by the super class.
2) It can be achived by avoiding explicit calls to the base class constructor.
3) It cannot be done in the Java Laungage with the above definition of the base class.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Amish
Answer one is false because if the first line of a constructor is not this(xxx) or super(xxx) the compiler puts in a call to the default superclass constructor. So you can't call the super class constructor from within a try-catch block.
Answer 2 wont work because even if you dont explicitly call the base class constructor the call is still supplied by the compiler.
Given the requirment that you can't throw an exception from the new class' constructor and if you can't change the base class then it can't be done.
hope that helps
[ January 14, 2002: Message edited by: Dave Vick ]

Dave
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Maybe helpful Amish:
Constructors are not inherited. But because they are chained together along the hierarquy up to Object, constructing an object of subclass will always throw the exceptions that the superclass throws because the base constructor will be called. That is why a constructor in the derived class must declare all the checked exceptions that the base class declare. Though, it can declare more, the ones that the construction of the proper derived object might throw.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Constructor and Inheritance
 
Similar Threads
Abhilash Quiz
Re:constructors throwing exceptions
Constructors??
Exceptions
from abhilash - constructors