3

So I have a class 'MyClass' & two interfaces 'A' & 'B'

public class MyClass implements A, B {

    @Override
    public void mymethod(){
        System.out.println("hello world");
    }

}

...

public interface A {
    void mymethod();
}

...

public interface B {
    void mymethod();
}

What method from what interface here is being overridden?

I'm thinking both are?

Thanks (student)

Kevvvvyp
  • 1,704
  • 2
  • 18
  • 38
  • I suppose in this situation it doesn't really matter which one, is there a situation where is *would* matter? – Kevvvvyp Nov 26 '14 at 13:39
  • possible duplicate of [Java Multiple Inheritance](http://stackoverflow.com/questions/21824402/java-multiple-inheritance) – Tom Nov 26 '14 at 13:41
  • possible duplicate of [Implementing multiple interfaces having same method](http://stackoverflow.com/questions/17484325/implementing-multiple-interfaces-having-same-method) – nobalG Nov 26 '14 at 13:41
  • @KevinPaton: you are correct. – Azodious Nov 26 '14 at 13:44
  • @KevinPaton yes there is a situation where it would matter if the methods have different signatures... – brso05 Nov 26 '14 at 13:45

4 Answers4

2

In this case it will be like there is only one method. It will override "both" methods in this case because they have the exact same signatures. If the methods had different signatures (return type or something) then there would be a problem. Check out this answer here

For Example:

public class MyClass implements A, B {

    @Override
    public void mymethod(){
        System.out.println("hello world");
    }

}
...

public interface A {
    void mymethod();
}
...

public interface B {
    String mymethod();
}

This would cause a problem...

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40
1

It would be both since it conforms to both interfaces.

Zach
  • 315
  • 6
  • 15
1
((A)(new MyClass())).mymethod();
((B)(new MyClass())).mymethod();

outputs

hello world
hello world

It's straightforward to test what can happen.

If you encounter this then there's potentially some common feature between A and B that could be abstracted to another interface that A and B both extend, or there's undesirable shared terminology between them causing the overlap and something might need renamed.

James
  • 121
  • 1
  • 8
0

An interface is just a contract.

Implementing A means MyClass should have a method void mymethod().

Implementing B means MyClass should have a method void mymethod().

It's overriding "both", but I am not sure this is a proper way to say it.

It's just about filling a contract and making sure MyClass has the proper method.

jchampemont
  • 2,673
  • 16
  • 19