Java Lesson 17: Interfaces

Hello everybody,

Michel here, and today’s post will be about interfaces in Java. This lesson does relate to my previous post onĀ abstraction in Java.

But how exactly do interfaces relate to abstraction? Interfaces are completely abstract classes with only abstract methods; this means that interfaces only have abstract methods.

Here’s an example of an interface:

interface InterfaceDemo
{
public void birthdate ();
public void address ();
public void name ();
public void maritalStatus ();
}

In this example, I created an interface with four abstract methods. To access these methods, the interface must be inherited from another class-this is exactly how you would access methods from an abstract class.

  • The main difference between an interface and an abstract class is that you would use the keyword interface for interfaces and the keywords abstract class for abstract classes.

Now let’s create a new subclass to access the four abstract methods:

class Info implements InterfaceDemo
{
public void birthdate ()
{
System.out.println(“11/30/1985”);
}

public void address ()
{
System.out.println(“724 Evergreen Terrace”);
}

public void name()
{
System.out.println(“Eddie Brock”);
}

public void maritalStatus()
{
System.out.println(“Married”);
}
}

Just as you would do with abstract classes, you need to create a subclass to access the interface. The difference with interfaces is that you would use the implements keyword to access the class-not the extends keyword.

Now let’s create a main class (a class with a main method) to run the interface:

public class Form
{
public static void main (String [] args)
{
Info myInfo = new Info();
myInfo.birthdate();
myInfo.address();
myInfo.name();
myInfo.maritalStatus();
}
}

And here’s the output:

run:
11/30/1985
724 Evergreen Terrace
Eddie Brock
Married
BUILD SUCCESSFUL (total time: 5 seconds)

Now, there’s one major feature of interfaces that makes them stand out. See, Java doesn’t support multiple inheritance; a Java class can only inherit from one superclass. However, multiple inheritance is possible with interfaces, because Java classes can implement multiple interfaces. Here’s how multiple inheritance with interfaces would work:

First, let’s create the interfaces:

interface TestInterface2
{
public void city();
}

interface TestInterface3
{
public void state();
}

interface TestInterface4
{
public void country();
}

As you can see, we have three interfaces. Now let’s access each of them with a subclass:

class City implements TestInterface2, TestInterface3, TestInterface4
{
public void city()
{
System.out.println(“Nashville”);
}

public void state()
{
System.out.println(“Tennessee”);
}

public void country()
{
System.out.println(“USA”);
}
}

Remember to separate the names of each of the interfaces with a comma!

Now that we’ve got the subclass set up, let’s set up the main class:

public class Location
{
public static void main (String [] args)
{
City c = new City();
c.city();
c.state();
c.country();
}
}

And let’s run the main class:

run:
Nashville
Tennessee
USA
BUILD SUCCESSFUL (total time: 2 seconds)

The multiple inheritance we built worked perfectly!

Alright, so you’re probably wondering why you should use interfaces. After all, abstraction serves the same purpose-to achieve greater program security by only showing the important details of an object. However, interfaces have the advantage of multiple inheritance (which I just demonstrated above).

Thanks for reading,

Michael