Simple guide to Java interface for beginners

0
491
Simple guide to Java interface for beginners

Java interface is one of the most important concepts of object-oriented programming. With interface, we can make full abstraction of everything and make software development easier.

However, Java interface doesn’t seem to be easy to understand for new learners to grasp.

This article will give you a simple explanation on Java interface.

Table of Contents

Java interface, what is it?

Let me rephrase the interface statement from official Java documentation.

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.

There are three things to notice on above definition:

  • Java interface is definitely not a class, it is another type similar to class.
  • It provides behaviors.
  • It is a contract between class and it allows class to implement it..

Let’s go through each of these points.

1. Interface itself is a special type

Certainly, it is not a class, it has its own declaration recognized by keyword interface.

Declaring a Java interface is similar to a Java class, instead of using keyword class, you have to use keyword interface like this.

public interface Movable { }

public interface Drawable { }

Naming interface is similar to naming class, same convention.

Practically, developers would name it depending on its expose-able behaviors.

2. It provides behaviors

Behaviors are methods in programming. However, methods in interface are different from methods in class.

In a class, a method will contain both declaration and definition, but an interface only declares method name and signature.

For example:

public interface Movable {
    void walk();
    void run();
    boolean canFly();
}

As you can see, there is absolutely nothing inside interface. Declaring methods is only what we need to do for interface.

3. It defines a contract between classes

Here comes to the main point of interface.

First, class can implement interface, which works as following

  • Class implements interface through implements keyword.
  • Class must fulfill the definition of methods declared by interface.
  • Any method that comes from interface should have Override annotation on signature.

See following example:

A Kitten certainly can walk in real life, so we would have the following Kitten class.

public class Kitten implements Movable {
    
    @Override
    public void walk() {
        System.out.println("Kitten can walk");
    }

    @Override
    public void run() {
        System.out.println("Kitten can run");
    }

    @Override
    public boolean canFly() {
        return false;
    }
}

Similarly, if we want to write an Eagle class.

public class Eagle implements Movable {
 
    @Override
    public void walk() {
        System.out.println("Eagle can walk");
    }

    @Override
    public void run() {
        System.out.println("Eagle can run");
    }

    @Override
    public boolean canFly() {
        return true;
    }
}

So now, we have two classes Kitten and Eagle that implement from Movable interface.

At this point, Movable is a contract between those implementation. What we can start to do here is to declare a new instance with that Movable contract type, and initialize with one of concrete implementation.

Look at this demonstration:

public class InterfaceTesting {
    public static void main(String... args) {
        Movable animal = new Kitten();
        animal.walk();
        animal.run();
        if(animal.canFly()) {
            System.out.println("I can fly");
        }

        animal = new Eagle();
        animal.walk();
        animal.run();
        if(animal.canFly()) {
            System.out.println("I can fly");
        }
    }
}

Interface is a type, it has no definition, so it can not be used to initialized a new object. Also, in above example, since animal instance is a type of Movable , we can instantiate it with any concrete class that implement Movable interface.

What is Java interface for?

There are many benefits from using interface.

  • We can totally apply full abstraction, so it gives the flexibility change the concrete code when necessary.
  • It helps to resolve the multiple inheritance problem. As you know, in Java, a class can only inherit directly from only one parent class.
  • It helps to loose coupling between modules.

Providing full abstraction

Look at the Movable interface above, it does not have any code definition for its methods, only declare.

This way, the concrete implementation is very flexible, the Movable animal instance can be switch to be either Kitten or Eagle as necessary.

Resolving multiple inheritance problem

Let say we have the following Animal base class.

public abstract class WalkingAnimal {
    public abstract void walk();
}

public abstract class FlyingAnimal {
    public abstract void fly();
}

and following children classes:

public class Kitten extends WalkingAnimal {

    @Override
    public void walk() {
        System.out.println("Kitten can walk');
    }
}

public class Eagle extends ??? {
    
    // WHAT TO DO?
}

Eagle can not only walk but also fly, how do we achieve this?

The truth is that you can’t do it with inheritance.

However, it can be resolved easily using interface.

public interface Walkable {
    void walk();
}

public interface Flyable {
    void fly();
}

public class Eagle implements Walkable, Flyable {
    @Override
    public void walk() {
        System.out.println("Eagle can walk");
    }
    
    @Override
    public void fly() {
        System.out.println("Eagle can fly");
    }
}

public class Kitten implements Walkable {
    @Override
    public void walk() {
        System.out.println("Kitten can walk");
    }
}

public class InterfaceTesting {

    public static void main(String... args) {
        Walkable animal = new Kitten();
        animal.walk();

        animal = new Eagle();
        animal.walk();
        Flyable flyAnimal = (Flyable) animal;
        flyAnimal.fly();
    }
}

Loose coupling

As you can see from the very first example, animal can be any appropriate type.

It is determined at runtime, so we don’t have to really tie animal to a specific implementation.

Look at following code:

public class InterfaceTesting {
    public static void main(String... args) {
        Animal animal = null;

        if(args[0].equals("kitten")) {
            animal = new Kitten();
        } else {
            animal = new Eagle();
        }

        animal.walk();
    }
}

If you start program with argument equal to “kitten”, animal will become an instance of Kitten class, or else, it will be an instance of Eagle class.

Conclusion

It is very important to understand the core concepts of Java interface, because it is the foundation of object-oriented software development.

If you’re new to this, I would recommend to keep practicing more, coding more, you will gradually understand it.

I hope this tutorial is not so difficult for beginners to pick up and learn basic concepts of interface in Java. Hope you like it!

Finally, if you like this tutorial, please subscribe or follow us on FacebookTwitterGoogle+ and Youtube to get updates on latest programming tutorials.