Java: Object Oriented Understanding

object oriented

  Everything is an object. A program is a collection of objects that tell each other what to do by sending messages. That is to say: object-centric, driven by messages (sending a message is a function call). Objects have state, behavior, and identity.

  1. State: refers to the data members of the class, that is, attributes;
  2. Behavior: refers to the method members of the class;
  3. Identification: Refers to the unique address identification of each object in memory.

Four (3+1) features: encapsulation, inheritance, polymorphism, abstraction


1. Packaging

  That is, hide the hidden and expose the exposed. Hidden and public, refers to the way to hide the properties and implementation details of objects and only provide public access to the outside world.

  Benefits: Hide details, isolate changes; Ease of use; Improve reusability; Control access, improve security.

2. Inheritance

  The relationship from general to special is an extension relationship. The subclass object is a kind of parent class, which can also be called "is a" relationship. In Java, it is manifested as single inheritance of classes and multiple implementations of interfaces.

  • Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behavior of the existing class, and can extend new capabilities
  • Inheritance is achieved through extends
  • Inheritance improves code reusability and improves software development efficiency. Let the relationship between classes and classes, this is the premise of realizing polymorphism.

3. Polymorphism

  Refers to allowing different objects of the class to respond differently to the same message, that is, the same message can adopt a variety of different behaviors according to the different receiving objects (sending a message is a function call).

  The technique of implementing polymorphism is called dynamic binding , which refers to judging the actual type of the referenced object during runtime and calling its corresponding method according to its actual type.

  The role of polymorphism: to eliminate the coupling relationship between types.

  Three necessary conditions for polymorphism to exist

  1. inheritance or interface implementation;
  2. Override Override;
  3. The parent class reference points to the child class object.

The benefits of polymorphism:

  1. Substitutability: Polymorphism has the ability to replace existing code. A parent class reference can point to any subclass implementation.
  2. Extensibility: Polymorphism makes code extensible. Adding new subclasses does not affect the operation and operation of existing classes' polymorphism, inheritance, and other features. Adding subclasses adds different implementations.
  3. Interface-ability: Polymorphism is achieved by the superclass providing a common interface to subclasses through method signatures, which can be improved or overridden by subclasses.
  4. Flexibility: It reflects flexible and diverse operations in the application and improves the efficiency of use.
  5. Simplicity: Polymorphism simplifies the code writing and modification process of application software, especially when dealing with the operations and operations of a large number of objects, this feature is particularly prominent and important.

Polymorphism Reference: Understanding Java Polymorphism

Dynamic binding: the calling process is as follows

  1.  The compiler looks at the declared type and method names of the object.
  2. Next, the compiler will look at the parameter types provided when calling the method. If one of all methods with the same name exactly matches the supplied parameter type, this method is chosen. This process is called overloading resolution. The method's name and parameter list is called the method's signature. The return type is not part of the signature.
  3. If it is a private method, static method, final method or constructor, then the compiler will know exactly which method should be called, which is called static binding.
  4. When the program is running and the method is called using dynamic binding, the virtual machine must call the method of the class most suitable for the actual type of the multi-reference object with the object variable.
  5. The virtual machine pre-creates a method table for each class, which lists the signatures of all methods and the methods that are called. When overriding a method, the subclass method cannot be less visible than the superclass method. Dynamic binding is only for methods, not properties.


4. Abstraction

  Abstract: From a specific point of view, the thinking process of extracting the characteristics and behaviors we are concerned about from some existing things, thereby forming a new thing, is a way of thinking from complex to concise.

  • Abstraction of data structures: complex data types, hiding implementation details, exposing related operations.
  • Abstraction of Behavioral Methods

Interfaces and abstract classes  in Java . An interface can be seen as a protocol or convention. You can refer to the **Open/Closed Principle (OCP: Open/Closed Principle)**, that is [open for extension, closed for modification], the key is to achieve "abstract design"

Guess you like

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