Object-oriented understanding of abstract classes & interfaces

1. About object-oriented

1. What is object-oriented

    Before explaining object orientation, let's talk about process orientation. Students who have studied C know that C is a process-oriented language. So what is process-oriented? For example, to assemble the host, for process-oriented, you need to start from 0. Buy cpu, graphics card, motherboard, power supply, fan, and link these together through the motherboard. I need to know every step of the host assembly clearly.
    Introduced the process-oriented, and then will get to the point, what is object-oriented? For the above process of installing the host, the object-oriented will first abstract the host into a chassis, which contains cpu, graphics card, motherboard, and power supply. People who use the host don't care about how it works inside, and they don't need to know the internal logic. They only know that they can be used when they are plugged in. Object-oriented is to abstract some behaviors and attributes into an objectively existing entity.

2. Why object-oriented

    There are two reasons for object orientation:
    2.1 Computers help people to solve problems, but a computer is a machine after all, it will only follow the code written by people, execute it step by step, and finally get the result, so no matter how complicated the program is, the computer can always handle it easily, Structured programming is code written according to the computer's thinking, but when people see such complex logic, they cannot maintain and expand. Procedural programming as described earlier.
    2.2 Structural design is to design and construct the application system with the function as the goal. This approach leads us to map the objects in the real world into the functional modules of the application program when we design the program. This transformation process deviates from people’s observation and The basic idea of ​​solving problems. 
    It can be seen that when designing a system, structured design cannot solve the problems of reuse, maintenance, and expansion, and will lead to overly complex logic and obscure code. So people think, can you let the computer directly simulate the real environment, and use the human thinking, habits, and steps to solve the problem to design the corresponding application program? Such a program, when people read it, will be easier to understand, and there is no need to convert back and forth between the real world and the program world, so object-oriented is born. 

3. The benefits of object orientation

    Object-oriented solves the maintainability, extensibility, and reusability of the system.
   We all know that object-oriented has three major characteristics, encapsulation, inheritance and polymorphism. Then let's take a look at how these three characteristics solve the maintainability, scalability and reusability of the system.
    3.1 Encapsulation: Abstract some properties and behaviors into an entity, and encapsulate them into an object. This allows you to modify or extend the object without affecting other parts. Encapsulation addresses the maintainability of the system
    3.2 Inheritance: The subclass inherits the parent class and can inherit the methods and properties of the parent class. It realizes polymorphism and code reuse, so it also solves the reusability and extensibility of the system.
    3.3 Polymorphism: Eliminate the coupling between types. Inheritance allows an object to be treated as either its own type or its base type. Polymorphic method calls allow a type to behave differently from other similar types, as long as they are all derived from a uniform base class. This achieves maintainability and scalability.
 

2. Abstract classes and interfaces

1. Abstract class

Definition: A class that contains abstract methods is called an abstract class. If a class contains one or more abstract methods, the class must be qualified as abstract.
Understanding: Abstract classes are used to capture common features of subclasses. It cannot be instantiated and can only be used as a superclass of subclasses. Abstract classes are templates used to create subclasses in an inheritance hierarchy . Creating abstract classes and abstract methods is very useful because they make the abstraction of the class explicit and tell the user and the compiler how they intend to use them. Abstract classes are also useful refactoring tools because they allow us to easily move public methods up the inheritance hierarchy.
application:
    1. The abstract class declaration is decorated with abstract
    2. Abstract methods are also modified by abstract
 1 public class HttpServlet extends GenericServlet {
 2     void service(ServletRequest req, ServletResponse res) {
 3         // implementation
 4     }
 5  
 6     protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
 7         // Implementation
 8     }
 9  
10     protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
11         // Implementation
12     }
13  
14     // some other methods related to HttpServlet
15 }

 

2. Interface

Definition: An interface is a level of abstraction that is more abstract than an abstract class. is a collection of abstract methods. Interfaces only provide form, not any concrete implementation.
Understanding: An interface means "all classes that implement that particular interface look like this." Therefore, any code that uses a particular interface class knows which methods of that class can be called, and only needs to know those. Therefore, the interface is used to resume the agreement between the classes.
application:
    1.  The keyword for creating an interface is interface.
     2. An interface can also contain fields, but these fields are implicitly static and final.
    3.  Before Java 7, only methods can be defined in the interface, and there can be no specific method bodies. But in Java 8, we can also define default methods for interfaces through the keyword default .
     4. We can explicitly declare methods as public in interfaces, but they are public even if you don't.
  
1 public interface Externalizable extends Serializable {
2  
3     void writeExternal(ObjectOutput out) throws IOException;
4  
5     void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
6 }

3. Comparison

parameter abstract class interface
Default method implementation It can have default method implementation Interfaces are completely abstract. it doesn't have an implementation of the method at all
accomplish Subclasses use the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide implementations of all the methods declared in the abstract class. Subclasses use the keyword implements to implement the interface. It needs to provide implementations of all declared methods in the interface
Constructor Abstract classes can have constructors An interface cannot have a constructor
Differences from normal Java classes It is no different from a normal Java class except that you cannot instantiate an abstract class Interfaces are completely different types
access modifier Abstract methods can have public , protected and default modifiers The default modifier for interface methods is public . You cannot use other modifiers.
main method Abstract method can have main method and we can run it The interface doesn't have a main method, so we can't run it.
multiple inheritance Abstract methods can inherit from a class and implement multiple interfaces An interface can only inherit from one or more other interfaces
speed it is faster than the interface The interface is slightly slower because it takes time to find the methods to implement in the class.
add new method If you add new methods to an abstract class, you can provide it with default implementations. So you don't need to change your current code. If you add methods to an interface, then you must change the class that implements the interface.

4. Choose

  Having said all that, when should we use abstract classes and interfaces

  4.1 If you have some methods and want some of them to have default implementation, then use abstract class.

  4.2 If you want to achieve multiple inheritance then you have to use interface. Since Java does not support multiple inheritance , subclasses cannot inherit multiple classes, but can implement multiple interfaces. So you can use the interface to solve it

 4.3 If the basic functionality is constantly changing, then an abstract class is required. If you keep changing the basic functionality and using an interface, then you need to change all classes that implement the interface.

refer to:
Java Programming Ideas (4th Edition)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324949096&siteId=291194637