What Is Interface Inheritance?
Interface inheritance is a concept from design patterns. Interface inheritance, also known as subtyping. Describes when an object can be used in place of another object.
Interface inheritance
Right!
- Chinese name
- Interface inheritance
- Also known as
- Subtyping
- From
- A concept in design patterns
- Related
- The interface only defines the behavior without the meaning of the category
- Interface inheritance is a concept from design patterns. Interface inheritance, also known as subtyping. Describes when an object can be used in place of another object.
- Basic concepts of interfaces
- Interface can
- Two meanings of the interface:
- class testInterface implements fruit, fruit1 {
- public float buyFruit () {
- // Here is the implementation of the method
- return 0;
- }
- public float saleFruit () {
- // Here is the implementation of the method
- return 0;
- }
- public float saleFruit1 () {
- // Here is the implementation of the method
- return 0;
- }
- public abstract class testInterface implements fruit {
- public float buyFruit () {
- return 0;
- }
- }
- The core concept of JAVA: interface
An interface belongs to the same level as a class. In fact, an interface is a special abstract class.
Such as:
interface IA {
}
public interface: A public interface is similar to a class. A file can have only one public interface and the same name as the file.
You cannot define a public interface and a public class in the same file.
- In an interface, all methods are public and abstract; all properties are public, static, and constant.
- The format of a class implementing an interface:
class IAImple implements IA {
};
- A class implements an interface, which is equivalent to inheriting an abstract class.
- The class must implement the methods in the interface, otherwise it is an abstract class.
The interface and class are the same in the implementation.
- You do not need to write public in the interface, but public cannot be saved in the process of implementing the interface in the subclass.
(If public is left, an error will be prompted during compilation: the object cannot implement the method from the interface.)
- Note:
In addition to inheriting another class, a class can also implement interfaces;
class IAImpl extends java.util.Arrylist implement IA {}
Inherited classes implement interfaces so that multiple inheritance can be implemented in disguise.
A class can only inherit another class, but it can inherit multiple interfaces, separated by ",".
Implements IA, IB
The so-called implementation of an interface refers to the implementation of the methods in the interface.
Interfaces can define inheritance relationships between interfaces, and allow multiple inheritance between interfaces.
Example: interface IC extends IA, IB {};
The interface can also be used to define the object IA I = new IAImpl ();
Implemented classes that inherit from parent classes and interfaces can be used as runtime types.
IAImple extends A implement IA, IB
IB I = new IAImple ();
I instance of IAImple;
I instance of A;
I instance of IA;
I instance of IB;
The results returned are all true.
- Interface and polymorphism are the core of JAVA technology.
- Interfaces are often defined by us as a class of XX things.
An interface is actually defining a specification and standard.
- Common attributes of objects at different levels and systems can be realized through interfaces;
Implement write once as anywhere through the interface.
Take JAVA database connection as an example: JDBC develops standards; data vendors implement standards; users use standards.
Interfaces are usually used to shield underlying differences.
Therefore, the interface is also used to maintain the stability of the architecture for the above reasons.