[Java] Interface

In Java, interfaces are a special reference type that has the following properties:
  • May contain constants (variables that are implicitly public, static, and final).
  • May contain method signatures, but no method bodies.
  • Interfaces cannot be instantiated, but can be implemented.
In other words, an interface provides a blueprint for its implementing classes. When a group of programmers work on a software, it's often useful to specify a contract that defines how different components of the software interact, so that the programmers can work on separate parts of the software concurrently. Interfaces serve perfectly for this purpose.

To define an interface, we use the interface keyword:

1
2
3
4
5
6
7
8
public interface Animal {

    public void eatSomething();
 
    public String makeSomeNoise();
 
    public int getSize();
}

Here, I just defined an Animal interface which has three methods. To implement this interface, we can use the implements keyword in the class definition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class Giraffe implements Animal {

    public void eatSomething() {
        System.out.println("I just ate some leaves.");
    }

    public String makeSomeNoise() {
        return "...";
    }

    public int getSize() {
        return 200;
    }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class HungarianHorntail implements Animal {

    public void eatSomething() {
        System.out.println("I just ate Harry Potter.");
    }

    public String makeSomeNoise() {
        return "Rawrrrr";
    }

    public int getSize() {
        return 500;
    }

    public void trainDragon() {
        System.out.println("Training dragon."); 
    }
}

I just created two different classes that both implement the Animal interface. Although they have the same method signatures, their method implementations are different. This difference will lead to different behaviors of Giraffe and HungarianHorntail objects.

Note that HungarianHorntail has a new method trainDragon(). It's okay to add new methods that are not specified in an interface to the implementing class. This enables the implementing classes to possess unique methods while enjoying the fixed API provided by the interface.

As a type, an interface can be the type parameter of a collection. Although Giraffe and HungarianHorntail have essentially different implementations, they can be put into a collection of Animals. Later if we need to differentiate them in the collection, we can use the instanceof operator, which tells if an object is an instance of a certain class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class InterfaceTester {

    public static void main(String[] args) {
        Giraffe g = new Giraffe();
        g.eatSomething();
  
        HungarianHorntail hh = new HungarianHorntail();
        System.out.println(hh.makeSomeNoise());
  
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(g);
        animals.add(hh);
  
        for (Animal a : animals) {
            a.eatSomething();
            System.out.println(a.makeSomeNoise());
            System.out.println(a.getSize());
   
            if (a instanceof HungarianHorntail) {
                HungarianHorntail dragon = (HungarianHorntail) a;
                dragon.trainDragon();
            }
        }
    }
}

The above program will print out the following:


I just ate some leaves.
Rawrrrr
I just ate some leaves.
...
200
I just ate Harry Potter.
Rawrrrr
500
Training dragon.

Comments

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula