Definition and implementation of Java interface Interface

Preface

An abstract class is a template abstracted from multiple classes. If this abstraction is carried out more thoroughly, a more special "abstract class" - interface (Interface) can be extracted. Interface is one of the most important concepts in Java. It can be understood as a special class. The difference is that the members of the interface have no execution body and are composed of global constants and public abstract methods.

interface

1. Use of interfaces

1.1. The interface is defined using interface

1.2. Interfaces and classes are two parallel structures in Java.

2. How to define the interface

2.1. Only global constants and abstract methods can be defined in jdk7 and before.

  • Global constants: public static final
  • Abstract method: public abstract

2.2, jdk8, in addition to defining global constants and abstract methods, you can also define static methods and default methods

3. Constructors cannot be defined in interfaces, which means that interfaces cannot be instantiated.

4. In Java development, interfaces are used by letting classes implement them.

4.1 If the implementation class covers all abstract methods in the interface, then this class can be instantiated

4.2 If the implementation class does not cover all the abstract methods in the interface, then the implementation class is still an abstract class

  • Java classes can implement multiple interfaces ------>> making up for the limitations of single inheritance of Java classes
  • 格式:class A extends B implements C,D,E
  • Interfaces can have multiple inheritances

5. Define the interface

  • public represents the modifier of the interface. When there is no modifier, the default modifier is used. At this time, the access rights of the interface are limited to the package to which it belongs;
  • interface_name represents the name of the interface. The interface name should adopt the same naming rules as the class name, that is, from a grammatical perspective, the interface name only needs to be a legal identifier. If Java readability specifications are to be adhered to, the interface name should be a concatenation of meaningful words, with the first letter of each word capitalized, without any separators between words.
  • extends represents the inheritance relationship of the interface;
  • interface1_name represents the interface name to be inherited;
  • constant_name represents the variable name, usually static and final types;
  • returnType represents the return value type of the method;
  • parameter_list represents a parameter list, and methods in interfaces have no method bodies.

Note: An interface can have multiple direct parent interfaces, but interfaces can only inherit interfaces, not classes.

implement interface

1. Implemented through inheritance of a class

Define a fruit class implementation standard.

package aaa; //包
interface Fruits{
    
      //接口标准
  public abstract String eat();  //接口方法
  public abstract String think(); //接口方法
  }
class Student implements Fruits{
    
       //子接口实现
@Override   //注解:准确覆写
	public String eat() {
    
      //覆写方法
    return "苹果";
    }
@Override
    public String think() {
    
    
	return "香蕉";
	}	
}
public class ZS{
    
    
public static void main(String args[]) {
    
    
	Fruitsa=new Student();  //实例化对象
	System.out.println(a.eat()+a.think());	//输出
    }
}

Output: apple banana

2. Implemented through inner classes inside the interface

package aaa;
interface Fruits{
    
    
public abstract String eat();
public abstract String think();
class Student implements Fruits{
    
      //类内部接口实现
	public String eat() {
    
    
		return "苹果";
	}
	public String think() {
    
    
		return "香蕉";
	}
  }
}
public class ZS{
    
    
public static void main(String args[]) {
    
    
	Fruits.Student a=new Fruits.Student(); //实例化
	System.out.println(a.eat()+a.think());	
    }
}

At this time, pay attention to the instantiation method: external class.inner class internal class object = new external class.inner class ();
if you want to define method output, the method is defined as static type.

Guess you like

Origin blog.csdn.net/m0_55400356/article/details/129762602