Inheriting an Interface Implementation in Java

I have two questions regarding interfaces in Java. 1) If a class implements all the interface methods of interface I without declaring itself as their implementation, can it still be used as an input to variables of type I? 2) A subclass of class A that implements interface I inherits conformance to this interface or should it declare itself as implementing I?

+3
source share
3 answers

If a class implements all the interface methods of interface I without declaring itself to be implemented, is it used as an input to variables of type I?

No. What you are describing is more like duck print .

A, , I?

, :

public class A implements I { /* ... */ }

public class B extends A { /* ... */ }

B I.

+16
  • " ?", "".
  • B .

- - .

+2
  • , .
  • , , , .
0

Source: https://habr.com/ru/post/1726548/


All Articles