-1

Consider following scenario: I have two interfaces A and B. Both interfaces have a member function display().

public interface A {
   public function display() {
   }
}

public interface B {
   public function display() {
   }
}

class C implement A, B {
   public function display{
       //definition here
   }
}

I just want to know

  1. how many display() functions are available in class C?
  2. If there is one member function, how it is possible?
5
  • 2
    Please provide a proper example (since when do interfaces have method bodies when it isn't a default method). Commented Mar 28, 2017 at 10:55
  • 1
    Is this not Homework? Commented Mar 28, 2017 at 10:56
  • Is it an urgent question I assume? Commented Mar 28, 2017 at 10:57
  • This is not a homework. I m PHP developer, learning Java Commented Mar 28, 2017 at 10:58
  • 1
    There are several compiler errors: 1. if the method does not return any value, replace "function" by "void". 2. there is no body in the interface-method: public void display(); 3. in your class the () are missing Commented Mar 28, 2017 at 10:58

2 Answers 2

3

A brilliant explanation is at: Implementing two interfaces in a class with same method. Which interface method is overridden?

If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error. This is the general rule of inheritance, method overriding, hiding, and declarations, and applies also to possible conflicts not only between 2 inherited interface methods, but also an interface and a super class method, or even just conflicts due to type erasure of generics.

Sign up to request clarification or add additional context in comments.

Comments

1

how many display() functions are available in class C?

Just only one

If there is one member function, how it is possible?

Because they have the same signature

But this is forbidden in java, it is not possible with the same name and different types, you can learn more about that here Java - Method name collision in interface implementation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.