Java learning notes 10 (Object-Oriented 3: interfaces) and java learning notes
Interface:
It can be understood as a special abstract class.
An interface is a set of functions. It can be viewed as a data type and is a more abstract "class" than an abstract class"
The interface only describes the methods that should be available, and has no specific implementation. The specific implementation is completed by the interface implementation class (equivalent to the interface subclass ).
In this way, the function implementation and definition are separated, and the program design is optimized.
All member methods of the interface are abstract. methods with method bodies cannot exist.
Ordinary member variables cannot be defined in the interface and must be defined as constants.
Interface Definition abstract methods have fixed formats
Everything has a function, that is, everything has an interface.
Interface Definition rules:
public interface MyInterface { public static final int a = 1; public abstract void function();}
Interface implementation class (Class and Class are called inheritance, and class and interface are called implementation ):
Public class MyInterfaceImpl implements MyInterface {public void function () {System. out. println ("implementation class, override interface method ");}}
Test:
public class Test { public static void main(String[] args) { MyInterfaceImpl my = new MyInterfaceImpl(); my.function(); }}
Features of the member variables in the interface:
Note that the public static final modifier can omit any number, but the effect remains the same. It is always public static final. It is recommended to write it completely.
Public interface MyInter {// static can be accessed in. // final mode, indicating that the public static final int x = 3 cannot be modified ;}
Test:
Public class Test {public static void main (String [] args) {// static can be used. method of access. For more information about static, refer to System. out. println (MyInter. x );}}
Features of the member methods in the interface:
Note that the public abstract here is also optional. No matter how effective it is, it is recommended to write it completely.
public interface MyInter { public abstract void function();}
Implementation class, implementation interface, rewrite all abstract methods of the interface, and create implementation Class Object
The interface cannot create objects.
The subclass must overwrite all the abstract methods in the interface before it can be instantiated. Otherwise, the subclass is an abstract class.
Multiple implementations of interfaces:
The most important embodiment of interfaces: Solves the disadvantages of Multi-inheritance, and implements the multi-Inheritance Mechanism too much in java.
Principle of solving Security Risks: Methods in interfaces are all abstract and can be implemented only when there is no subject. Therefore, there is no security problem.
Therefore, multiple implementations of interfaces solve the limitations of single inheritance.
Example:
Interface:
public interface A { public abstract void a(); public abstract void c();}
Interface B:
public interface B { public abstract void a(); public abstract void b();}
Multiple implementations:
/** Class C: implements two interfaces at the same time. Interfaces A and B * are used as implementation classes. C must rewrite all the abstract methods of the two interfaces, C class Object **/public class C implements A, B {public void a () {}public void B () {} public void c (){}}
You can also implement interfaces while inheriting classes:
Class D:
public abstract class D { public abstract void d();}
C:
// While inheriting one class (only one), implement multiple interfaces public class C extends D implements A, B {public void a () {} public void B () {} public void c () {}public void d (){}}
Interface has multiple inheritance:
Because the interface methods are abstract, there is no security risk.
public interface A { public abstract void a();}
public interface B { public abstract void b();}
public interface C extends A,B{ public abstract void c();}
public class D implements C{ public void a(){} public void b(){} public void c(){}}
Here is a question about java:
Is there any inheritance in java?
A: There is no multi-inheritance for classes, and there are multiple inheritance between interfaces.
Interface idea:
For example, there are multiple plug-ins on the computer. These plug-ins can be inserted into the corresponding device because these devices comply with the use rules of the plug-in during production. Otherwise, they cannot be inserted and used.
Benefits of interfaces in development:
1. Extended functions of the interface
2. interfaces are actually exposed rules.
3. The appearance of interfaces reduces coupling and decouples devices (whether the USB Plug-in of a laptop can be run on a computer)
What interfaces and abstract classes have in common:
1. They are all at the top of the inheritance class for implementation or inheritance by other classes.
2. Objects cannot be directly instantiated.
3. All include abstract methods, and their subclasses must overwrite these abstract methods.
Differences:
1. abstract classes provide implementation for some methods to avoid repeated implementation of these methods by subclass, improving code reusability. interfaces can only contain abstract methods.
2. A class can only inherit one direct parent class (which may be an abstract class), but multiple interfaces can be implemented. The interface makes up for java's single inheritance.
Ideological differences:
Abstract classes are the content that should be possessed in this transaction. The Inheritance System is a relationship between is..
The interface is an extra content in this thing, and the inheritance system is a relationship like...
Example of pseudocode: class thief extends implements theft
A thief is a person. He can steal not all people.
Optional:
1. Give priority to interfaces and use abstract classes as few as possible.
2. When you need to define the behavior of the subclass and provide the common function for the subclass, select the abstract class.