42

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?

munchschair
  • 1,593
  • 3
  • 19
  • 43
  • 2
    you should try it and come here to ask - "why".. – TheLostMind Dec 12 '13 at 04:00
  • 4
    @munchschair - what the members above are trying to say is that this is something you can easily explore just by writing a small program, running it and studying the output. If something is not clear from that, *then* it is suitable to open an SO question. One of the core rules of this community is *research first, ask later*. I am of course happy to help you in either case, but you should keep it in mind. – csvan Dec 12 '13 at 04:14
  • Okay. It just gets confusing sometimes. – munchschair Dec 12 '13 at 04:16
  • 1
    @Oli - Or munchschair can ask the question to experts, on a site created for that. – Nicolas Barbulesco Jul 23 '14 at 07:58
  • 12
    @chrsva - *Asking is part of research.* This question is very good. Nobody forces you to answer it if you don't want to. By asking this question here, munchschair not only gets the answer much more quickly than by experimenting, but it also helps me to find the answer with [the modern Oracle](http://www.google.com). – Nicolas Barbulesco Jul 23 '14 at 10:12
  • @NicolasBarbulesco - I refer you to the community guidelines. – csvan Jul 23 '14 at 13:42
  • 9
    There always has to be a bunch of annoying people on this site who don't answer questions, and instead tell people to not ask them. Guys, look how much space your ridiculous comments took up. If you have nothing to contribute, please stay OFF this site. munchschair, you're question was very good and there was nothing wrong with it. Thank you for your contribution! This post helped me to understand the output of the program that I wrote. – Omar N Apr 23 '17 at 04:49

2 Answers2

26

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.

csvan
  • 8,782
  • 12
  • 48
  • 91
  • Okay, so it does work. I'm gonna add this the original. So to use another 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". The subclass of canine "Dog" implements the interface "Pets". Therefore all subclasses of "Dog" also implement the interface "Pet" without having to individually implement pet, right? – munchschair Dec 12 '13 at 04:14
  • Yes, in that case all subclasses of Dog will have the additional interface basetype `Pets`. The methods you declare in `Pets` will then have to be implemented in `Dog`, since Dog (I assume from your example) is not abstract. If you want other Pets Dogs, such as Shepherd or Chihuahua to behave differently than Dog as Pets, then you can simply override those methods in each of the Shepherd and Chihuahua classes. They are still `Pets` though, no matter what you do. – csvan Dec 12 '13 at 04:17
  • I'm trying to think of scenarios where this will be useful, using interfaces instead of inheritance. – munchschair Dec 12 '13 at 04:21
  • 2
    There are lots, and lots (and lots!) of them :) Interfaces are an incredibly important part of effective Java design strategies, even if they may appear pretty suspect at first. – csvan Dec 12 '13 at 04:22
15

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.

malana
  • 5,045
  • 3
  • 28
  • 41
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170