• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Interface

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is for Questions 81 and 82
//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");
}
}
Question 81.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .
--------------------------------------------------------------------------------
Question 82.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
Will cause no compile time error and print "I will never throw and Exception the screen".
Will Cause a run time error .

The answer for Question 82 is: "Will cause no compile time error and print "I will never throw and Exception the screen".
The answer for Question 81 is: "Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String)) "
I agree with the answer of Question 82. I do not understand why does the code in Question 81 throw an error[I thought it will compile].
Any comments will be appreciated.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In question 81, a reference of type AnInterface is declared and polymorphically assigned an implementer class instance. The implementer happens not to throw an exception in the overridden method signature, but another implementer of AnInterface may decide to throw a checked exception. Such an object will still be assignable to the reference of AnInterface type, but everything gets resolved at runtime. The guarded regions are needed for methods calls in polymorphic cases like this.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonali,


Answer for Question 81 is: "Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String)) "


This is because the reference type of the object is AnInterface
AnInterface ai = new AnInterfaceImpl();
Thus at compile time methodOne() of the interface is seen, this method throws an Exception and thus the compiler gives the error.
Look at the modified code below:

Now the program will run just fine, because the Exception has been caught.
I hope this helps!!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of Exception mechanism is to notify user(end-user) of class (interface of method) that there are special situations or conditions he or she must at least know about when using such piece of code.
If you declare Exception and then don't throw it for now (but still planning to throw it in the future), the users of your code can already use it for now, and don't make any changes in the future, when you will finally change your IMPLEMENTATION (not interface = contract) so it will throw the Exception.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the question 81..
the code will not compile until u put try catch block because at compile time the method is being called by using interface type, so compiler expects a try catch block..
for 82..
everthing is just fine because method is being called by the class type....
cheers...
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic