Forums Register Login

Doubt in multiple inheritance

+Pie Number of slices to send: Send
hi... take a look at this code.

interface I5 {
final int i = 5;
public void help();
}
interface I6 {
public int help();
}
class D {
D () {
System.out.println("This is D");
}
public void printThis () {
System.out.println("superclass version");
}
}
public class C extends D implements I5, I6{
private int a;
public int help() {
System.out.println("from I6");
return 1;
}
public void help() {
System.out.println("sdkfj");
}
C () {
System.out.println("Tjis is D");
}

public void printThis() {
System.out.println("subclass version");
String x = "192.1.2.3";
System.out.println("hello");
}

public static void main(String args[]) {
new C().help();
}
}

Here i'm getting a compile time error:
Duplicate method help in type C
how do i overcome this problem. if i have 2 methods with same name in 2 interfaces. interfaces not serving the purpose of multiple inheritance.

Please reply soon. it's urgent.

Thanks,
Roopa.
+Pie Number of slices to send: Send
You can't. a method is defined by it's name and parameter list, not it's return type, regardless of interfaces and inheritance. it is perfectly legal to ignore what a method returns. So if in my code, i make this call:

help();

am i calling the version that returns a void, or the version that returns an int, but have decided to not save that int?

you can't tell, and neither can the compiler. thus the error.
+Pie Number of slices to send: Send
I personally never considered Interfaces as a route to multiply inheritance, more a way to extend a class with generic functionality that is not easierly classified to just one type of object.

Perhaps if I6 extended I5 you might be able to get the functionality you require in the help method.

As stated to overload a method the parameter list must be different. Retrun types are not part of a method's signature.
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 643 times.
Similar Threads
difference bet. interfaces and abstrct classes
Integer unboxing and == operator question
(navie) Question on HashMap instanceof Collection
Creation of Ineger Wrapper Object
why does this return false?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:17:43.