• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Some people say object oriented, what is this object? Everything is an object. You taste, you fine taste!

Delve into the three main features of object orientation

1. Package:

The so-called encapsulation hides the internal implementation details of the object as far as possible, and controls the modification and access permissions of the object.

Typically, private access modifiers modify properties to make them visible to the class.

Access channels. The get/set method is the only way for the outside world to access private attributes, and the data can be filtered internally. (Filter criteria can be added in set method)

Basic requirements: Provide a common access method to ensure that data can be entered normally.

2. Inheritance:

Inheritance is a gift or acquisition of characteristics and behaviors between classes.

Extraction of commonality

Syntax: Class extends extends parent {}

Application: After inheritance, subclasses can have superclass attributes and methods

Note: A class can only inherit from one parent class, but can inherit at multiple levels, with attributes and methods superimposed

Access modifier

Maximum range:

Public >protected >default >private

An inheritance

1. A constructor in a class is only responsible for creating objects of the class

2. Properties and methods of the private modifier, visible only to this class

3. If parent and child classes are not in the same package, the default modifier 1 method and attribute cannot be inherited

case

Create an animal class with name, color and eat().

 public class Animal {
            private String name ;
            private String color;
            public void eat() {
                    System.out.println("Eat something."); }}Copy the code

Create a cat class with footCount, eat(), play()


    public class Cat extends Animal{
            // Define the number of variable claws
            private String footCount;
            public void eat() {
                    System.out.println("Eat cat food");
            }
            public void play() {
                    System.out.println("Playing with water"); }}Copy the code

Create a dog class with the number of PAWS and the play() method

     public class Dog extends Animal{
        super.eat()
          // Define the number of variable claws
          private String footCount;
          public void play() {
          System.out.println("Play ball"); }}Copy the code

Create a test class

public calss test{ public static void main (String[] args){ Cat cat = new Cat(); // Create a cat object cat.name = "cat "; // Error, syntax pair, but need get/set method access private cat.footcoount = 4; / / the correct eat (); // Rewrite the eat() method -- content -- eat cat food cat.play(); // Call play Dog Dog = new Dog(); dog.eat(); // Use super to access the common attributes or methods of the parent class}}Copy the code
Method override/override:

Coverage principle:

1. The return value type, method name, and parameter list —— must be the same

2. Access modifiers can be the same as or wider than the parent class

Execution mechanism: After a subclass overrides a method, the version of the method overridden by the subclass takes precedence

Super keyword:

1. In subclasses, you can directly access properties and methods inherited from the parent class in the form of “super.”

If the parent class has properties and methods with the same name and needs to obtain the parent class properties or methods, you need to use super to obtain the parent class members

2. On the first line of the subclass constructor: using super (), call the parent constructor to issue the method

Subclass object creation under inheritance relationship:

Subclass object creation – Create the superclass object first, then create the subclass object

By the parent class generic, exclusive subclass, complete the subclass object

Process: first build the subclass object —— initialize the property —- to execute the constructor (default no parameter construction, parameter construction execution process needs to be specified)

Polymorphism:

By polymorphism, a parent class reference points to a subclass object, resulting in multiple forms.

1. The two must have a direct or indirect inheritance relationship

2. The parent class reference can only call the attributes and methods of the parent class, but cannot call the attributes and methods of the child class

3. When a method is invoked by a parent class reference, the overridden method is executed first

public calss test{
        public static void main (String[] args){
        Animal cat = new Cat();// A parent class reference creates a subclass object
        Animal dog = new dog();
        cat.eat();// Eat cat food - subclass overlay
        dog.eat();// Eat something}}Copy the code

Two application scenarios of polymorphism:

  1. Scenario 1: Implement polymorphism using a parent class as a method parameter

  2. Scenario 2: Implement polymorphism using the parent class as the method return value