How to Use A Java Interface


The Java Interface is similar to a specific type of Java class we have discussed before. In the Java Inheritance you saw an interesting new keyword that went before the word class at the top of your class file: abstract. This allowed you to create a class that cannot be made into objects (please see the intermediate tutorials to look up the difference between a class and an object if you do not already know). Now we'll be dealing with a new kind of Java file, the interface. You'll soon see that you can bring a lot of structure to your program by using interfaces to help guide your development.

The Java Interface

An interface is not a class, nor is it an object! An interface acts as a blueprint for a class, but by itself does not comprise of a class. This is different from an abstract class which IS a class, but cannot be made into an object. A Java interface, likewise, cannot be made into an object either. So let's see what an interface looks like:

Notice that ALL of the methods have no code in them, and they end in semicolons! The only other types of methods that can end in semicolons and have no code in them are abstract methods, which belong to abstract classes. In an interface it is important to remember that none of the methods can be implemented at all, and that they must all end in semicolons. This is because remember, an interface is NOT a class.

Important note: You create an interface initially the way you would create a new class; make sure that the name of the file ends in .java and has the same name as the interface itself (including the same capitalization!)

Ok so, you have a Java file that happens to be an interface that has no real working code in it. You're probably asking yourself what is the point? Why bother with the interface at all? To explain this, let's backtrack to the abstract class. Remember that with an abstract class, in order to use that class, you had to use the extends keyword. Here is a previous example:

Notice how Dog is extending Animal, which means that Dog is a subclass of Animal and gets all the functionality from the Animal class. Now let's say that I create a new abstract class, one called FourLeggedObject:

Let's now say that I want my Dog class to also extend FourLeggedObject. Well guess what, it's not gonna happen, it's impossible to do so! But why? Why not just have any object extend as many classes as it wants? The reason for this is because Java cannot handle multiple inheritance, which is exactly what this situation would cause. Multiple inheritance is beyond the scope of this tutorial, but feel free to learn more about it if you wish.

How to Use A Java Interface

Java has sort of solved the issues involved with multiple inheritance by creating an interface. A Java interface is useful to you by allowing objects to implement one or multiple interfaces, so that your object conforms to multiple types of blueprints (what the interfaces essentially are). Here is an example of a Dog implementing multiple Java interfaces:

To add a single interface, simply declare the one interface, but if you want to implement multiples, simply add a comma in between each one. It's that simple. Note that this means that the Dog class now MUST implement all of the methods defined in Animal AND all of the methods in FourLeggedObject. That is the point of the interface - to provide the programmer a blueprint for which to create an object. Please realize that you are not stuck to just implementing the methods defined in the interfaces, you can continue to add new methods to your class on top of the ones you HAVE to have.

Note: If you get an error in your class, please make sure you have implemented ALL of the methods in all of the interfaces you are implementing. Also make sure that the signatures match (accessor type, return type, name capitalization, etc).

The Java Interface provides one more benefit to you, and that's being able to put an object into collections that hold different kinds of objects. Do you remember about Java Collections? One such collection is an ArrayList, and we can use an ArrayList to store particular kinds of objects. For example, let's store a bunch of Animals. In our example Dog implements Animal, so Dog is an Animal.

You can tell a collection to store a bunch of objects based on their Interface type! That is correct. So, we are not limited to storing Dogs in an animal ArrayList, we can also store them in a FourLeggedObject ArrayList:

This means we can mix and match objects simply because they have the same Interface! Let's store another object in our FourLeggedObject ArrayList, let's say a chair (some chairs have four "legs" right?):

Now we have both Dogs and Chairs in the same ArrayList, because we are storing objects of type FourLeggedObject, and both of those objects are FourLeggedObjects.

This sure beats having to have a separate ArrayList for each specific type of object wouldn't you say?


Try using Java interfaces in your code. It can help you with initially creating objects when you're not sure how to implement them since interfaces have no implementation. They can help be building blocks to your Java applications and I highly recommend using some!

The next lesson will deal with Polymorphism and it's different kinds. Have no idea what Polymorphism is? Stay tuned for the next lesson to find out how you can use it to truly create object-oriented applications in Java!

If you have any questions, comments, or concerns, feel free to contact us.

Custom Search