For example, creatures and people. At this time, creatures can be used as interfaces, people can be used as abstract classes that implement organisms, and men and women can be used as concrete implementation classes. Why can it be designed like this? Because biology is a very, very abstract concept, it is difficult for you to abstract its specific behavior (that is, specific method implementation). At this time, it can be used as an interface. Why can humans be used as an abstract class? Because it can have specific behaviors and actions. For example, you can crawl away, but men and women may have different sexual characteristics. At the same time, there may be some public behaviors but different behavioral characteristics. This can be used as an abstract method of an abstract class and let subclasses implement it. That's probably it.
For example, you have many similar objects (after thinking about it for a long time, other words are not suitable), whether they are people or things. Their attributes are almost the same, so you can abstract them into a class. For example, the car category has attributes such as wheels, engine, chassis, and shell. new an object to save the current attributes. You may want to save these attributes to the database, or you may want to display them to the user. The current object plays the role of saving data. Objects can not only save data, but also have their own methods, such as starting a car. Click a button to call the launch method of the current object. The methods of general classes will be related to changes in data. I am talking about general methods, not all. For example, the database connection class will not change the data, but only plays a role in connecting to the database. Interfaces are generally for external calls. You write a series of methods that ultimately accomplish a useful action. The very beginning of this series of methods is the interface, you can understand it this way. For example, in a simple login interface, after you enter the username and password, click OK to start calling the background login interface, and transfer the username and password to the server. The server needs a method to receive it, and after receiving it, it needs to verify the password, which is another method. After verification, a message is returned to the client. Login is successful or failed, and this series of actions is completed. Of course, you can complete these in a method. In short, the starting point is the interface. The above statement is relatively straightforward, but not entirely accurate.
Interface represents a capability/behavior. For example: if people can run and dogs can run, you can define the interface IRunable. Interfaces are a good way to achieve decoupling between components. Abstract classes are designed to achieve polymorphism and make the code more easily scalable.
Interfaces are more inclined to define a protocol for a group of classes, while abstract classes are already a step closer to implementation.
For example, abstract classes are used in the template method pattern to implement some common methods, while deferring different methods to subclasses for implementation.
To give a simple example, if you now want to abstract people from all countries, then you will think of abstracting it into an interface, and then there is a method called sayHello:
public interface Human{
String sayHello();
}
OK, now every object that implements this interface only needs to implement the sayHello method to represent people from various countries, and they have different ways of greeting each other.
But now that the business has become more complicated, you need these people to also have some general abilities, such as cry,walk,那么这个时候,你是不是应该考虑把Humanabstraction into an abstract class, like this:
public abstract class Human{
public abstract String sayHello();
public String cry(){
System.out.println("wu~wu~wu~wu~wu~");
}
public String walk(){
System.out.println("左右左 左右左");
}
}
To summarize, if there are only abstract methods, then use interfaces. If there are general concrete methods, use abstract classes
参考: The Java™ Tutorials :: Learngin The Java Language :: Interfaces and Inheritances :: Inheritances :: Abstract Methods and Classes :: Abstract Classes Compared to Interfaces
For example, creatures and people. At this time, creatures can be used as interfaces, people can be used as abstract classes that implement organisms, and men and women can be used as concrete implementation classes. Why can it be designed like this? Because biology is a very, very abstract concept, it is difficult for you to abstract its specific behavior (that is, specific method implementation). At this time, it can be used as an interface. Why can humans be used as an abstract class? Because it can have specific behaviors and actions. For example, you can crawl away, but men and women may have different sexual characteristics. At the same time, there may be some public behaviors but different behavioral characteristics. This can be used as an abstract method of an abstract class and let subclasses implement it. That's probably it.
For example, you have many similar objects (after thinking about it for a long time, other words are not suitable), whether they are people or things. Their attributes are almost the same, so you can abstract them into a class. For example, the car category has attributes such as wheels, engine, chassis, and shell. new an object to save the current attributes. You may want to save these attributes to the database, or you may want to display them to the user. The current object plays the role of saving data. Objects can not only save data, but also have their own methods, such as starting a car. Click a button to call the launch method of the current object. The methods of general classes will be related to changes in data. I am talking about general methods, not all. For example, the database connection class will not change the data, but only plays a role in connecting to the database.
Interfaces are generally for external calls. You write a series of methods that ultimately accomplish a useful action. The very beginning of this series of methods is the interface, you can understand it this way. For example, in a simple login interface, after you enter the username and password, click OK to start calling the background login interface, and transfer the username and password to the server. The server needs a method to receive it, and after receiving it, it needs to verify the password, which is another method. After verification, a message is returned to the client. Login is successful or failed, and this series of actions is completed. Of course, you can complete these in a method. In short, the starting point is the interface.
The above statement is relatively straightforward, but not entirely accurate.
Interface represents a capability/behavior. For example: if people can run and dogs can run, you can define the interface IRunable. Interfaces are a good way to achieve decoupling between components.
Abstract classes are designed to achieve polymorphism and make the code more easily scalable.
Interfaces are like 100% natural abstract classes.
Interfaces are more inclined to define a protocol for a group of classes, while abstract classes are already a step closer to implementation.
For example, abstract classes are used in the template method pattern to implement some common methods, while deferring different methods to subclasses for implementation.
To give a simple example, if you now want to abstract people from all countries, then you will think of abstracting it into an interface, and then there is a method called
sayHello
:OK, now every object that implements this interface only needs to implement the
sayHello
method to represent people from various countries, and they have different ways of greeting each other.But now that the business has become more complicated, you need these people to also have some general abilities, such as
cry
,walk
,那么这个时候,你是不是应该考虑把Human
abstraction into an abstract class, like this:To summarize, if there are only abstract methods, then use interfaces. If there are general concrete methods, use abstract classes
参考:
The Java™ Tutorials :: Learngin The Java Language :: Interfaces and Inheritances :: Inheritances :: Abstract Methods and Classes :: Abstract Classes Compared to Interfaces
Abstract classes are used for abstract things, interfaces abstract behaviors
This class defines unified operations and provides methods for objects