Author Topic:   error messages
Eric Barnhill
ranch hand
posted May 14, 2000 09:36 PM         
I was surprised by the error message I got here. Also, it seems of a different order than the error that , I think, Bruce Eckel's TIJ suggests would come here. Here is the code, stripped to bare essentials:


interface inter{
int aMethod();
}
class One{
public void aMethod(){}
}
public class Two extends One implements inter{
public static void main(String[] args){
}
}

The error was:

2: Method redefined with different return type.

I have two little questions about this:

1) why is the error called in the interface and not the public class?
2) is there a good reason why I didn't get the "interface collision" Eckel talks about in TIJ?

Thanks

Eric

Suresh
greenhorn
posted May 14, 2000 11:02 PM             
hi eric,
I compiled your program and I got the error only in the public class Two and not in the interface as you say!!
The error I got was:
[bold]
The method void aMethod() declared in class One cannot override the method of the same signature declared in interface inter. They must have the same return type.
[/bold]
This error points to the class declaration of class Two.
I use JDK1.2.2. And this error is what you expect I feel.

And what is the 'interface collision' eckel is talking about? Can you give us some details?

Regards,
Suresh.

maha anna
bartender
posted May 14, 2000 11:06 PM             
Eric,
The error message will come from class Two and it is reasonable. Because, when you define an interface, and you define a class implementing this interface and when the implementing class is NOT an abstract class, then this means there is somehow you are giving the implementaion for ALL the abstract methods in the interface.

You can give the implemetation of the methods in many ways.

1. The usual way is the implemention class (here class Two) itself can give the implemntation fot the abstract method aMethod() from the interface inter.

2.You can inherit the implementation from its parent class. THis is very much legal. This is what happens in your sample code. The class Two has an inherited method called aMethod() from its parent class One but its return type is NOT 'int' instead it is 'void'

3. The inference is in any class you can't have 2 methods with same name and parameter list (basically signature) and different signatures.

4. So when you say that class Two is extending class One and at the same time implementing inter, the compiler is very much confused. Where in the world are you going to give me a method of 'public int aMethod()' when you already inherited a method from your parent class with different type ? like that.

5. Well, the compiler is not exactly saying the same as Maha's words. Instead it is naive and thinks you are trying to implement the method from interface inter but you have given the WRONG return type like that.

In fact this is possible. A class can give the implementation for a method of the interface it implements by extending a class which happened to implement another totally different interface which happened to have the same method as our first class's implemented interface, and the compiler is happy to accecpt this kind of interface implementation. See the code foll.
This code will compile and run with the output of 'Hello Eric'
regds
maha anna



interface inter{
int aMethod();
}
class One{
public void aMethod(){}
}
/*public class Two extends One implements inter{
public static void main(String[] args){
}
}*/

public class Two extends Another implements inter{
public static void main(String[] args){
new Two().aMethod();
}
}

interface inter1 {
int aMethod();

}
class Another implements inter1 {
public int aMethod() {
System.out.println("Hello Eric");
return 1;
}
}


[This message has been edited by maha anna (edited May 14, 2000).]

Eric Barnhill
ranch hand
posted May 15, 2000 09:22 PM         
Suresh,

yes, that's more like the error I was expecting. I was surprised to get mine! I was using JDK1.1.7A at work on NT. I tried it again elsewhere in the Code Warrior IDE and got exactly the error you got. So that must be a quirk of that JDK.

Bruce Eckel cites an error code that uses the term "interface collision". I'll paste the code here:


The difficulty occurs because overriding, implementation, and
overloading get unpleasantly mixed together, and overloaded functions
cannot differ only by return type. When the last two lines are
uncommented, the error messages say it all:

InterfaceCollision.java:23: f() in C cannot
implement f() in I1; attempting to use
incompatible return type
found : int
required: void
InterfaceCollision.java:24: interfaces I3 and I1 are
incompatible; both define f
(), but with different return type

If anyone knows why I would get one error and not the other, i'd be curious to know - I may be missing a concept here.

Thanks,
Eric

|