Three characteristics of object oriented

Keywords: Java OOP

preface

This article introduces the three characteristics of object-oriented.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Encapsulation

1. Concept

It is one of the three characteristics of object-oriented (encapsulation, inheritance and polymorphism). It is the simulation of the objective world by object-oriented programming language. In the objective world, member variables are hidden inside the object, and the outside world can not be operated directly

2. Principles

Some information of the class is hidden inside the class. External programs are not allowed to access member variables directly. Instead, the operation of hidden information and access to member variables private are realized through the methods provided by the class, and the corresponding getXxx()/setXxx() methods are provided

3. Advantages

Control the operation of member variables through methods to improve the security of code; Encapsulate the code with methods to improve the reusability of the code

4. Steps

(1) Add modifiers to attributes

Modifiers are public, private, protected, and default. (see my previous blog for details)
The code is as follows (example):

class A{
	public String name;
	private int age;//The age modified by private can only be used in this class. If you want to use it, you must use the method we require to access it
}

(2) Construction method

The specific introduction of the construction method is mentioned in the concept of class and object. If you don't understand it, you can click here ---- > Construction method.

The code is as follows (example):

class A{
	public String name;
	private int age;
	public A(){}
	public A(String name,int age){
		this.name=name;
		this.age=age;
	}
}

(3) Provide set and get methods for properties

The set method is to provide a value to the object (generally used when providing and modifying values), and the get method is to obtain the value of the object. Therefore, the modifier of this method is generally decorated with public.
The code is as follows (example):

class A{
	public String name;
	private int age;
	public A(){}
	public A(String name,int age){
		this.name=name;
		this.age=age;
	}
	public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Tip: the set and get methods of the above construction methods and properties can be provided by IDEA, and the shortcut key is Alt+Inter. Constructor and Getter and Setter.

2, Inherit

1. Concept

Inheritance is one of the three characteristics of object-oriented. It can make subclasses have the properties and methods of the parent class, redefine them in subclasses, and append properties and methods.

2. Format

Class subclass extends parent class {}
The code is as follows (example):

public B extends A{}

The url used here is the data requested by the network.

3. Advantages and disadvantages

(1) Benefits

Improve code reusability (put the common attributes in several classes into one class) and Maintainability (when you need to modify methods, you only need to modify one instead of multiple)

(2) Disadvantages

It improves the coupling between codes. There is an association between a parent class and a child class. When the parent class changes, the child class also changes, because the child class contains all the methods of the parent class.

4. Access characteristics in inheritance

You can read the previous blog and click it ----------- > Inherited access characteristics

5. Considerations for succession

(1) Classes in Java only support single inheritance, not multiple inheritance

(2) Classes in Java support multi-layer inheritance

6. Cases

The code is as follows (example):

public class A {
    private String name;
    private int age;
    
    public void write(){
        System.out.println("Parent class A");
    }
}
public class B extends A{
    public int StudentNumber;

    public void read(){
        System.out.println("Subclass B");
    }
}
public class text {
    public static void main(String[] args) {
        B b = new B();
        b.read();
        b.write();//Subclasses can also access the methods of the parent class
    }
}

3, Polymorphism

1. Concept

The same object shows different forms at different times

2. Premise

(1) There should be inheritance or implementation relationship

(2) There should be method rewriting

(3) A parent class reference should point to a child class object

3. Characteristics of member visits

You can read the previous blog and click it ----------- > Inherited access characteristics

4. Advantages and disadvantages

Improve the scalability of the program. When defining a method, the parent type is used as a parameter. When using, the specific subtype is used to participate in the operation
 You cannot use subclass specific functions/member

5. Transformation in polymorphism

(1) Upward transformation

When a parent class reference points to a child class object, it is an upward transformation. For example: Animal a = new Cat(); 

(2) Downward transformation

Format: subtype object name = (Subtype)Parent class reference; For example: Cat c = (Cat)a;

6. Application

In the interface and in the abstract class: because the interface and abstract class cannot be instantiated, the interface and abstract class can be instantiated by referring to the polymorphic method.
Tip: the interface and abstract class will be explained later.

Posted by xengvang on Tue, 26 Oct 2021 04:31:57 -0700