• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in multiple inheritance  RSS feed

 
Roopa Prabhu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!