Java Interfaces: Definition & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems, has a PhD in Information Technology Management, and a degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

A Java interface need not be a scary concept: It is like a class, but without methods. Interfaces are used to work with several classes and share information between them. We will define the term and provide examples of Java code.

Interfaces: They Aren't That Scary

When we think interface, we can think of the app icons on our phones: They are the interface between your thumb and the processing unit on the phone. This concept can also be applied to Java: an interface is the gateway to different objects and methods. A book interface might call methods that might turn the page, open an index, or search for text.

An interface won't actually hold the code for turning the page, however. It's just the gateway.

So why use an interface in Java? One of the biggest reasons to use interfaces in Java is to avoid the multiple inheritance restriction.

Multiple Inheritance

Other languages support multiple inheritance, a subclass inheriting from multiple parent classes. Java doesn't allow this. However, a way around this restriction is through the use of interfaces.

For example, think of a Periodicals class: You can create three classes, each that inherit from Periodicals, say Books, Magazines, and Newsletters. However, you can't create a How-To Manual that inherits from ALL of the other classes. The following graphic depicts this restriction:

Java Multiple Inheritance Not Allowed

Instead, you can create an interface so that you can still get to the pieces you need from the other classes.

Interfaces: Examples

Let's create a basic interface that draws on our example above. We'll start at the highest level, Periodicals, and gradually work our down to the How-To Manual.

First, create a Periodicals class, and the interface that calls some methods within the Periodicals class. Remember, interfaces are declared with the interface statement:

Java Interface Basic

You'll notice that the interface calls the methods getPublisher and getPublishDate. This is all it can do with methods; it can't create its own. These methods as they are displayed here are called abstract methods.

Abstract methods are those that are declared but don't actually have any code in them. The real code in the method is defined somewhere else. Again, think of the interface on the phone: The Pinterest button doesn't do Pinterest work, it just opens the application.

One of the classes shown in the earlier diagram is the Books class. The Books class can easily inherit from the Periodicals class, it's the How-To Manual that can't inherit from everybody.

So now let's create the Books class. We will inherit from the Periodicals class, but we will also want to implement, or invoke, the interface we created for Periodicals. To do this we need the implements keyword when we define the class so that Java knows which interface is being used.

Additionally, we have to create the two methods we referenced in the interface definition!

Java Interface Class Implements

Let's put this all together in a working program. Items have moved a little, but the core code remains. In the main method, we create a new Books object and call the methods.

Java Basic Interface Full Code

The output of running the above code will be:

Java interface output

Multiple Classes

Now we can get to the fun stuff: bypassing the multiple inheritance restriction and using the interface across classes.

Our end goal is to create a How-To Manual class that can draw from both the Periodicals AND the Books classes. One interface cannot implement another one, but it can extend another one. This is done by using the extends keyword when creating the interface, and referencing the interface you are extending. Extends is also used when creating a child class; it tells Java which parent class is being inherited.

First, let's create the interface for the books class. We've included the original interface, getPeriodicals, for reference. Note that the getBooks interface the original interface.

Java interface extends

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account