java Foundation_ object-oriented

Three characteristics, encapsulation, inheritance, polymorphism

encapsulation

Abstract data types are used to encapsulate data and data-based operations to form an indivisible independent entity. The data is protected inside the abstract data type, the internal details are hidden as much as possible, and only some external interfaces are reserved to make it contact with the outside. Users do not need to know the internal details of the object, but can access the object through the interface provided by the object

Vernacular understanding:
Protect the internal data of the object. Only some external data interfaces are reserved. There is no need to care about the internal implementation!

advantage:

  1. Reduce coupling: it can be developed, tested, optimized and used independently
  2. Reduce maintenance burden
  3. High availability: it is encapsulated and can be used elsewhere without rewriting the logic code step by step
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!");
        }
    }
}

It can be clearly seen from the person class that the name, gender and age attributes are encapsulated internally. The outside world can only obtain the value of this attribute through getname and GetGender, regardless of how the internal implementation is implemented.

Note that the gender attribute is stored using the int data type, and the encapsulation makes the user unaware of this implementation detail. When the data type used by the gender attribute needs to be modified, it can also be done without affecting the client code.

inherit

Inheritance implements the IS-A relationship. For example, Cat and Animal are a kind of IS-A relationship. Therefore, Cat can inherit from Animal to obtain non private attributes and methods of Animal.

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

Cat can be used as Animal, that is, cat objects can be referenced with Animal. A parent class reference to a child class object is called an upward transformation.

Animal cat = new Cat()

advantage:
1. Improve the reusability of the code
2. Improved code maintainability
3. This is also the premise of realizing polymorphism
Disadvantages:
Class coupling enhancement

polymorphic

Polymorphism is divided into compile time and run time

  • Compile time polymorphism mainly refers to the method of subclass overriding parent class
  • Runtime polymorphism means that the specific type pointed to by the object reference defined in the program is determined during runtime

The following are the three necessary conditions for polymorphism at runtime:

  • Inherit parent class
  • Override parent method
  • Upward transformation means that the parent class reference points to the child class object, such as Animal cat = new cat(); The cat class inherits the Animal class
//Parent class
public class Animal {
  public String color;
  public int eyes;
  public String name;

  public int getEyes() {
    return eyes;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public void setEyes(int eyes) {
    this.eyes = eyes;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  public void eat(String food){
    System.out.println(name + "Eating" + food);
  }
 static class Dog extends Animal{
   @Override
   public void eat(String food) {
     System.out.println("Called is" + name + "eat" + food);
   }
 }
 static class Cat extends Animal{
   @Override
   public void eat(String food) {
     System.out.println("Called is" + name + "eat" + food);
   }
 }

  public static void main(String[] args) {
    Animal animal = new Animal();
    Animal cat = new Cat();
    Animal dog = new Dog();
    animal.setName("I'm an animal");
    animal.eat("Carnivorous"); //Output: I'm an animal eating meat
    cat.setName("I'm a cat");
    cat.eat("fish"); // Output: the call is that I am a cat eating fish
    dog.setName("dog");
    dog.eat("bone");//Output: the call is for the dog to eat bones
  }
  
}

As can be seen from the above code, dog and cat both have color, eyes, name attributes and eat methods. They rewrite the eat method of animal class and print their corresponding execution results instead of the methods in animal class

advantage

1. The replaceable polymorphism works for Animal and any other Animal;
2. Polymorphic code is extensible. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes. In fact, newly added subclasses are easier to obtain polymorphic functions.
3. flexibility. It embodies flexible and diverse operation in application and improves the use efficiency.
4. simplicity. Polymorphism simplifies the coding and modification process of application software, especially when dealing with the operation and operation of a large number of objects.

Keywords: JavaSE

Added by kcengel on Mon, 17 Jan 2022 17:01:12 +0200