java: object-oriented-interface (interface)

How it came about

Further provisions are made on the basis of abstract classes.
We know that an abstract class can have ordinary methods and abstract methods, so if an abstract class only has abstract methods and constants, it can be defined as an interface.

Interface is a special abstract class.

Define the interface

interface 接口名 {
    
    
	常量;
	抽象方法;
}

The default definitions in the interface are constants, that is

public static final int x = 5;int x = 5;

The methods defined by default in the interface are abstract methods,

public abstract a ();
同
a ();

There is no constructor in the interface, and the method cannot be instantiated.

Implement the interface

The class that implements the interface is called the implementation class, the implementation class must implement all the methods in the interface, and everything must be decorated with public

class 类名 implements 接口名 {
    
    
	
}

characteristic

  • A class can implement multiple interfaces, separated by commas
  • Above jdk8: Add default to the method in the modified interface, which can have a method body, and the implementation class does not force the implementation of this method.
interface 接口名 {
    
    
	public default f () {
    
    
		
	}
}

The reason for this?
For example, there are many methods in defining an interface. If our implementation class only wants to implement one of the methods, then we have to write other methods that are not used, which is not very friendly. So added this,

inherit

One interface can inherit another interface, which is similar to the way of inheritance between classes.

// 文件名: Sports.java
public interface Sports
{
    
    
   public void setHomeTeam(String name);
   public void setVisitingTeam(String name);
}
 
// 文件名: Football.java
public interface Football extends Sports
{
    
    
   public void homeTeamScored(int points);
   public void visitingTeamScored(int points);
   public void endOfQuarter(int quarter);
}

Multiple inheritance:
In Java, multiple inheritance of a class is illegal, but interfaces allow multiple inheritance.

public interface Hockey extends Sports, Event

effect

  • Indirect realization of multiple inheritance (java class does not support multiple inheritance)
  • Define norms and standards through interfaces
  • Separate standard formulation and implementation

Suggest interface-oriented programming to improve scalability

Comparison of interface and abstract class

Same point:

  • Can not be instantiated
  • Can contain abstract methods

difference:

  • There are only static constants in the interface, and ordinary member variables in the abstract class.
  • Only the abstract interface method, there may be non-abstract methods (using an abstract class defaultmodified)
  • The interface does not contain a constructor, but an abstract class can have a constructor
  • Multiple inheritance is supported in the interface, abstract class does not support multiple inheritance

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113837425