[JAVA] Interface benefits

Introduction

Interfaces have the advantage of helping class design, such as defining operations that are common between classes as a group, or defining an empty interface to give the implementation class some characteristics and clarify the processing intent. There are various.

Among them, many people may feel that the following points are the greatest advantages of the interface.

However, I couldn't sort out these meanings, so I decided to sort them out in my own words [^ me].

If you don't use the interface ...

For example, suppose you define three methods in class A as follows:

python


class A {
    //Method to call
    void call() {
        System.out.println("Hit / Wear / Jankenpon");
    }
    //Jacques method
    void jakajaka() {
        System.out.println(
            "Rock-paper-scissors, rock-paper-scissors, rock-paper-scissors, rock-paper-scissors!"
        );
    }
    //Dancing method
    void odoru() {
        System.out.println("The incident is happening in the conference room.");
        System.out.println("No, it's the site.");
        System.out.println("It's a conference room.");
        System.out.println("Site, site.");
        System.out.println("Meeting room, meeting room, meeting room.");
        System.out.println("Site, site, site, site,...Field room! !! !!");
    }
}

And suppose you define the following code to use this class.

python


A a = new A();
a.call();
a.jakajaka();
a.odoru();

Doing this will, of course, print the message defined by each method.

Here, it is assumed that a new B class with a modified implementation of the A class is newly established as follows, and the user is ** called to use the B class instead of the A class in the future **.

Implementation changes


class B {
    //Remove call method
    //Changed method name from jakajaka to sing
    void sing() {
        ... //Same as above
    }
    //Changed method name from odoru to genbashitsu
    void genbashitsu() {
        ... //Same as above
    }
}

Since the user has to stop using class A and use class B, it is necessary to understand the implementation of class B and modify the code so far as follows.

python


B b = new B();
b.sing();
b.genbashitsu();

Although the same message as before is output from the sing method and genbashitsu method, the user who is in trouble because the call method cannot be used ** has used some A class functions contrary to the call **. I will.

Violation example


A a = new A(); //Violation of the call
a.call();      //Violation of the call
B b = new B();
b.sing();
b.genbashitsu();

It may be a good makeshift, but if class A was to be deprecated, any part of it accessing class A might result in a compile error. In that case, you will have to fix that part in the future.

Without an interface like this, when any implementation change occurs, the user has to keep track of the changes in the new implementation and modify the code to maintain the same implementation as before. You will have to think about what you have to do. Also, if you use an old feature that you shouldn't use, you'll have to spend time fixing it when that feature is discontinued in the future.

With the interface ...

You can use the interface to let the user know which methods are available. As long as the user knows the methods defined in the interface and the functions of the implementation class to be used, even if the implementation is changed, it is not necessary to check the methods that can be used and the methods that cannot be used.

python


interface Contract {
  void call();
  void sing();
  void genbashitsu();
}

python


class B implements Contract {
    //Method to call
    @Override public void call() {}; //Empty implementation
    //Jacques method
    @Override public void sing() {
        ... //Same as above
    }
    //Dancing method
    @Override public void genbashitsu() {
        ... //Same as above
    }
}

Also, if the class that implements the interface has an empty implementation method, the user will be able to implement that method using an anonymous class if necessary [^ Empty implementation] ].

Interface usage example


Contract c = new B() {
    @Override public void call() {
        System.out.println("Hit / Wear / Jankenpon");
    }
};
c.call();        //Hit / Wear / Jankenpon
c.sing();        //Rock-paper-scissors, rock-paper-scissors, rock-paper-scissors, rock-paper-scissors!
c.genbashitsu(); //The incident is (...) the field room! !! !!

Summary

The interface allows users to build flexible programs without fear of the need for modifications due to implementation changes. If a new implementation class appears like the following code and you want to use that class, you can use that implementation just by changing the instance you generate.

New implementation class


class NewB implements Contract {
  //Method to call
  @Override public void call() {}; //Empty implementation
  //Jacques method
  @Override public void sing() {
    System.out.println("Ouiea");
  }
  //Dancing method
  @Override public void genbashitsu() {
    System.out.println("Lalala Love Sun Buddy Tonight Sticky Spring Spring Spring Spring Valeza Love Go");
  }
}

Switching the class to use


Contract c = new NewB() {
    @Override public void call() {
        System.out.println("Hit / Wear / Jankenpon");
    }
};
c.call();        //Hit / Wear / Jankenpon
c.sing();        //Ouiea
c.genbashitsu(); //Lalala Love Sun Buddy Tonight Sticky Spring Spring Spring Spring Valeza Love Go

[^ User]: The person who uses the defined class. same as below.

[^ Contract]: The requirements and specifications that the program must meet.

[^ me]: I'm honestly not confident because I'm just a hobbyist with no practical experience. If you find any mistakes in the interpretation, we would appreciate it if you could point out or make suggestions.

[^ Empty implementation]: If you use an anonymous class, you can override any method that can be accessed even if it is not an empty implementation.

Recommended Posts

Interface benefits
interface
java (interface)
new interface
[java] interface
About Java interface
[Android] Interface example
Functional interface introduction
Interface usage example
interface and abstract
[Java] About interface
[Java] Functional interface
About interface, java interface
Inheritance and interface.