aspose file tools*
The moose likes Certification Results and the fly likes Method overriding from multiple interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Certification Results
Bookmark "Method overriding from multiple interfaces" Watch "Method overriding from multiple interfaces" New topic
Author

Method overriding from multiple interfaces

Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Hi All,

Can someone tell me how to override a method which is in more than one interface and throws different exceptions . How to implement this method in implementor class?

Example:

import java.io.FileNotFoundException;
import java.io.IOException;

interface Parent {
void myMethod(int x) throws IOException;
}

interface Parent2 {
void myMethod(int x) throws ClassCastException;
}

public class ObjectTest implements Parent, Parent2 {

public void myMethod(int x) throws IOException{
System.out.println("Child");
}

public void myMethod(int x) throws ClassCastException{
System.out.println("Child");
}

public static void main(String args[]) {
Parent ot = new ObjectTest();
try {
ot.myMethod(2);
} catch (IOException e) {

e.printStackTrace();
}
}


The above code gives error at both method implementation.

If I implement method once and add both throws, still it gives error.

What should be done to implement such methods?

Thanks,
Vivian
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
This works:

A subclass should be able to be used in anywhere its superclass can be used. (See "Liskov Substitution Principle"). In this case any code that uses a Parent2 reference expects to be able to call myMethod(int) without having to handle an IOException. Your subclass method can't be more restrictive than the method it is overriding. It can however be less restrictive and throw fewer checked Exceptions than the method that it is overriding.
[ July 02, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Vivian Josh
Ranch Hand

Joined: Oct 31, 2006
Posts: 112
Thanks Garrett !

Does this mean we can not use throws clause with implementor class where same method is used in multiple interfaces with different exceptions?

What if developer wants to use more restrictive checked exceptions on the method?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Does this mean we can not use throws clause with implementor class where same method is used in multiple interfaces with different exceptions?
Thats correct.

What if developer wants to use more restrictive checked exceptions on the method?
That is simply not allowed in Java.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
sliding back to scjp


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method overriding from multiple interfaces