Interface (Interface)
is a collection of abstract methods , and interfaces are usually declared with interface.
A class inherits the abstract method of an interface by inheriting the interface.
interfaces are not classes , and the way they are written is similar to classes, but they are different concepts.
class describes the properties and methods of an object. The interface contains the methods to be implemented by the class .
Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.
the interface cannot be instantiated, but it can be implemented . A class that implements an interface must implement all of the methods described in the interface ( all methods are abstract methods ), otherwise it must be declared as an abstract class.
Interfaces have no constructor methods, support multiple inheritance , and cannot contain member variables, except static and final variables.
Declaration method of the interface
interface 接口名称 [extends 其他的类名] { //任何类型 final, static 字段 public [返回类型] [函数名] (); // 抽象方法(所有都是 public)}
Interfaces are implicitly abstract , and it is not necessary to use the abstract keyword when declaring a method in an interface and an interface. The methods in the interface are public.
class implements the interface using the Implements keyword. In a class declaration, the Implements keyword is placed after the class declaration.
... implements 接口名称[, 其他接口, 其他接口.........
When overriding methods in an interface, be aware that:
A class cannot throw a mandatory exception when implementing the method of an interface, but only in an interface, or an abstract class that inherits an interface.
class to maintain a consistent method name When overriding a method, and should maintain the same or compatible return value type.
A class can inherit only one class, but it can implement multiple interfaces , while an interface can inherit another interface.
an interface can inherit another interface , and the inheritance between classes is more similar. The inheritance of an interface uses the extends keyword, which inherits the method of the parent interface .
Abstract class
In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects , and if a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract classes In addition to the object cannot be instantiated , other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class. Abstract classes must be inherited in order to be used. (It does not have enough information to depict an object that can only be used if the subclass is fully inherited)
Abstract classes are used in the Java language to define abstractions.
publicabstractclass Employee{ //成员变量 //成员方法,可以是抽象的或实现的(可以没有抽象方法) abstract [返回类型] [函数名]();}
Declaring an abstract method can result in the following two results:
If a class contains an abstract method, the class must be an abstract class. But an abstract class can have no abstract method, just declare the class as abstract.
Any subclass must override the abstract method of the parent class (one sentence abstract method is overridden, except that the interface's methods are all abstract), or declare itself an abstract class.
Difference
An interface is not a class, and an abstract class is a class that is not fully functional and cannot instantiate an object.
A class can implement (implements) multiple interfaces. A class can inherit only (extends) an abstract class.
The interface has no constructors, all methods are public abstract, and the member variables are not generally defined. (All member variables are static final and must show initialization).
Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.
A class that implements an interface must implement all of the methods described in the interface ( all methods are abstract methods ), otherwise it must be declared as an abstract class.
If a class contains an abstract method, the class must be an abstract class. Any subclass must override the abstract method of the parent class, or declare itself an abstract class.
[Java] Interface (Interface) and abstract class usage rules and differences