First, the concept of interface:
1, the initial understanding, can be a special abstract class. When a method in an abstract class is abstract, the class can be represented by an interface
2.class for defining classes, interface for defining interfaces
Second, the interface definition, the format characteristics:
1. Common definitions in interfaces: constants, abstract methods
2. The members in the interface have fixed modifiers.
Constants: public static final
Method: Public Abstract
3. Remember that the members in the interface are public.
As long as the interface inside members have fixed modifiers, can be omitted, but generally to look good to add
Iii. the relationship between classes and classes
The class is an inheritance relationship with the class extends
Between classes and interfaces is the implementation relationship implements
The relationship between interfaces is also inherited extends (can inherit more)
Iv. definition of the interface
Interface is not to create objects, because there is an abstract method, requires the Quilt class implementation, sub-class docking in the mouth of the abstract method is all overwritten, the subclass can be instantiated otherwise the subclass is an abstract class
Interfaces can be implemented by a class (the method in the interface is abstract without the method body, and the method that inherits the parent class has the method body) is also the transformation form of multiple inheritance Java support multi-implementation
Interfacea{voidMethodA ();}InterfaceB//extends A//Java supports multiple inheritance between interfaces because there is no method body for the interface{ voidMethodB ();}InterfaceCextendsb,a{voidmethodc ();}classDImplementsc{ Public voidMethodA () {} Public voidMethodB () {} Public voidmethodc () {}}
Public classInterfacedemo { Public Static voidMain (string[] args) {Test T=NewTest (); System.out.println (T.num); System.out.println (Test.num); System.out.println (Inter.num); //t.num = 4;//error, because NUM is a constant }}Interfaceinter{ Public Static Final intNUM = 3; Public Abstract voidshow ();}Interfaceintera{ Public Abstract voidshow ();}classdemo{ Public voidfunction () {}}//Java can support multiple implementations while inheriting a classclassTestextendsDemoImplementsInter,intera { Public voidShow () {}}
V. Extensibility of the interface
Interfaces are mostly used for extended functions, (basic functions defined in the class, extension functions defined in the interface)
1 Public classInterfaceTest1 {2 Public Static voidMain (string[] args) {3 4 }5 }6 Abstract classstudent{7 Abstract voidstudy ();8 voidsleep () {9System.out.println ("Sleep");Ten } One } A //The ability to extract some features not everyone has to encapsulate into an interface, the need for people to achieve him, do not need to realize his - Interfacesmoking{ - Public Abstract voidsmoke (); the } - - //if Zhangsan smokes, then implement smoking this interface - classZhangsanextendsStudent { + voidstudy () {} -}
1 Public classInterfaceTest2 {2 Public Static voidMain (string[] args) {3 4 }5 }6 7 //The design idea of the program is problematic, do not design an object into a class,8 Abstract classstudent{9 Abstract voidstudy ();Ten voidsleep () { OneSystem.out.println ("Sleep"); A } - } - the //provide an interface, so long as the required class to implement it, do not need to implement it, so that you can have a lot of features - Interfacesmoking{ - Public voidsmoke (); - } + - //Zhangsan This man smokes, he inherits smoking. + classZhangsanextendsStudentImplementssmoking{ A voidstudy () {} at Public voidsmoke () {} - } - classLisiextendsstudent{ - voidstudy () {} -}
Vi. the similarities and differences between abstract classes and interfaces:
Same point:
1, are constantly upward drawn out of
Different points:
1. Abstract classes need to be inherited, but only inherited.
Interfaces need to be implemented and can be implemented more
2, abstract class can be defined abstract methods and non-abstract methods, subclass inheritance, you can directly use non-abstract inverse method
Only abstract methods can be defined in an interface and must be implemented by subclasses
3, abstract class inheritance, is a relationship, in the definition of the basic common content of the system
When an interface is implemented like a relationship, the additional functions in the definition system
Object-Oriented _ interface