Benefit analysis of java interface

This article describes the advantages of java interface. Share with you for your reference, as follows:

What is the function of interface?

Suppose we developed the implementation class Demo1 of a business before, and now we need to redesign the implementation class demo2 of the business according to the requirements without affecting the operation of other businesses.

At this time, you need a standard, a standard interface for the business implementation. All classes that implement the interface need to implement the methods of the interface. Through the upward transformation of Java, there are different classes of implementations for different businesses.

notes:

1. Interface has only method name and return value, no method body, and is an abstract method. It must not be instantiated directly (the implementation of the interface is obtained through the upward transformation of its implementation class).

Test. java

package Test;
public interface Test
{
  //定义了一个接口方法
  public String getTestText();
}

Test1. java

package Test;
public class Test1 implements Test
{
  @Override
  public String getTestText()
  {
    return "this class name is test1";
  }
}

Test2. java

package Test;
public class Test2 implements Test
{
  @Override
  public String getTestText()
  {
    return "this class name test2";
  }
}

Index. java

import Test.Test;
public class Index
{
  public static void main(String[] args)
    throws Exception
  {
    Test test1 = testIndex("Test.Test1");
    System.out.println(test1.getTestText());
    Test test2 = testIndex("Test.Test2");
    System.out.println(test2.getTestText());
  }
  public static Test testIndex(String classPath)
  {
    Test test = (Test)Class.forName(classPath).newInstance();
    return test;
  }
}

So far, do you see the benefits of this use? Both test1 and test2 implement the test interface. If we want to change the test implementation or have different test implementations for different businesses in the future, we can dynamically load different implementation classes for test.

Interfaces can be inherited from one interface to another

Demo1. java

package Demo;
public interface Demo1
{
  public String getName();
}

Demo2. java

package Demo;
public interface Demo2 extends Demo1
{
  public String getAge();
}

Demo3. Java inherits demo2 and needs to implement the interface methods in Demo1 and demo2

package Demo;
public class Demo3
{
  @Override
  public String getName()
  {
    return "my name is Mr.tan";
  }
  @Override
  public String getAge()
  {
    return "my age is 22";
  }
}

In fact, the essence is the multi clock representation (polymorphism) of a class. For different services, the same interface or class shows different forms through different implementation classes.

For more Java related content, interested readers can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>