In this paper, I will explain some specific concepts related to object-oriented programming at the conceptual level, and re-understand inheritance, polymorphism and encapsulation, based on my understanding of the selected text of “Ideas of Java Programming” (hereinafter referred to as I) and “Design Patterns – Foundations of Reusable Object-oriented Software” (hereinafter referred to as II).

  • What is an object

Problem space - where problems occur Solution space - where problems are modeledCopy the code

Elements in the problem space and their representation in the solution space become “objects”. It has state, behavior, and identity.

  • Behavior – What actions (methods) can be applied to an object
  • State – How does the object respond after the operation is applied
  • Identification – how to identify different objects with the same behavior and state

Object-oriented programming allows problems to be described in terms of the problem, rather than the computer that solves it.

C, for example, needs to be considered based on the structure of the computer, not the structure of the problem to be solvedCopy the code
  • First, find the right partner

Object-oriented programs consist of objects, which include data and the procedures that operate on that data, often called methods or operations. Object performs an action upon receiving a request or message from a customer. like

int a = obj.f();
Copy the code

The act of calling a method is called sending a message to an object. Where f() is the message sent and obj is the object that receives the message.


So when you talk about sending a message to an object, you usually mean calling a method on an object.

A client request is the only way for an object to perform an operation, which in turn is the only way for an object to change its content data.

In this statement, the unique method refers to way, which can be understood as the unique way, and the operation is operate or method, which is used to send messages to objects.Copy the code

This leads to the concept of encapsulation, where functionality and data are encapsulated together to give an appropriate representation of the idea of a problem space without being constrained to use the underlying machine language to correlate.

Data is hidden and the state of the object (data) can be changed by the corresponding operation 1. Encapsulated data is inaccessible to the client programmer -- data that is necessary for internal operations but not of user concern for problem solving; 2. Encapsulation allows library or interface designers to change internal implementation logic without worrying about external impact. Regardless of the language features, from the understanding of the basic concept of encapsulation as above. What is called access in different languages is just an extension or extension of this conceptual level.Copy the code
Classes designed in object-oriented programming usually don't exist in real life. Some are low-level classes like arrays, and some are higher. Models that strictly reflect the current world do not produce systems that reflect the future world, so some degree of abstraction is required (which can be improved by studying design patterns, etc.)Copy the code
  • Second, object granularity

Different abstractions result in the size and number of objects in an object-oriented system and the complexity of the system. Objects can be used to represent anything from the hardware down to the entire application, so how you design them is critical.

  • Three, interfaces,

  • 3.1 type structure

Object each operation declared specifies the name of the operation, the object as a parameter, and the return value. These are called configurations of an operation.

  • 3.2. The interface

The collection of all the operational configurations that an object can define (perform) is called its interface. An interface describes the set of requests that the object can accept, and any request (message) that matches the medium configuration of the object’s interface can be sent to the object

The Java keyword interface can be understood as a further abstraction of the concept in Java itself, specifically a syntactic sugar, which is a bit of a special case, not to be confused with the conceptual level of interface. According to the interface concept above, each object has its own interface (a collection of constructs) that can be used to manipulate the object's data. Here is for each object, rather than a class, here don't into the concept of specific languages such as Java, because Java is a strongly typed language, some words may be an object is a class (the last half sentence may have a problem here, emphasizes the strange language, do not use a specific language points and concept of confusion).Copy the code
  • 3.3. Type-type

The name used to represent the interface. If an object accepts all operation requests defined by the “Window” interface, the object has type “Window”. An object can have multiple types, and different objects can share a type.

An object may be of type Window as well as Door, such as a Door object. Different objects can have the same Window type, such as a Window object or a house Window object. One part of an object's interface can be described by one type, while the other parts can be described by other types. The fact that two objects have the same type indicates that they share some or all of their interfacesCopy the code

When an interface of type A contains an interface of type B, type A is said to inherit from type B, where A is A subtype and B is A supertype.

Sub and super are translation problems, directly refers to sub and supCopy the code
  • 3.4. Dynamic Binding (Late binding)

Interfaces are a fundamental part of object-oriented design. Because of encapsulation, you can only operate on an object through its interface; otherwise you can’t know or manipulate anything about an object. But the interface and implementation of an object are separate, and different objects can have different implementations.

The implementation here refers to Implement, but distinguish the Java keyword for example: the Window and house Windows in the preceding example both have Window opening and Window closing operations, which are of the same Window type. But in ordinary life, the window may slide left and right, while the window of a house moves inside and out.Copy the code

So the actual result or side effect of sending a message to an object depends on the object receiving the message.

The connection between the request sent to an object and the run-time of its corresponding operation becomes a dynamic binding.

Extended pre-binding - a function call generated by a compiler that is not object-oriented programming resolves at run time to the absolute address of the code to be executed.Copy the code
  • 3.5. Polymorphism

Because of dynamic binding, for client code, it is possible to design a general program that only requires that the called object satisfy a certain type, and the specific effect is determined by the specific object, thus simplifying the definition of client. At the same time, dynamic binding allows an object to be replaced at run time, as long as the new object satisfies a particular interface (type). This substitutability is polymorphic.

Client code is understood as code that sends a message to an object, such as Main in a simple demo. The general program refers to that all the customers need is the type of the object, and they do not need to care about which specific object is, that is, they do not need to care about the realization of the object.Copy the code
  • 3.5.1 Upcasting – Upcasting

Even among different objects of the same class, the processing logic of the same message may be different, and the behavior of objects corresponding to points of different subtypes may be quite different. When you design a general program, you can only care about the parent type, not whether the specific object is an object of that parent type or a child type. If the object is of a subtype, then an upward transformation has taken place. Moving up makes client code more extensible, because it works well with whatever objects are available at run time.

  • 3.6. Objects provide services – high cohesion

Programs themselves provide services, and programs run on services provided by objects. In good object-oriented design, each object can do a good job, but it’s not trying to do too much.

  • 4, class – class

Classes describe collections of objects that have the same properties (data elements) and behavior (functionality). In other words, classes specify the internal data and representation of the object, and colleagues specify the operations that the object can perform.

A type is related to a class, but note that the type determines the interface, and the class is a specific implementation of that interfaceCopy the code
  • 4.1 instantiation

An object is created by instantiating a class, which is called an instance of that class. Instantiating a class involves allocating storage space to the object’s internal data and associating operations with that data.

In many cases, objects = instances, sometimes even called instance objects. But in my opinion, object is a much broader concept, you instantiate a class and get an instance. For example, humans are classes, and each of us is an instance. Each person (instance) is a concrete object of humanity.Copy the code
  • 4.2 inheritance

New classes can be derived from existing classes by inheritance. New classes are called subclasses. Subclasses contain all data and operations defined by the parent class; Subclass instance objects contain all subclass and superclass defined data types, and can perform all operations defined in the superclass.

Type equivalence - By inheritance, a message sent to a superclass object can also be sent to a subclass object, that is, the subclass and the superclass have the same type.Copy the code
  • 2 an abstract class

Classes describe collections of objects that have the same properties (data elements) and behavior (functionality) (see above). An abstract class is the abstract parent of some concrete class. Shape, for example, is only a concept in reality. There are subclasses of Circle, Triangle, and Rectangle, and each concrete class has various objects. From a conceptual point of view, idle classes cannot be instantiated. There is no Shape object.

Abstract classes defer the implementation of some or all of their operations to subclasses, that is, some or all of the methods they define have no method body, only type. These methods are called abstract methods. Further, the abstract class cannot be instantiated because some behavior has not been defined.

  • 4.2.2 Differences in the parent classes of subclasses

1. Add new data types and methods to the subclass (the type of the subclass is not exactly the same as the parent class); 2. Re-create the parent class’s methods so that the child class behaves differently when calling the method at run time.

Substitution principle - subclass only overrides the methods of the parent class. Only subclass objects can completely replace the parent class. This is a pure substitution, i.e. subInstance is supInstance changes the interface of the subclass. So subInstance is like supInstance.Copy the code

To determine inheritance is to determine whether it can be described by “is A” and make it have practical meaning.

  • 4.2.3 Class inheritance and interface inheritance

Object class differs from object type:

- "class" defines how an object is implemented, including its internal state and implementation of operations. - Type is related only to the interface, that is, the set of requests that the object can respond to. An object can have multiple types, and objects of different classes can have the same type.Copy the code

But there is a connection:

A class defines the operations that an object can perform, in other words, the type of the object. If an object is an instance of a class, it supports an interface defined by that class.Copy the code

Due to the differences and connections at the conceptual level, there are also differences in inheritance:

- Class inheritance defines the implementation of one object based on the implementation of another. It is a sharing mechanism for code and presentation. - Type (interface) inheritance describes when an object can be used to replace another object - polymorphism and upward transition are involved here - note that although the two are different, most programming language levels do not clearly distinguish between the two. Class inheritance can also be a type (interface) inheritance.Copy the code

Inheritance in Java

As mentioned in II, 1.C++ pure interface inheritance is close to common inheritance pure abstract class, and pure implementation or pure class inheritance is close to private inheritance; Smalltalk inheritance is implementation-only inheritance (Smalltalk is a dynamic language, and the compiler does not do compile-type checking. It only checks if an object implements a message, and implementation-only inheritance follows. Given that object A and object B both have A method f(), even though the two objects are completely unrelated at the real level, the language level is still considered to be interchangeable at run time, i.e. the same type. - Java interface and pure abstract class can be compared to C++ pure abstract class, so in Java, there is a distinction between type inheritance and class inheritance. Interfaces generally have only interface definitions (types), so the extend relationship between Implement and interface can fall under the "interface inheritance" described above. -Java's pure class inheritance can be understood as extending a new class from a concrete class -- see above, a shared mechanism for code and presentation.Copy the code
  • 4.3 Program for interfaces, not implementations

Class inheritance is a basic mechanism for extending the functionality of applications by reusing the functionality of their parent classes. New objects can be defined quickly from old objects. Another benefit of inheritance is that it allows you to define families of objects of the same type, which is the nature of polymorphism.

Use the concepts above to understand the concepts of interfaces and implementations so that this important principle can be understood properly, rather than being confined to the specific syntax of one language. This principle is the basic principle for writing reusable object-oriented programs in general. Instead of declaring a variable as an instance object of a particular concrete class, let it conform to the interface defined by the abstract class.

Benefits - Reduced interdependence prior to subsystem implementation. - 1. The customer does not need to know the specific type of object to be used, but only needs to care about the expected common interface; - 2. The customer doesn't need to care about the implementation, just the abstract class that defines the interfaceCopy the code
  • Five, the reuse

  • 5.1 Combination, aggregation and acquaintance

Composition - aggregation can be composed of any number of existing objects of any type in any way that enables the functionality of a new class - composition that occurs dynamically, meaning that one object owns or is responsible for another object. An object is said to contain or be part of another object. The key is that the aggregated object and the owner have the same declarative cycle acquaintance -- meaning that one object knows about the other, and acquaintance objects may request each other's operations, but the other is not responsible for understanding: Composition - a code level representation of what a new class needs to be combined together, such as the Engine aggregation required by Car - a dynamic composition, so that a specific Engine object can be understood as part of a specific Car at run time. Runtimes can dynamically replace Car objects with engines that are not responsible for each other, so for example an Engine run would need to start and run using a method that uses gasoline. The gasoline object could have a "burn()" method, in which case the Engine would use the gasoline burn, but the two are acquaintedCopy the code
  • 5.2 Comparison of Inheritance and Combination

  • 5.2.1 inheritance

Due to inheritance, the internal details of the parent class are visible to the subclass, also known as “white box reuse.”

  • 5.2.2 combination

The combined objects do not know the details of each other, requiring the combined objects to have well-defined interfaces, also known as “black box reuse.”

  • 5.2.3 requires more

Inheritance provides a way to reuse classes that are statically defined at compile time, can be used directly, and can be overridden or added to quickly change the implementation to produce a usable class. However, since the structure of inheritance is determined in static code, it is not possible to change this inheritance system dynamically at run time; In addition, inheritance exposes the implementation details of its parent to subclasses, destroying the “encapsulation” of the parent class. In addition, any changes to the parent class will cause the subclass to change, and the subclass may need to rewrite the parent class or derive a new subclass if it does not meet the requirements of the problem. This dependency limits reusability to some extent. The alternative is to inherit only abstract classes.

Composition is defined dynamically at run time by obtaining references to other objects. It requires objects to comply with each other’s interface conventions that do not prevent the substitution of objects in use. Because composed objects can only be accessed through interfaces, encapsulation is not broken, and dependencies can be reduced by dynamic substitution between objects. Composition helps keep each class encapsulated and focused on a single task. This keeps classes and class hierarchies small and unlikely to become uncontrollable behemoths. Based on composition, there are more objects in the design, and the operation of the system depends on the relationship between objects rather than being defined in a class.

Object composition is preferred over class inheritance.

In general, you should consider using existing components to compose new ones, creating new capabilities by assembling existing capabilities. But the actual artifacts are not rich enough, and inheritance is simpler than composition. Therefore, combination and inheritance should be used together, so that they complement each other.Copy the code
  • 5.2.4 commissioned

Delegation is a composition that gives composition the same reuse capability as inheritance.Copy the code

The same reuse capability here means that when inherited, the inherited operation can always refer to the object receiving the request. That is, when a subclass object calls an operation inherited from its parent (that is, when an inherited message is sent to the subclass object), the subclass object actually performs the operation, so when overwriting occurs, polymorphism occurs. Delegating means letting the principal get a reference from the principal. Here, the principal is the object visible to the client, which will “itself” to the principal, so that the principal can obtain the principal object in the entrusted operation.

For example, the Window class has a Shape component. Shape specifies the Shape of the Window, and the area of the Window is delegated to Shape. Here, area() of Window is the message requested by the customer, while W calls area() of S, and w passes itself to S as a parameter. The pseudo-code is as follows: customer code: w.rea (); Public class Window {... private Shape s; public double area() { s.area(this); }} Here we can make an analogy, Window is obtained by combining Shape, corresponding subclass (Sub) inherits parent (Sup), for code Sup Sup = new Sub(); sup.operate(); The subclass object is actually executed, but sup.operate() is called as an interface to the parent class, but the subclass object is actually referenced. The Window subclass and Shape subclass need to get a reference to the w object when s.rea () is executed. Get "same reuse as inheritance"!Copy the code

Dynamic, highly parameterized software is more difficult to understand than static software, and it also has some inefficiencies. So delegation is a good choice only if it makes the design simpler, not more complex, and its efficiency is also related to the actual context.

  • 5.3 Design Mode

A deeper understanding of reuse requires learning design patterns.

  • 5.4 Parameterized Type

Parameterized types are either generic (Ada, Eiffel) or template (C++) or generic (Java)Copy the code
  • Summary of the characteristics of object-oriented language

1. Everything is an object. Directly map things in the problem space to 2 in the solution space. Programs are collections of objects that send messages telling each other what to do. Different objects work together to send messages to each other in various ways to solve problems (mimicking problem space solutions) 3. The complexity of complex systems can hide behind object simplicity 4. Every object has a type 5. All objects of a particular type of object can receive the same message. This is at the heart of inheritance and polymorphismCopy the code