Ireneusz Kordal wrote:Hi,
if you define a method inside interface, this method is always implicitly public.
In the class implementing this interface you must always declare this method as public,
otherwise compiler complains that this method is not implemented.
Ireneusz Kordal wrote:Hi,
And the other compiler errors is not because of implementing interfaces, but due to
rules of overloading method (overloading = declare two/more methods with the same name in the class).
If you overload method, methods with the same name have to have different signatures (different parameters list).
You cannot declare inside class two methods with the same name and the same parameter list, compiler will always complain.
is there a way to implement these two interfaces simultaneously?
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara wrote:
As pointed out earlier...
Marco Ehrentreich wrote:Hi Aakash,
if you still would like to "implement" the two interfaces simultaneously you could use a workaround.
Have two other classes implementing each one of the interfaces and put objects of these classes as members inside an object of your original class. The you can hide the members as private and declare two public methods (which you have to give different names of course) and these public methods can delegate to the member object's methods. Alternatively you could implement the interfaces directly as a member variable with anonymous classes.
That way you avoid the name clash by implementing the interfaces in two different classes. But to the outside the functionality appears in only one class which delegates internally to the said classes.
Marco
Have two other classes implementing each one of the interfaces and put objects of these classes as members inside an object of your original class. The you can hide the members as private and declare two public methods (which you have to give different names of course) and these public methods can delegate to the member object's methods.
Aakash Goel wrote:
Another scenario:
implementation1:
Compiler says: A.a() not defined.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Marco Ehrentreich wrote:Hi Ankit,
sorry for confusing anybody here, of course you're right. I should have been more careful with my examples. ........
But the the basic idea behind my examples how to solve such a naming conflict still should work to more or less elegantly implement multiple interfaces with conflicting methods.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:
Are you sure about this. There's no error in this case as both the a methods in the two interfaces have the same signature and return type so the overriding a in class clash will override the a method in both A and B...
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Well I tried it and I didn't get any error and neither should have you got any error...
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |