What are the characteristics of Java object-oriented

topic

Three characteristics of object-oriented: encapsulation inheritance polymorphism

Object-oriented programming is an idea of ​​programming using classes and objects. All things can be classified . A class is a high degree of abstraction for things in the world. Different things have different relationships. The encapsulation relationship between a class itself and the outside world, the inheritance relationship between a parent class and a subclass, and the relationship between a class and multiple classes. Polymorphic relationship. Everything is an object , and an object is a specific world thing. The three major characteristics of object-oriented are encapsulation, inheritance, and polymorphism. Encapsulation, encapsulation describes the relationship between a class's behavior and attributes and other classes, low coupling, high cohesion; inheritance is the relationship between parent classes and subclasses, and polymorphism refers to the relationship between classes and classes.

Encapsulation hides the internal implementation mechanism of the class, which can change the internal structure of the class without affecting the use, and also protects the data. Its internal details are hidden from the outside world, and only its access methods are exposed to the outside world. Attribute encapsulation: users can only access data through pre-customized methods, and can easily add logic control to limit unreasonable operations on attributes; method encapsulation: users call methods in a predetermined way, and do not need to care about methods Internal implementation, easy to use; easy to modify, enhance the maintainability of the code;

Inheritance is a new class derived from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can extend new capabilities. In essence, it is a special-general relationship, which is often referred to as an is-a relationship. The subclass inherits the superclass, indicating that the subclass is a special kind of superclass and has some properties or methods that the superclass does not have. A base class is abstracted from multiple implementation classes, so that it has the common characteristics of multiple implementation classes. When the implementation class inherits the base class (parent class) with the extends keyword, the implementation class has these same properties. Inherited classes are called subclasses (derived or superclasses), and inherited classes are called parent classes (or base classes). For example, an animal class can be abstracted from cats, dogs, and tigers, which have common characteristics (eating, running, barking, etc.) with cats, dogs, and tigers. Java implements inheritance through the extends keyword. The variables and methods defined by private in the parent class will not be inherited, and the variables and methods defined by the parent class through private cannot be directly manipulated in the subclass. Inheritance avoids the repeated description of common features between general classes and special classes. Through inheritance, the range of concepts to which each common feature fits can be clearly expressed, and the attributes and operations defined in general classes are adapted to the body of this class. and all objects of each special class below it. Using the inheritance principle makes the system model more concise and clear.

Compared with encapsulation and inheritance, Java polymorphism is the more difficult one of the three major features. Encapsulation and inheritance ultimately come down to polymorphism. Polymorphism refers to the relationship between classes and classes. Two classes are inherited. There are methods. , so that the parent class reference can point to the subclass object when calling. There are three essential elements of polymorphism: inheritance, overriding, and the parent class reference points to the child class object.

accumulation of knowledge

Glossary

1. Encapsulation

Encapsulation refers to attribute privatization

Provide setter and getter methods to access properties as needed, hide specific properties and implementation details, and only open the interface to control the access level of properties in the program.
Purpose: To enhance data security, not allow other users to freely access and modify data, simplify programming, users do not need to care about the specific implementation details, but can only access the members of the class through the external interface.

public class Person {
    
        int age;    String name;    public int getAge() {
    
            return age;    }    public void setAge(int age) {
    
            this.age = age;    }    public String getName() {
    
            return name;    }    public void setName(String name) {
    
            this.name = name;    }}



2. Inheritance (Extend)

Inheritance refers to extracting multiple identical properties and methods and creating a new parent class

A class in java can only inherit one parent class, and can only inherit access rights to non-private properties and methods
. Subclasses can override the methods in the parent class and name the properties with the same name as the parent class
Purpose: code reuse

3. Polymorphism

Polymorphism can be divided into two types: design-time polymorphism and runtime polymorphism

Design (compile) time polymorphism: overloading (Overload), which means that java allows the same method name but different parameters (the return value can be the same or different), and one or more functions with the same name are allowed in the same class. As long as the parameter type or the number of parameters are different

Runtime polymorphism: Override must be in the inheritance system, the subclass overrides the parent class method, and the JVM runtime decides which method to call according to the type of method that is called

Notice

1. In java, the inheritance relationship should be minimized to reduce the coupling degree.
2. When using polymorphism, when the parent class calls a method, the method of the child class is called first. If the child class does not override the method of the parent class, the parent class will be called again. Class method
3, java access controller scope table:

this class this package Subclass External packaging
public T T T T
protected T T T F
default T T F F
private T F F F

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324130771&siteId=291194637