Encapsulation Inheritance Polymorphism and Abstraction of Java Object Oriented Fundamentals

  • 2021-12-11 07:32:26
  • OfStack

Directory 1. Encapsulation 2. Inheritance 3. Polymorphism 4. Abstract Summary

Step 1 Package

Encapsulation is an important principle of object-oriented method, It is to combine the attributes and behaviors (data) of objects into an independent whole, and hide the internal implementation details of objects as much as possible, that is, to hide what you don't want to tell or shouldn't tell others, and to disclose what you can tell others. Others can only use the functions I provide to realize the requirements, but don't know how to realize them. Increase security


public class Person {
    private String name;
    private int gender;
    private int age;
    public String getName() {
        return name;
    }
    public String getGender() {
        return gender == 0 ? "man" : "woman";
    }
    public void work() {
        if (18 <= age && age <= 50) {
            System.out.println(name + " is working very hard!");
        } else {
            System.out.println(name + " can't work any more!");
        }
    }
}

The above Person class encapsulates name, gender, age and other attributes. The outside world can only obtain name and gender attributes of an Person object through get () method, but cannot obtain age attributes, but age attributes can be used by work () method.

Note that the gender attribute is stored using the int data type, and the encapsulation prevents the user from noticing this implementation detail. And if you need to modify the data type used by the gender attribute, you can do so without affecting the client code.

2. Succession

Inheritance: It is the most remarkable feature of object-oriented. Inheritance is to derive new classes from existing classes, which are called subclasses. Subclasses inherit the data attributes and behaviors of parent classes, and can expand new behaviors according to their own needs, thus improving the reusability of codes.


Animal animal = new Cat();

Inheritance should follow the Richter substitution principle, and subclass objects must be able to replace all parent class objects.

3. Polymorphism

Polymorphism: Allows different objects to correspond to the same 1 message. That is to say, the same 1 message can adopt many different behaviors according to different sending objects (sending messages is a function call). Encapsulation and inheritance are almost all prepared for polymorphism. During execution, the actual type of the reference object is judged, and its corresponding methods are called according to its actual type.

Polymorphism is divided into compile-time polymorphism and run-time polymorphism. Compile-time polymorphism mainly refers to overloading of methods, while run-time polymorphism refers to specific types pointed to by object references defined in programs that are determined at run time.


// Parent class 
public class Base {
    protected void show() {}
}
// Subclass 
class Kid: Base {
     public  void show() {
        System.out.println(" i am  kid");
    }
}

public static void main( String[] args )
    {
        Base base =  new Kid();
        base.show();
    }

4. Abstraction

Abstraction is to extract common and essential features from many things, while abandoning their non-essential features.

For example, apples, bananas, raw pears, grapes, peaches, etc., their common characteristic is fruit. The process of getting the concept of fruit is an abstract process.

To abstract, we must compare. Without comparison, we can't find common parts.

Common characteristics refer to those characteristics that can distinguish one kind of things from other kinds of things, and these distinguishing characteristics are also called essential characteristics.

Therefore, extracting the common features of things means extracting the essential features of things and abandoning different features.

Therefore, the abstract process is also a cutting process, and all the different and non-essential features are cut off.

The so-called common features are relative, which means that they are common from a certain facet.

For example, for cars and rice, from the perspective of buying and selling, they are both commodities and have prices, which is their common feature, but from other aspects, they are different.

Therefore, in abstraction, the similarities and differences depend on the angle from which to abstract. The angle of abstraction depends on the purpose of analyzing the problem.

In the process of software development, identifying stable requirements, identifying core requirements, identifying conceptual requirements, designing system architecture, defining interface relations among components in the system, etc. are all abstract processes, which reflect the essential characteristics of the system.

Abstract is stable and eternal. The opposite of abstract is concrete. People often say, "What you say is too abstract. Can you talk about one specific point?"

In a development language, there are abstract classes and concrete classes. Concrete classes can inherit from abstract classes and can be instantiated. Abstract classes can derive many different concrete classes.

The so-called: "1 gives birth to 2, 2 gives birth to 3, and 3 gives birth to everything". The stability of the system is reflected in the abstract class, and the change of the system is reflected in the concrete class. The level of abstract classes is higher than that of concrete classes.

The system is stable because of abstraction and vivid because of abstraction.

The abstract is decorated with the abstract keyword, and the class cannot be instantiated when it is decorated with abstract, which shows that abstract classes exist for inheritance,

If you define an abstract class without inheriting it, you create it for nothing, because you can't do anything with it. When you modify a method with abstract, this method is an abstract method, and the abstract method must exist in the abstract class. The abstract method has no method body.

For a parent class, If one of its methods is implemented in the parent class without any meaning, Different implementations must be carried out according to the actual needs of subclasses, so this method can be declared as an abstract method, and the modifier of abstract method must be public or protected, because private cannot be inherited by subclasses, and subclasses cannot implement this method, which defaults to public by default.

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: