Copyright

Comparing Interfaces & Abstract Classes in Java

Instructor: Martin Gibbs

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

Object-oriented programming is all about reducing unnecessary code. In Java, interfaces and abstract classes are powerful tools for writing well-organized programs. This lesson will cover each and provide code examples.

Interfaces and Abstract Classes

Interfaces and abstract classes are great tools for reducing repetitive code and keeping all the nitty-gritty details of code away from the end user or programmers. We'll cover each type and provide working code examples. Let's start with interfaces.

Interfaces

In reality, an interface is already abstract because they don't hold all the details of what is being done. Think of a card game app on your phone; there's a little icon on your screen that you click. That is all you need to do. The interface is a gateway to the actual code that runs the card game.

What does this look like in code? Let's take a look at the following interface for a card game. We don't necessarily have to know the details behind the shuffling of the cards, or how the cards are dealt. There are two empty methods listed, shuffle and deal.


interface CardGame {
 public void shuffle();
 public void deal();
}


Multiple Inheritance

Interfaces are great tools that help us get around Java's restriction on multiple inheritance. That is, a single class cannot inherit from more than one main class. The game of Golf cannot inherit from both CardGame and SolitaireCardGame classes. Instead, interfaces would allow you to access methods from both classes.

Remember that interfaces let us avoid the restriction of multiple inheritance (also called polymorphism). An interface CAN inherit (extend) from multiple classes. When we create an interface, we can tell it to draw from multiple classes. We do this by using the extends keyword.

Take a look at the following code. Here we create a new interface that extends both the CardGame and Solitaire interfaces:


public interface SolitareDoubleDeck extends CardGame, Solitaire {
 public CardGame doubleShuffle();
}


Classes Using Interfaces

We used the extends keyword on an interface to get around multiple inheritance restrictions. That is specific to interfaces. So how do we use an interface in a class? In Java, the keyword is implements to specify an interface to use.

If we create a new class for a card game, say Double Diamond, we can use that interface by using the implements keyword:


public class DoubleDiamond implements CardGame() {
}


An interface is useful for our card game example - you shouldn't have to reinvent the wheel each time a new game is added. There are hundreds of solitaire games. Our code should be flexible and transparent in order to add a new game. The shuffle and deal methods should still work regardless of the new type of game.

Extend vs. Implement

It may be tricky to remember these important concepts for classes/interfaces. Here's a quick method for using each:

  • If you add more buttons to your interface, you EXTEND it
  • If your code uses an interface, it is IMPLEMENTED

Now that we've covered some basics of interfaces, let's look at another important concept in object-oriented programming, the abstract class.

Abstract Classes

Where an interface declares methods and can let us get around the multiple inheritance restriction, an abstract class is a class that cannot be instantiated. You cannot create an instance of an abstract class.

What's the use? you might ask.

To unlock this lesson you must be a Study.com 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 Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
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? Study.com 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
Support