Object-oriented programming

Before we introduce object-oriented programming, let’s take a look at two very important, very basic concepts: classes and objects.

A class is a collection of objects with equal functionality and the same properties — abstraction

An object is an instance of a class — concrete

/** * Girlfriend */
public class GirlFriend {
    /** * name */
    private String name;
    /** * age */
    private int age;
    /** ** gender */
    private String sex;
    
    public void eat(String value){
        System.out.println("My girlfriend" + name + "Like to eat"+value);
    }

    public void buy(String value){
        System.out.println("My girlfriend" + name + "Like to buy"+value);
    }
	// omit get set...
}
Copy the code
/** ** girlfriend */
// Create an object
	GirlFriend girlFriend = new GirlFriend();
// Assign a value to the attribute
    girlFriend.setAge(18);
    girlFriend.setName("Baby");
    girlFriend.setSex("Female");
Copy the code

Objects are real in memory and can store data. Objects are created using a template called class. There is only one class and multiple objects can be created.

Object-oriented programming is a way of programming. It takes class or object as the basic unit of code organization, and takes encapsulation, abstraction, inheritance and polymorphism as the cornerstone of code design and implementation.

Generally speaking, surface common object programming has nothing to do with technology, but is just a kind of programming method or programming idea. When programming, first rely on clear what is needed, and then create a class, and set the required attributes and methods, when using directly call.

Process-oriented programming

Process oriented is also a programming style or programming thought, which takes process (can be understood as method, function, operation) as the basic unit of organization code, and data (can be understood as member variables, attributes) and method are separated as the main feature. Process-oriented style is a process-oriented programming style that manipulates data to accomplish a function by stitching together a set of sequentially executed methods.

Process orientation is different from object orientation

Process oriented is to analyze the steps needed to solve the problem, and then use the function to achieve these steps step by step, when using one by one can call; Object orientation is to decompose the transaction that constitutes the problem into various objects. The purpose of establishing objects is not to complete a step, but to describe the behavior of something in the whole step of solving the problem.

What are the advantages of object-oriented programming over procedural programming?

  • For large-scale complex program development, the program processing flow is not a single main line, but a complex network structure. Object-oriented programming can handle this complex type of program development better than procedural programming.
  • Object-oriented programming has more features (encapsulation, abstraction, inheritance, polymorphism) than procedural programming. The code written by using these features is easier to expand, reuse and maintain.
  • From the evolution of the way that programming languages deal with machines, we can conclude that object-oriented programming languages are more humane, more advanced and more intelligent than procedural programming languages.

Four major features

encapsulation

We know that you can define properties and methods in a class. When some information is not intended for others to see or modify, we can expose a limited access interface in the class and grant external access to internal information or data only through the methods provided by the class.

Permission modifiers private, Default, protected, and public are provided in Java to support encapsulation of this feature.

The meaning of encapsulation:

  • Improve code maintainability by hiding data information and protecting it from arbitrary modification;
  • Exposing limited and necessary interfaces improves the ease of use of classes.

inheritance

Inheritance is the derivation of a new class from an existing class, which can absorb the data attributes and behavior of the existing class and extend the new capabilities. For example, guns are a class. Rifles, pistols and sniper rifles all have the attributes and abilities of guns, but they also have their own unique abilities.

One of the biggest benefits of inheritance is code reuse.

A subclass implements one or more interfaces while inheriting a class: be sure to inherit before you implement

Why Java does not support multiple inheritance

Multiple inheritance has side effects: the diamond problem (diamond inheritance). If B and C inherit from A and overwrite the same method of A, and D inherit from B and C, then D will inherit from B and C. Which method of A will D inherit from B and C? This is where ambiguity arises.

Principle of inheritance

In Java, classes are loaded dynamically, the first time the class is used. If a subclass object is not created before it is created, the JVM creates a subclass object and stores it “inside the subclass object space” so that the subclass object can directly access the properties of the subclass object inside it.

abstract

An abstract class is a collection of common attributes of all its subclasses, and is a class that contains one or more abstract methods. Abstract classes can be thought of as further abstractions of classes.

Interface classes are all about hiding the implementation of a method so that the caller only needs to know what the method provides, not how it is implemented.

Abstraction can be implemented through interface classes or abstract classes, but no special syntax mechanism is required to support it.

In the Java language, classes modified with the abstract keyword are abstract classes, and classes modified with the interface keyword are interface classes.

Abstract class properties

Abstract classes are not allowed to be instantiated, only inherited

Abstract classes can contain properties and methods

Subclasses inherit from abstract classes and must implement all of the abstract methods in the abstract class

Interface Features

Interfaces cannot contain attributes (member variables)

Interfaces can only declare methods, and methods cannot contain code implementations

When a class implements an interface, it must implement all methods declared in the interface

Application scenario differences between abstract classes and interfaces

To solve the problem of code reuse, abstract classes are used;

To solve the problem of abstraction rather than code reuse, interfaces are used.

Abstract the meaning of existence

  • Improve the extensibility and maintainability of the code, modify the implementation does not need to change the definition, reduce the scope of code change;
  • Easy to deal with complex systems, can effectively filter out unnecessary attention to information.

polymorphism

Polymorphism refers to multiple forms of the same entity at the same time, that is, a reference to a superclass type refers to one of its subclasses.

/** * The application of polymorphism */
List<String> strArr = new ArrayList<>();
List<String> strLink = new LinkedList<>();
Copy the code

Polymorphism can be implemented in Java through inheritance and interfaces.

Polymorphic characteristics:

  • Access member variables: Only member variables of the parent class can be accessed, not subclass-specific members
  • Call the same method: Only subclass methods can be called

Significance of the existence of polymorphism:

Polymorphism can improve the extensibility and reuse of code, and is the code implementation basis of many design patterns, design principles, and programming skills.