Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt in multiple inheritance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic