سؤال

Lets say I have two interfaces interface A and interface B:

public interface A {
  public int data();
}

public interface B {
  public char data();
}

interface A has a method public int data() and interface B has a method public char data().

when I implement both interfaces A and B in some class C the compiler gives me an error. Is this a flaw in java? As I presume this is one of the major reasons why we are not allowed to extend more than one class then why are we allowed to implement more than one interface when this problem still persists?

هل كانت مفيدة؟

المحلول

The Java Tutorials: Defining Methods - Overloading Methods states,

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.

also,

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

The two implemented methods share a common method signature (i.e. data()) and as such, the compiler cannot differentiate between the two and will have that single method satisfy both interface contracts.


Edit:

For instance,

public class Foo implements IFoo, IBar{

    public static void main(String[] args) {
        Foo foo = new Foo();
        ((IFoo) foo).print();
        ((IBar) foo).print();
    }

    @Override
    public void print() {
        System.out.println("Hello, World!");
    }
}

public interface IBar {
    void print();
}

public interface IFoo {
    void print();
}

which will output,

Hello, World! 
Hello, World!

نصائح أخرى

The Java compiler (C# also I think) does not differentiate methods by their return value. In your case, both methods are considered the same by the compiler despite the difference in the return type

The problem you presented is nothing related to multiple inheritance. Neither is the class implementation that implements more than one interface.

When you define an interface you are just saying that the implementor of this interface should agree with a contract and implement all the methods defined on that interface.

A class implementation can implements more than one interface without problem, but the interface cannot conflict. In your case you are trying to implement two interfaces that declares a method with the same signature.

A method signature is composed by name and parameters type in java.

Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

In order to overload a method you need to have different signatures.

The multiple inheritance in Java is not allowed, as there is complex problems such as define which implementation should take place when method is implemented by two or more super classes. For this topic I suggest a look into the Diamond Problem

In fact interfaces are used in some situations to simulate multiple inheritance allowing classes to present a merged set of methods.

One solution is to use an inner class:

  1. Your "class C" implements interface "A"

  2. You also have an inner class "D" that implements interface "C":

    public class C implements A { ... private class D implements B {

If you truly need both interfaces in the same class, this might let you to have your cake and eat it, too :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top