This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt" Watch "Doubt" New topic
Author

Doubt

Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
In the following code :


//Contents of File AnInterface.java

public interface AnInterface
{
public void methodOne() throws Exception;
}


//Contents of File AnInterfaceImpl.java

public class AnInterfaceImpl implements AnInterface
{
public void methodOne()
{
System.out.println("I will never throw an exception");
}
}



public class ATest
{
public static void main(String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
}


Attempting to compile and run the above code

1. Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
2. Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
3. Will cause no compile time error and print "I will never throw and Exception the screen".
4. Will Cause a run time error .


The answer is 1.....Why not 2 ?

Thanks
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
When u are overriding, the child (sub class)method can throw zero or more exception (or sub class of exception) of the superclass method. It is not mandatory to throw the exception.
Hope u got the it


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by rathi ji:
So , It should be 3 . Right ?


No, it's 1 like it says above.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Perhaps an explanation will help.

When you invoke a method on a reference type variable, the compiler does not know which object the variable will actually refer to, so it applies the rules to the declaration of the reference type.

In this case, AnInterface was declared with a method methodOne() that throws Exception, so when calling methodOne() on AnInterface you must catch or throw Exception.

While Java does late binding to select among overridden methods with the same signature, it also does extensive checking at compile time based on the method in the declared type. This is why overriding methods cannot throw additional exceptions - the compiler has no way to check them.


Mike Gershman
SCJP 1.4, SCWCD in process
Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
Hi Original Poster,

I am trying to reinforce what Anand has mentioned.
Referring from the book from Kathy & Bert(K&B):
1) The overriding method must not throw new or broader checked exception
2) The overriding method can throw narrower or fewer exceptions (as mentioned by Anand too)

These two concepts attempts to clear the fact that AnInterfaceImpl can override methodOne() from the super class(though here it is an interface).

Now looking at the code in main
ai.methodOne();
Compiler has only information that this method refers to a method declared in AnInterface which throws Exception and it is a checked exception.

It must be caught or thrown and so the correct answer is choice 1).

Please comment...

cheers
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I got .
Thanks to all .
Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51
I got it too....Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Doubt
 
Similar Threads
interface problem
Interface questions
Ques from Abhilash 'ssite
Interface
Interface