This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes subclass constructor exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "subclass constructor exceptions" Watch "subclass constructor exceptions" New topic
Author

subclass constructor exceptions

Ol Holmes
Greenhorn

Joined: Sep 10, 2003
Posts: 1
Hi,
My understanding of this topic is as follows..
If the ParentClass constructor throws an exception, the ChildClass constructor must also throw that exception (or a super class of that exception) if a call to super is made in the constructor.
Is this correct?
Many thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,
Welcome to JavaRanch!
Yes. Note that the call to the superclass constructor can be implicit -- i.e., if you don't explicitly call a constructor, the compiler inserts a call to super() .
[ September 10, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
Here's one way to think about the implications:
If your superclass constructor declares an exception, your subclass constructor must also declare that exception.
What does this actually mean? Several things...
1) You MUST write a constructor in your class! You can't just allow for the compiler to insert the default constructor, because you must have a place to declare the exception.
class Foo {
Foo() throws FooException { }
}
class Bar extends Foo {
Bar() throws FooException { } // we MUST have this
}
In this example, even though we don't *need* a constructor in Bar for any other reason (in other words, we don't need any code in our constructor) we must still put in the constructor just so that we have a place to declare the exception!
2) Constructors are an EXCEPTION to the "Handle or Declare" law
(sorry about the overloaded use of the word 'exception' )
With constructors, there is no option to "Handle" -- you can ONLY declare. Because, where would you *put* the try/catch?
class Bar extends Foo {
Bar() {
try {
super();
} catch (FooException fe) { } // will not work!
}
}
You already KNOW why this is not possible... because the first line of every constructor must be either the call to super() or this(), and here you are trying to put the 'try' ahead of the super(). Not possible.
<side-note>
This is not covered in the current exam, but there are several areas in Java where this is an issue. Most notably, if you do RMI, where your Remote class extends UnicastRemoteObject, whose constructor does indeed declare a RemoteException. So your Remote class then must explicitly declare a constructor, so that you have a place to declare the exception declared by your superclass constructor.
</side-note>
cheers
Kathy
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
Here's one way to think about the implications:
If your superclass constructor declares an exception, your subclass constructor must also declare that exception.
What does this actually mean? Several things...
1) You MUST write a constructor in your class! You can't just allow for the compiler to insert the default constructor, because you must have a place to declare the exception.
class Foo {
Foo() throws FooException { }
}
class Bar extends Foo {
Bar() throws FooException { } // we MUST have this
}
In this example, even though we don't *need* a constructor in Bar for any other reason (in other words, we don't need any code in our constructor) we must still put in the constructor just so that we have a place to declare the exception!
2) Constructors are an EXCEPTION to the "Handle or Declare" law
(sorry about the overloaded use of the word 'exception' )
With constructors, there is no option to "Handle" -- you can ONLY declare. Because, where would you *put* the try/catch?
class Bar extends Foo {
Bar() {
try {
super();
} catch (FooException fe) { } // will not work!
}
}
You already KNOW why this is not possible... because the first line of every constructor must be either the call to super() or this(), and here you are trying to put the 'try' ahead of the super(). Not possible.
<side-note>
This is not covered in the current exam, but there are several areas in Java where this is an issue. Most notably, if you do RMI, where your Remote class extends UnicastRemoteObject, whose constructor does indeed declare a RemoteException. So your Remote class then must explicitly declare a constructor, so that you have a place to declare the exception declared by your superclass constructor.
</side-note>
cheers
Kathy
 
 
subject: subclass constructor exceptions