Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Interfaces and abstract classes

So my question is are interfaces strictly used to help organize your code? Everywhere I look online they say interfaces are a way you can have multiple inheritance but I dont understand when the examples are fleshed out. When you have an interface you dont really inherit anything. At least it doesnt seem like anything you see when you just inherit a normal class. You basically are just inheriting a bunch of abstract methods you must implement later. So why have them at all? I can just create a class like

public class Dog{
  public void speak(){
      System.out.println("woof!");
  }
}

I dont see why you would need an interface that says you must write a speak method if you implement a speakable interface. Can someone please explain why I would use an interface? And please give me an example I could probably parrot back to you the abstract reasoning behind why at this point but I dont understand it from a practical standpoint.

2 Answers

To use the speak() abstract method example, let's assume you have an app where you have different living animals: dogs, cats, etc. They all have their own class and all implement the Speakable interface which declares the speak method:

public interface Speakable {
  void speak();
}

Now, let's assume that you want to keep a list of all animals that can speak. You could indeed create different lists for each type. For example:

List<Dog> dogs;
List<Cat> cats;
...

However, this wouldn't be very good. For starters, you would need a list for each different animal that can speak, and your code could become a mess very quick. However, because all of your animals should be able to speak (and therefor implement the Speakable interface), you could create one list containing all different animals that can speak:

List<Speakable> animals;

You could then do something similar than this (assuming you have a working Speakable interface and a Dog and Cat class which implements the Speakable interface):

public static void main(String[] args)
{
        List<Speakable> animals = new ArrayList<Speakable>();
        animals.add(new Dog()); // Adding a new Dog object to the list
        animals.add(new Cat()); // Adding a new Cat object to the same list

        for (Speakable animal : animals)
        {
            animal.speak(); // For each animal, we want it to speak. We don't care what kind of animal it is.
        }
}

The beauty about the above code is that you don't need a different variable or field for every type of animal. You simply store all the implementations of the Speakable interface (in our case the Dog and Cat objects) inside one shared list. So yes, one of the reasons to use interfaces is to help organize your code. Why should you have multiple lists and multiple loops to achieve one goal if it could be done in far less lines? Also, maintaining this code is far more easier. If I would want to add a new type of animal that could speak, I would just have to create the implementation. Because the new implementation would implement the Speakable class, I could automatically use the new animal in all my code where the Speakable interface is used.

Using interfaces would also give you more flexibility in your code. A good example might be the java.util.Collections, this class has a couple of cool and useful methods that can be used exclusively on interfaces. For example, the sort() and reverse() methods are available for the List interface. This means that any class implementing List (ArrayList or LinkedList to give a few examples) can now use the static method Collections.sort() or Collections.reverse(). Even if you would create your own implementation of the List interface, you could use the sort and reverse static methods on it!

I hope that this example answers your question or helps you understand the use cases...

Thank you for the reply. I think I get your example but I ended up getting a much better understanding by looking at the strategy design pattern. Here is a link to the video I watched for like 3 hours today. http://www.newthinktank.com/2012/08/strategy-design-pattern-tutorial/

It makes a lot more sense when you put the interface and it's implementations in the same file. When you separate them out you need to make an additional mental leap to understand the implementation is connected to the interface but separate from the dog class.

Thanks for your help.