Java Object Oriented Features

Preface

1. Object-oriented features

1. Packaging

Encapsulation is a method of packaging and hiding the implementation details of abstract functional interfaces.
Encapsulation can be thought of as a protective barrier against random access of the class's code and data to code defined by an external class.
Access to code and data for this class must be controlled through a strict interface.
The main function of encapsulation is that we can modify our own implementation code without modifying the program snippets that call our code.
Proper encapsulation makes your code easier to understand and maintain, and also enhances its security.

1.1 Encapsulated Personal Understanding

  1. As far as possible, hide the specific internal implementation of the data, and the outside world cannot directly access the data for assignment and other operations.
  2. If the outside world wants to do such operations as assignment, it must modify the assignment operation by providing a public interface.

Advantages of 1.2 Packaging

  1. Reduce coupling: You can develop, test, optimize, use, understand, and modify independently to ease the maintenance burden. You can be more easily understood by programmers and debug without affecting other modules
  2. Effectively tuning performance can reduce the risk of building large systems by profiling to determine which modules affect the performance of the system, improving software reusability
  3. Even if the entire system is unavailable, these stand-alone modules may be available

Specific examples of 1.3 encapsulation

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!");
        }
    }
}

2. Inheritance

  1. Inheritance is a cornerstone of java's object-oriented programming technology because it allows you to create hierarchical classes.
  2. Inheritance is the inheritance of the parent's characteristics and behavior by a child class, so that the child class object (instance) has the instance domain and method of the parent class, or that the child class inherits the method from the parent class, so that the child class has the same behavior as the parent class.

2.1 Personal Understanding of Inheritance

Inheritance implements the IS-A relationship, that is, the subclass is an implementation of one of the parent classes
Inheritance can be used when methods or attributes of the parent class are not sufficient and need to be extended on top of the parent class
Inheritance requires adherence to the Richter Replacement Principle
So-called Richter Replacement Principle
Personally, it's a norm.
First, the subclass tries not to change the parent's things.
But you can add something on top of the parent class
When overloading parent methods, parameters are looser than the parent
Return values are stricter than parent classes

Upward Transition
(Cats can be used as Animal s, that is, they can be used to reference Cat objects. Parent references to subclass objects are called up-conversion.

Animal animal = new Cat();

)

3. Polymorphism

Polymorphisms are divided into compile-time and run-time polymorphisms:

Compile-time polymorphism refers primarily to overloading of methods
Runtime polymorphism refers to the specific type of object reference defined in a program that is not determined until the runtime

Runtime polymorphism has three conditions:

inherit
Overlay (Rewrite)
Upward Transition

/**In the code below, the Instrument class has two subclasses: Wind and Percusion, which both override the parent's play() method and use the parent Instrument in the main() method to reference Wind and Percusion objects. When Instrument references a call to the play() method, play() of the class in which the actual reference object is located is executed.Method, not method of Instrument class.
**/
public class Instrument {
    public void play() {
        System.out.println("Instument is playing...");
    }
}

public class Wind extends Instrument {
    public void play() {
        System.out.println("Wind is playing...");
    }
}

public class Percussion extends Instrument {
    public void play() {
        System.out.println("Percussion is playing...");
    }
}

public class Music {
    public static void main(String[] args) {
        List<Instrument> instruments = new ArrayList<>();
        instruments.add(new Wind());
        instruments.add(new Percussion());
        for(Instrument instrument : instruments) {
            instrument.play();
        }
    }
}

Tags: Java

Posted on Tue, 28 Sep 2021 12:30:54 -0400 by brotherhewd