• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt

 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
So , It should be 3 . Right ?


No, it's 1 like it says above.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got .
Thanks to all .
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it too....Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic