1, the characteristics of the interface:
A: interface with keyword interface representation
Interface interface Name {}
B: Class implementation interface with implements representation
Class name implements interface name {}
C: interface cannot be instantiated
So, how does an interface instantiate?
To instantiate in a polymorphic manner.
D: Sub-class of the interface
A: It can be an abstract class. But it doesn't make much sense.
B: Can be a specific class. To override all abstract methods in an interface. (Recommended scheme)
Thus:
A: Specific polymorphic (almost none)
B: Abstract class polymorphism (common)
C: Interface polymorphism (most commonly used)
1 //Defining animal Training interfaces2 InterfaceAnimaltrain {3 Public Abstract voidJump ();4 }5 6 //abstract class implementation interface7 Abstract classDogImplementsAnimaltrain {8 }9 Ten //Concrete Class Implementation Interface One classCatImplementsAnimaltrain { A Public voidJump () { -System.out.println ("The cat can jump."); - } the } - - classInterfacedemo { - Public Static voidMain (string[] args) { + //Animaltrain is abstract; unable to instantiate - //Animaltrain at = new Animaltrain (); + //at.jump (); A atAnimaltrain at =NewCat (); - at.jump (); - } -}
2, interface member characteristics
member variables, which can only be constants and are static. Final amount, cannot be rewritten
Default modifier: public static final
Recommendation: Manually give it yourself.
Constructor method: The interface does not have a constructor method.
Member method: can only be an abstract method.
Default modifier: Public abstract
Recommendation: Manually give it yourself.
All classes inherit from a class by default: Object.
Class Object is the root class of a class hierarchy. Each class uses Object as a superclass.
1 InterfaceInter {2 Public intnum = 10;3 Public Final intnum2 = 20;4 Public Static Final intNUM3 = 30;5 6 //Error: Required < identifier >7 //Public Inter () {}8 9 //interface methods cannot have a bodyTen //Public Void Show () {} One A //abstract void Show ();//default Public - Public voidShow ();//Default Abstract - } the - //Interface Name +impl This format is the implementation class format of the interface - /* - class Interimpl implements Inter { + Public Interimpl () { - super (); + } A } at */ - - classInterimplextendsObjectImplementsInter { - PublicInterimpl () { - Super(); - } in - Public voidShow () {} to } + - //Test Class the classInterfaceDemo2 { * Public Static voidMain (string[] args) { $ //Creating ObjectsPanax NotoginsengInter i =NewInterimpl (); - System.out.println (i.num); the System.out.println (i.num2); + //i.num = +; A //i.num2 = $; the //System.out.println (i.num);//cannot assign value to final variable num + //System.out.println (i.num2);//cannot assign a value to the final variable num2 - System.out.println (inter.num); $ System.out.println (inter.num2); $System.out.println ("--------------"); - } -}
3. Classes and classes:
Inheritance, can only be single-inheritance, multi-level inheritance.
Classes and Interfaces:
The implementation of the relationship, can be implemented alone, or more.
You can also implement multiple interfaces while inheriting a class.
Interfaces and interfaces:
inheritance, which can be either single-inheritance or multiple-inheritance.
1 InterfaceFather {2 Public Abstract voidShow ();3 }4 5 InterfaceMother {6 Public Abstract voidShow2 ();7 }8 9 InterfaceSisterextendsFather,mother {//interfaces and interfaces can be multiple inheritance relationshipsTen One } A - //class Son implements Father,mother//classes and interfaces are multi-inheritance, multiple implementations - classSonextendsObjectImplementsFather,mother { the Public voidShow () { -System.out.println ("Show Son"); - } - + Public voidShow2 () { -System.out.println ("Show2 son"); + } A } at - classInterfaceDemo3 { - Public Static voidMain (string[] args) { - //Creating Objects -Father f =NewSon (); - f.show (); in //F.show2 ();//Error - toMother m =NewSon (); + //m.show ();//Error - M.show2 (); the } *}
4. The difference between abstract classes and interfaces:
A: Member Differences
Abstract class:
Member variables: Can be variables, or constants
Construction method: Available
Member methods: can be abstract or non-abstract
Interface:
Member variable: can only be constant
Member methods: can only be abstracted
B: Relationship Difference
Classes and Classes
Inheritance, single inheritance
Classes and Interfaces
Implementation, single implementation, multi-implementation
Interfaces and interfaces
Inheritance, single inheritance, multiple inheritance
C: Differences in design concepts
Abstract classes are inherited as follows: "is a" relationship. The common function of the inheritance system is defined in the abstract class.
The interface is implemented as follows: "Like a" relationship. The extension functionality of the inheritance system is defined in the interface.
Java 8-7 Interface