The Java Interface concept:
It's about the narrow interface type. The generalized interface refers to the interface of the class and the interface of the method.
Initial understanding, can be considered a special abstract class
When a method in an abstract class is abstract, the class can be represented by an interface.
Java interfaces are similar to pure abstract classes, containing only constants and abstract methods, without variables and specific methods. Understanding Java interfaces from a deeper level is a separation of definitions (specifications, constraints) and implementations. The Java interface itself does not have any implementations, so the Java interface is more abstract than the Java abstract class. Java interface methods can only be abstract and public, Java interface can not have a constructor method, Java interface may have public, static final property.
Format:
interface{}
When the interface is defined, the format features:
1. Common definitions: constants, abstract methods
2. The members in the interface have fixed modifiers
Member constants: Publicstatic final
member functions: Publicabstract
The members in the interface are public.
Interface inter{public static final int num = 3; public abstract void Show ();
Where Num's modifier, except int, can be stripped out.
interface inter{ int num = 3; void Show ();
Even if nothing is written, the modifiers of the members within the interface are not changed, that is, the modifier for NUM is public static final, and the modifier for the show method is public abstract.
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 of all the replication, the subclass can be instantiated, otherwise the subclass is an abstract class.
The relationship between interfaces and classes: Implementing implements
Interfaces can be implemented in many classes. Makes up for Java's inability to inherit multiple defects.
The reason why Java cannot inherit more:
If you say there are two classes:
class a{ int num = ten; void Show () { System.out.println (num); }} class b{ int num =; void Show () { System.out.println (num); }}
Another class C inherit these two, then call the Class C object's Show method, then the question is: Which class is called the Show method, the output num value is how much?
This is why Java has abandoned multiple inheritance.
Why more inheritance is not possible, more implementation can be?
This is because the member modifiers are fixed by the interface. Variables are constants, methods are abstract, there is no method body, concrete implementations are done by subclasses, so it does not cause confusion.
So what if there are the same methods in the two interfaces that are implemented more than one?
Or that sentence, the methods are abstract, the implementation of the specific subclass of the completion. Subclasses can overwrite a method with the same name in an interface just by completing a single replication operation.
Interface inter{public static final int num = 3; public abstract void Show (); Interface inter_b{public static final int num = ten; public abstract void Show (); Class Test implements inter,inter_b{public Void Show () { System.out.println (inter.num+ "\ n" +inter_b.num ); }} public class interface{public static void Main (string[] args) { new Test (). Show (); }}
A class can implement multiple interfaces while inheriting a class.
The specific form is as follows:
class A extends B implements c,d
Inherit first, then implement.
Where A and B are class names, and C and D are interface names
This means that the definition Class A inherits Class B and implements both interfaces C and D.
Interface inter{public static final int num = 3; public abstract void Show (); Interface inter_b{public static final int num = ten; public abstract void Show (); Class ress{ int x =; public void display () { System.out.println ("Res.show ()" +x);} } Class Test extends Ress implements inter,inter_b{public Void Show () { System.out.println (inter.num+ "\ n "+inter_b.num);} } public class interface{public static void Main (string[] args) { new Test (). Show (); New Test (). display ();} }
The relationship between interfaces and interfaces:
Between classes and classes is an inheritance relationship
The implementation relationship between a class and an interface
An inheritance relationship between an interface and an interface
Multiple inheritance exists between interfaces.
interface a{ int num = 0; void Show1 ();} interface b{ int num = 2; void Show2 ();} Interface C extends a,b{ void show3 ();} Class Test implements c{public void Show1 () {} public void Show2 () {} public void Show3 () {}}
Java---interface