• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
So , It should be 3 . Right ?



No, it's 1 like it says above.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got .
Thanks to all .
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it too....Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic