Come on Interface Forest

Interface </ b>, one of the three major syntaxes </ b> that I arbitrarily defined when I started studying.

When is this person used? </ b> Is the syntax really necessary? </ b>

I'm going to unravel that.

By the way, it doesn't matter, but the remaining three major syntaxes are extends and abstract. Youthfulness. </ font>

What is Interface?

Interface is an English word that represents the contact surface and the intermediate surface. (From Wikipedia) The USB attached to a personal computer is also called a interface </ b> for connecting the personal computer to various peripheral devices. This is because it is too troublesome to connect various peripheral devices if they have their own connectors, so they are standardized according to the USB standard.

With that kind of feeling, I / O that is common to other classes can be realized by implementing Interface. Let's do it.

How to make Interface

Use the ʻinterface` keyword to define an Interface in Java. Only member variables and abstract methods can be defined in Interface.

Member variables are automatically treated as public static final, so they can only be used as constants.

Since the method is an abstract method, you cannot define the process in Interface, you can define only the method name, return value, and formal parameters.

ExampleInterface.java


interface ExampleInterface{
  int getNum();
  void setNum(int num);
}

So, I will implement it using the ʻimplements keyword on the class side that uses this. ʻImplements means "implementation".

ExampleClass.java


class ExampleClass implements ExampleInterface{
  int num = 10;

  @Override
  int getNum(){
    return num;
  }

  @Override
  void setNum(int num){
    this.num = num;
  }
}

To repeat the above, the method defined in Interface becomes an abstract method without permission, so it must be overridden.

For the time being like this, I tried to implement the getter and setter that I have written so far as Interface.

Convenient use of Interface

Can be cast

You can also cast Interface as you did when inheriting.

For the time being, I made an appropriate class. Interface uses the previous one.

ExampleInterface.java


interface ExampleInterface{
  int getNum();
  void setNum(int num);
}

Make suitable classes A and B.

ClassA.java


class ClassA implements ExampleInterface{
  int num = 10;

  @Override
  int getNum(){
    return num;
  }

  @Override
  void setNum(int num){
    this.num = num;
  }
}

ClassB.java


class ClassB implements ExampleInterface{
  int num = 20;

  @Override
  int getNum(){
    return num;
  }

  @Override
  void setNum(int num){
    this.num = num;
  }
}

Then, would you like to move this one? I will try to output appropriately without using the advantages of Interface.

RunClass.java


class RunClass{
  public static void main(String[] args){
    ClassA classA = new ClassA();
    ClassB classB = new ClassB();

    printNum(classA);
    printNum(classB);
  }

  static void printNum(ClassA classA){
    System.out.println(classA.getNum());
  }

  static void printNum(ClassB classB){
    System.out.println(classB.getNum());
  }
}

I wrote it a lot to get the explanation. It's okay because there are only two defined classes now, but it would be difficult if there were more Class C and Class D.

Now, let's cast it to the Interface type and process it </ b>!

RunClass.java


class RunClass{
  public static void main(String[] args){
    ClassA classA = new ClassA();
    ClassB classB = new ClassB();

    printNum(classA);
    printNum(classB);
  }

  static void printNum(ExampleInterface instance){
    System.out.println(instance.getNum());
  }
}

Like this, all classes that implement ʻExampleInterface can now be processed by the printNum` method.

I think it can be used when you want to promise that there is a method with a common name in all the implemented classes </ b>.

Multiple implementations are possible

ʻExends` keyword inheritance couldn't inherit two or three classes, but Interface can </ b>.

Not only USB but also analog terminals for inserting earphones and LAN ports are available on personal computers. With that kind of feeling, you can make it compatible with various inputs and outputs by adding Interface.

So, I think that Interface should be used only for implementing I / O related methods, and should not be used for writing gorigori processing.

If a class has too many features, you shouldn't do it because you'll end up with more than 1000 lines in a file and you won't know where or what you wrote.

So, if you want to implement gorigori processing, you should consider making it an abstract class, and if you want to inherit another class, you should consider creating another class by separating the function in the first place.

You are in charge of ○○! I think it's best to be able to express it in one word ...?

Hawfinch

The proper use of Interface and abstract class seems to be a problem that everyone faces at least once, so until I find the optimal solution for myself, I get along with Google teachers and read Qiita entries and articles of Naughty Engineer School. I think I should try it.

that's all!

reference

[Introduction to Java] Summary of how to use interface