No.
An interface defines how a class should look like (as a bare minimum). Whether you implement this in a base class or in the lowest subclass doesn't matter.
The interface has to be entirely implemented throughout the hierarchy of sub classes and base class and has to be defined at the level where the interface implementation signature is located (implements Interface
).
The sub classes themselves have no knowledge about the interface, but they have the implicit connection trough their base class.
Because I'm a kind person:
public class Test {
public static void main(String[] args) {
BaseClass obj = new SubClass();
obj.test();
obj.test2();
SomeInterface obj2 = new SubClass();
obj2.test();
obj2.test2();
}
}
interface SomeInterface {
public void test();
public void test2();
}
abstract class BaseClass implements SomeInterface {
@Override
public abstract void test();
@Override
public void test2() {
try {
System.out.println(Arrays.toString(this.getClass().getMethod("test2", null).getDeclaringClass().getInterfaces()));
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SubClass extends BaseClass {
@Override
public void test() {
try {
System.out.println(Arrays.toString(this.getClass().getMethod("test", null).getDeclaringClass().getInterfaces()));
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output:
[]
[interface SomeInterface]
[]
[interface SomeInterface]
As you can see it shows that test()
(which is implemented in SubClass
) does not return any interfaces implemented, while test2()
(which is implemented in BaseClass
) does show that an interface is implemented by that class. Abstract classes can allow to implement the methods from an interface they implement to be implemented in sub classes by marking the definition as abstract
.
And as you can see in the main
method, both defining the object as BaseClass
or SomeInterface
works and makes no difference.