Sometimes, we may want to construct a very abstract parent class object. It may only represent a classification or abstract concept, and its instance has no meaning, so we do not want it to be instantiated. For example, there is a parent class "Fruit", which has several sub-classes: "apple", "orange", and "banana. Fruit is only used as a classification here. Obviously, the fruit instance has no significance (just like if a person tells you that he has bought some fruit but does not tell you whether it is an apple or an orange, it's hard to imagine what he bought .). The fruit category needs to be quilt, which requires that we use abstract class to solve this problem.
In Java, a class can be defined as an abstract class by adding the abstract modifier before the class keyword. Abstract classes cannot be instantiated. For example:
Define abstract class fruit (fruit)
Public abstract class fruit {
......
}
If we try to use the following statement to obtain an instance, the compilation will fail.
Fruit fruit = new fruit ();
We can still construct sub-classes of the fruit class, such:
Subclass "apple )"
Public class Apple extends fruit {
......
}
Subclass "orange )"
Public class orange extends fruit {
......
}
So that we can achieve our goal.
Abstract classes can have the same attributes and methods as normal classes, and abstract methods ). For example:
Abstract class shape has the abstract method draw ().
Public abstract class shape {
......
Public abstract void draw ();
......
}
Abstract METHODS correspond to abstract behaviors. Generally, this behavior has no meaning to the parent object, while sub-objects have specific actions. For example, the method draw () has no significance for class shape, while the method draw () of the class shape subclass rectangle (rectangle () there can be actual actions (draw the four sides of the rectangle based on the four vertices of the rectangle), the method of sub-class circle draw () you can also draw a circle Based on the center and radius ).
Abstract classes can have abstract methods or no abstract methods. However, if a class has abstract methods, this class can only be defined as an abstract class.
If the following code class "shape" still has the abstract method draw () but is not defined as an abstract class, compilation will fail.
Public class shape {
......
Public abstract void draw ();
......
}
Another feature of an abstract method is that it forces the subclass to either remain abstract (that is, it does not implement the method and is still defined as an abstract class ), you can either display the behavior of this method (implement the specific action or throw an unsupportedoperationexception to indicate that this action is not supported ). This also strengthens polymorphism.
This section briefly analyzes abstract classes and describes interfaces ). Java uses the keyword interface to define an interface. An interface is an abstract object, which is even more abstract than an abstract class. All methods in the interface are abstract methods.
An interface can inherit other interfaces. A Class declares an interface through the implements keyword and implements the interface method.
For example, there is an interface interfacea,
Java code
- Public InterfaceInterfacea {
- VoidMethoda ();
- }
Class classa implementation interface interfacea.
Java code
- Public ClassClassa implements interfacea {
- Public VoidMethoda (){
- System. Out. println ("methoda of classa implements interfacea ");
- }
- }
If an abstract class implements an interface, it can be implemented by its subclass instead of the method that implements the interface (to keep its abstraction.
Abstract class classb implements interface interfacea, but there is no specific implementation method methoda (),
Java code
- Public Abstract ClassClassb {}
Subclass classbsub implements interface interfacea, but there is no specific implementation method methoda (),
Java code
- Public ClassClassbsub {
- Public VoidMethoda (){
- System. Out. println ("methoda of classbsub The subclass of classb ");
- }
- }
An obvious commonality between an interface and an abstract class is that both interfaces and abstract classes can have abstract methods.
The differences between interfaces and abstract classes are as follows:
(1) abstract classes can have instance variables, while interfaces cannot have instance variables. All variables in interfaces are static constants (final ).
(2) abstract classes can have non-abstract methods, while interfaces can only have abstract methods.
In Java, classes and classes cannot be inherited. The reason why Java bans multi-inheritance between classes is that multi-inheritance has great disadvantages.
Although multi-inheritance enables sub-classes to have features of multiple parent classes at the same time, its disadvantages are also significant. There are two main aspects:
(1) If an instance variable with the same name exists in multiple parent classes inherited by a subclass, The subclass will produce ambiguity when referencing this variable, unable to determine which parent class variable should be used. For example:
Class classa:
Java code
- Public ClassClassa {
- Protected IntVarsame = 0;
- }
Class classb:
Java code
- Public ClassClassb {
- Protected IntVarsame = 1;
- }
Subclass classc: (assuming that multi-inheritance between classes is allowed)
Java code
- Public ClassClasscExtendsClassa, classb {
- Public VoidPrintout (){
- System. Out. println (Super. Varsame );
- }
- Public Static VoidMain (string [] ARGs ){
- Classc =NewClassc ();
- Classc. printout ();
- }
- }
What is the running result of the above program? Output 0 or 1?
(2) If multiple parent classes inherited by a subclass have the same method and the subclass does not overwrite the method, ambiguity will occur when this method is called, the method of the parent class that should be called cannot be determined. For example:
Class classa:
Java code
- Public ClassClassa {
- Public VoidPrintout (){
- System. Out. println (0 );
- }
- }
Class classb:
Java code
- Public ClassClassb {
- Public VoidPrintout (){
- System. Out. println (1 );
- }
- }
Subclass classc: (assuming that multi-inheritance between classes is allowed)
Java code
- Public ClassClasscExtendsClassa, classb {
- Public Static VoidMain (string [] ARGs ){
- Classa =NewClassc ();
- Classa. printout (); // ------------------------- Row
- Classb =NewClassc ();
- Classb. printout (); // ------------------------- Row B
- Classc =NewClassc ();
- Classc. printout (); // ------------------------- Row C
- }
- }
What is the running result of the above program? Is the output of Rows A, B, and C 0 or 1?
Java prohibits a class from inheriting multiple parent classes because of the above fatal disadvantages. Fortunately, Java provides interfaces, it can also obtain many advantages of multi-inheritance through interface functions, and discard the disadvantages of Multi-inheritance of classes and classes.
Java allows an interface to inherit multiple parent interfaces, and also allows a class to implement multiple interfaces. Such multi-inheritance has the disadvantages mentioned above?
The answer is no. This is determined by the abstraction of the interface.
As described above, there cannot be instance variables in the interface, but only static constants, no specific methods (including method bodies), and only abstract methods, therefore, the disadvantages of Multi-inheritance are abandoned.
When a class implements multiple interfaces, because the interface only has abstract methods, the specific methods can only be implemented by the class that implements the interface, during the call, only the method that implements the class is always called (there is no ambiguity), so there is no second disadvantage of Multi-inheritance; and because the interface only has static constants, however, because static variables determine the call relationship during the compilation period, an error will be prompted during compilation even if there is a conflict. The class name or interface name is used directly to reference static variables, so as to avoid ambiguity, there is no first disadvantage of Multi-inheritance.
These disadvantages do not exist when an interface inherits multiple parent interfaces.
See the following example.
Interface:
Java code
- Public InterfaceInterfacea {
- IntLen = 1;
- VoidOutput ();
- }
Interface B:
Java code
- Public InterfaceInterfaceb {
- IntLen = 2;
- VoidOutput ();
- }
Interface sub inherited interfaces A and B:
Java code
- Public InterfaceInterfacesubExtendsInterfacea, interfaceb {}
Class XYZ implementation interface Sub:
Java code
- Public ClassXYZImplementsInterfacesub {
- Public VoidOutput (){
- System. Out. println ("output in class XYZ .");
- }
- Public VoidOutputlen (IntType ){
- Switch(Type ){
- CaseInterfacea. Len:
- System. Out. println ("Len of interfacea =." + type );
- Break;
- CaseInterfaceb. Len:
- System. Out. println ("Len of interfaceb =." + type );
- Break;
- }
- }
- Public Static VoidMain (string [] ARGs ){
- XYZ =NewXYZ ();
- XYZ. Output ();
- XYZ. outputlen ();
- }
The above Code does not have any problems, but if you try to write the following conflicting code, the compilation will fail.
Java code
- XYZ =NewXYZ ();
- IntLen = xyz. Len;
- System. Out. println (LEN );
The introduction of interfaces makes Java very flexible and also makes Java polymorphism more magical.