• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

I need your help!!!

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//Read the code following and ans the question.

A) will cause a compile time error.(Exception must be caught or thrown by main(String)).
B)will cause a compile time error for Class InInterfaceImpl.The method methodOne() be declared with throws Exception".
C)will cause no cmpile time error and print"I will never throw and Exception the screen".
D)Will cause a run time error.
Given ans:A.

In this case ,we but a instance of AnInterfaceImp(suber-class),the reference type is AnInterface(super-class),I think we when we call the method ,it prefer the instance type,while the variable it prefer the reference type,as a result above ,when we call the ai.methodOne() in main(String arg[]),it will not invoke the methodOne of supper-class.but when I compile ,it give me the error that the main(String arg[]) shoud caught or throws a throws execption statement.
[This message has been edited by Gong James (edited November 05, 2001).]

(Marilyn added code tags)
[This message has been edited by Marilyn deQueiroz (edited November 06, 2001).]
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be C.

I tried it in my environment (SDK 1.3.1 in Windows 95).
What version are you using?
When implementing an interface, the overriding method can not throw more "checked" exceptions than the method
it overrides.
You don't even have to throw any "checked"
exceptions. BTW, the method in your question does not throw a "checked" exception. So,
it is a moot issue anyway.
[This message has been edited by Nain Hwu (edited November 05, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:

//Read the code following and ans the question.
interface AnInterface
{
public void methodOne()throws Exception;
}
class AnInterfaceImp implements AnInterface
{
public void methodOne()throws Exception //This is optional. If you add this here also add this in main() method or enclose the method call in the main() method in a try catch block.
{
System.out.print("I will never throw an exception");
}
}
public class ATest
{
public static void main(String arg[])throws Exception
{
//AnInterface ai=new AnInterfaceImp();]
AnInterfaceImp ai=new AnInterfaceImp();
ai.methodOne();
}
}


The correct answer is C.
Reason: The method in the class implementing the interface is overriding the original definition in the interface. Overriding methods need not throw any exceptions. But if it chooses to throw, it can throw only all or a subset of the exceptions and their subclasses that the original definition specifies.
HTH
Shyam

[This message has been edited by Shyamsundar Gururaj (edited November 06, 2001).]
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shyamsundar,
There is no need to add Exception at main() at all.
See my previous comment.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nain,
I was just trying to show that if at all you decide to include the throws statement, what should be the course of action.
Alternatively, one can enclose method call within a try catch block in the main method. That is if the throws statement is included in the overridden method
You are right too! But I wrote whatever I did based on the code I modified..
HTH
Shyam

[This message has been edited by Shyamsundar Gururaj (edited November 06, 2001).]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important to note, though that if the commented declaration had been used instead:
AnInterface ai=new AnInterfaceImp();
then try-catch or throws statement in main() would have been required by the compiler for the same call to ai.methodOne().
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very True!
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
AnInterface ai=new AnInterfaceImp();
AnInterfaceImp ai=new AnInterfaceImp();
Why there is the difference between these two cases?
I thought that the methdodOne() from AnInterfaceImp will be called always, but it seemd like the "parent" method tries to be executed in the first case.
Why?
Thanks,
..Cristian
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In overriding there is a "chain of command". The cast of the object whose method is called will determine the rules. When an overridden method of an object cast as a parent is called, the binding will have to "go through" the parent method to get to the child method. If that parent method is capable of throwing an exception, the caller must meet those requirements.
In the declaration: Parent p = new Child(), the object of Child is cast as a Parent, so the parent method definitions will determine the calling requirements.
Using: Child c = new Child(), the object of Child is not re-cast. The calling requirements remain as they appear in the Child method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic