munchschair
munchschair

Reputation: 1683

Do subclasses inherit interfaces?

Quick question, I'm learning about interfaces and inheritance.

This is not actual code, just an example. Let's say I have the abstract class Animal. There's some inheritance with groups like horses, and canines. There's also an interface "Pets". It's gonna be used on different subclasses of Animal. The subclass of canine "Dog" implements the interface "Pets". Therefore all subclasses of "Dog" also implement the interface "Pet" without having to individually implement "Pets" on each subclass of "Dog", right?

Upvotes: 42

Views: 55808

Answers (3)

haozhe heng
haozhe heng

Reputation: 1

No, but kind of.

Inheritance just make sure whatever is in the superclass is in the subclass.

Interface ensures what methods must be available in the class. However any subclasses can have all the methods needed in the interface, when the superclass implements the interface and already have the required methods.

Abstract methods in abstract class is similar to interface where the method must be available in the subclass.

So for example if we have Pets

interface Pets {
    public int owningPrice();
}

Animal

abstract class Animal {
    public int age;

    public abstract void ageOfDeath();

    public String toString() {
        return String.format("age :%d;", age);
    }
}

Dog

class Dog extends Animal implements Pets {
    public void ageOfDeath() {
        System.out.println("ageOfDeath is 20");
    }

    public int owningPrice() {
        return 1000;
    }
}

and Shiba

class Shiba extends Dog {
    // yes this is empty
}

if you declare the variables like so

Animal animal = new Shiba();
Shiba shiba = new Shiba();

and try to print out everything in the variables, it will become like this

animal
age :0;
ageOfDeath is 20
---
shiba
age :0;
owningPrice is 1000
ageOfDeath is 20

Notice that while both is instantiated using Shiba class, "shiba" has owningPrice, and "animal" doesn't since it is not a subclass of Dog and does not implement Pets.

Conclusion, subclass does not inherit interface, however if superclass implemented the interface, subclass is considered one of the interface.

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

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.

Upvotes: 16

csvan
csvan

Reputation: 9454

If you have:

abstract class StaffMember implements MyInterface

where

interface MyInterface {
    void myMethod();
} 

then all of the classes extending StaffMember will inherit the type MyInterface, and you will be able to refer to them by this base type in other parts of the code where a MyInterface instance is expected as an operand/argument, for example:

void otherMethod(MyInterface param) { //... }

The actual implementation of the interface MyInterface can take place either in the abstract class, or in any of the classes extending the abstract class. The important thing is simply, in this case, that myMethod() is specified somewhere in the inheritance hierarchy, so that the JVM can find a definition of it to invoke.

Upvotes: 27

Related Questions