Home > Back-end >  Java interfaces
Java interfaces

Time:05-09

Interface: in miscellaneous drama of cats and dogs in addition to eating and sleeping dogs and cats can also count, and the cat and dog itself does not have the ability to calculate this special and
But can be trained to get the special ability to define in the animal is not appropriate, also not suitable for class definition to the dog and cat
, because only part of the cat and dog have this function, so in order to reflect the extensibility, Java provides the interface to define the
These additional features, does not give a specific implementation, the cats and dogs need to be trained in the future, only need to this part of the dog and cat to
This extra functions can,
The characteristics of the interface:
1, the interface with keywords interface modification
Format: interface interface name {
//...
}
2, the class implements the interface with implements said
Format: name of class class implements the interface name {
//...
}
3, the interface is not on the actual meaning of a class, it just says function extension
4, interface cannot be instantiated, but can use polymorphic way to instantiate
Example: the interface A {
Public abstract void method ();
}
Class B implements A {
Public abstract void method () {
System. The out. Println (" method ");
}
}
The class Demo {
Public static void main (String [] args) {
A=new B ();//upcasting
Arjun ethod ();//output results: method
}
}
5, the subclass of the interface
A, abstract class
B, a concrete class - need to implement (rewrite) interface all the methods in the
Members:
Member variables: can only be constant, and is static, the default modifier (if there is no system will default completion) : public
The static final
Constructor: interfaces - so no constructor cannot be instantiated
Members method: can only be abstract methods, the default modifier: public abstract
Ordinary classes, the difference between abstract class and interface:
1, members of the difference:
Normal class:
Member variables: can be variable, also can is a constant
Method: no arguments constructor, belt and constructor
Members of methods: can only be abstract
Abstract:
Member variables: can be variable, also can is a constant
Method: no arguments constructor, belt and constructor
Member methods: can be abstract, also can be abstract
Interface:
Member variables: can only be constant, and the static
Method: no
Member methods: can only be abstract, and the public modification (public)
2, the relationship between the difference:
1, class to class (including the abstract class) : the can only single inheritance, multilayer inherit relationship
Example: the class A {

}
Class B extends A {

}
Class C extends B {

}
2, the classes and interfaces (including abstract class) : relations, can be single, also can achieve more (
To implement all the interface methods) and, of course, they can in the case of inheriting a class
Now a single or multiple interfaces
Example: the interface A {
Public abstract void method1 ();
}
Interface B {
Public abstract void an ();
}
//implements A, B two interfaces, so all the way to realize A and B interface
Class C implements A, B {
Public abstract void method1 () {

}
Public abstract void an () {

}
}
3, interface and interface: inheritance relationships, can be single inheritance and multiple inheritance
Example: the interface A {
Public abstract void method1 ();
}
Interface B {
Public abstract void an ();
}
//single inheritance
Interface extends A C {

}
//multiple inheritance
Interface D extends A, B {

}
3, the difference between design concept:
Class (including abstract class) : be inherited reflects the "is... A "relationship, class or in the abstract class
Definition is the inheritance system of the common function of
Interface: implemented reflects the "like... A ", the relationship between the interface definition is the inheritance system of
The extension of the function
  • Related