Java Abstract class and interface, java Abstract class Interface
An important aspect of the object-oriented design process is how to abstract the elements in the "problematic space" and the elements in the "solution space" to establish an ideal one-to-one ing relationship. Abstract classes and interfaces are the products of abstract processes.
I. abstract classes
We know that objects are generated through classes, but not all classes can describe specific objects.
If a class does not contain enough information to describe a specific object, it becomes an abstract class. Abstract classes are the abstraction of objects with the same features but different representations in details. For example, birds are called, but different birds are called differently. They can abstract the concept of "call.
Define the following bird:
Public abstract class Bird {
Private String color;
Public String getColor (){
Return color;
}
Public void setColor (String color ){
This. color = color;
}
Public Bird (){
}
Public abstract void sing (); // tweet
}
// Magpie
Class Magpie extends Bird {
Public void sing (){
System. out. println ("I can sing in a whisper .");
}
}
Abstract class features:
(1) abstract classes are modified with abstract keywords
(2) abstract methods in abstract classes are modified with abstract keywords, and there is no method body (specific implementation ).
(3) abstract classes can contain non-abstract methods.
(4) abstract classes can not contain abstract methods (it makes no sense to design them into abstract classes ...), However, classes that contain abstract methods must be abstract classes.
(5) abstract classes are essentially classes and can only be inherited
(6) abstract classes cannot be instantiated, but cannot be new. As we have said before, it does not describe specific objects and cannot be instantiated.
(7) abstract classes can have instance variables and constructor Methods
Ii. Interfaces
An interface is a set of method features and a contract that specifies what you can do. The software design process depends on abstraction rather than concrete implementation.
Like the usb interface on our computer, whether you are a hard disk, a usb flash drive, or a mobile phone, you can connect your computer as long as you implement the interface specified by usb.
In the example of the abstract class above, I want to add a "Flying" function, considering that not all birds can fly, such as penguins, ostrich, and duck. What should I do?
Modifying abstract classes is definitely not appropriate: 1. Violation of the open and closed principles; 2: it will make all subclasses inherit the "Flying" function, we will see the ducks flying all over the sky ^_^.
We can define the following interface:
Public interface IFly {
Void fly ();
}
// Magpie can implement the interface in IBird:
Class Magpie extends Bird implements IFly {
Public void sing (){
System. out. println ("I can sing in a whisper .");
}
Public void fly (){
System. out. println ("I will fly! ");
}
}
Magpie has implemented the fly interface in IFly and can finally fly.
What if we need to add a swimming function for birds? Think for yourself...
Interface features:
(1) modifier: public, abstract, default (not written)
(2) Keyword: interface
(3) methods in interfaces are abstract methods and cannot be implemented.
(4) The methods in the interface are all public abstract by default, and must be modified using public in the Implementation class.
(5) All methods in the interface must be implemented in the implementation class (except the abstract class.
(6) the variables in the interface are public static final by default.
(7) A class can implement multiple interfaces.
Iii. application scenarios
1) abstract classes reflect the relationship of "is a". If some classes have a common implementation, an abstract class can be abstracted to implement common code, personalized methods are implemented by sub-classes.
2) interfaces reflect the relationship of "like a", which represents the abstract behavior of different classes of objects. For example, when both planes and birds fly, they can exit the flight interface, but they are not similar.
3) in software design, interface-oriented programming is particularly important when you need to separate interfaces from implementations and encapsulate changes.
For example, in the Ioc idea, the client does not care what type you are, and the specific object is injected by the container.
Another example is the interaction between two systems. A good design is that the two sides provide interfaces and do not care about internal implementation. This reduces coupling while encapsulating changes.
Many design principles, design ideas, and design patterns all reflect the importance of forward-to-Interface Programming: open and closed principles, interface isolation, dependency inversion, and adapter patterns.
Follow ginger to talk about technology, no.: helojava, or scan the following QR code.
One post per day, chicken soup for technical purposes.