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:
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? 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,
|
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 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:
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,
|
| | |