java object-oriented programming -- Inheritance and polymorphism

1, Inherit

Inheritance is a powerful object-oriented mechanism, which can realize code reuse through the extensions keyword

class Person{
    
}
class Student extends Person{
    
}

ps: the subclass will automatically get the fields of the parent class, so you can't define the same fields as the parent field in the subclass

Extension: in OOP terms, people are called super class, parent class, base class, and students are called subclass es and extended class es

1. Inheritance tree

  • The extends keyword is not written. The Object class is inherited by default

  • java only allows one class to inherit from one class, that is, each class has only one parent class. Object is special and has no parent class

protected

The subclass cannot access the private field and private method in the parent class. Modifying the field in the parent class to the protected attribute can enable the subclass to access it successfully

Reason: protected keeps the access control of fields and methods inside the inheritance tree

super parent class

  • The subclass accesses the name field super in the parent class name , this.name has the same effect as name

  • The subclass will not inherit the constructor of the parent class. The default constructor of the subclass is automatically generated by the compiler. If the parent class has no default constructor, you need to call super() to let the compiler automatically match the constructor in the parent class

Block inheritance

  • If the final keyword is not used, any class can inherit

  • The sealed keyword is decorated to limit the classes that can be inherited. Permissions specifies which classes can be inherited

    public sealed class Person permits Xiaoming, Lihua{
        ......
    }
    

Upward and downward transformation

  • Upcasting is to safely convert a subclass type to a parent type

    For example, if the inheritance tree is Student - > Person - > Object, the Student class can be transformed into Person class or even Object class

  • Downcasting converts a parent type to a child type

    This may lead to errors, because there are fewer methods in the parent class than in the child class, and the conversion may not succeed. Judge first and then convert through isinstance()

2. Inheritance and combination

Inheritance is the relationship of is and combination is the relationship of has

class Book{
    ...
}
class Person{
    ...
}
class Student extends Person{
    private Book book;
}

2, Polymorphism

  • Override, which reflects the polymorphism between the subclass and the parent class, and the polymorphism at run time
  • Overload (overload). Overload reflects the polymorphism of methods in the class and the polymorphism at compile time

ps: the method name is the same, the method parameters are the same, but the return value of the method is different, which is also a different method

1. Polymorphic form

/*
Parent object name = new subclass name

Interface object name = new implementation class constructor
*/
  • Method call: compile to the left and run to the right
  • Variable call: compile to the left and run to the right

2. Override Obejct method

  • toString() method to output the instance as a string type
  • The equals() method determines whether two instances are equal
  • hashcode() method to calculate the hash value

super can implement the middle note override method in which the parent class is overwritten.

3. Role of final

  • Classes decorated with final are not allowed to be inherited

  • final modified methods are not allowed to be overridden

  • The final modified field is initialized when the object is created and cannot be modified later

4. Learn the last question

Create an Auto class, including the number of tires, Car color, body weight, speed and other member variables. And create instances through different construction methods. At least the Car is required to be able to accelerate, decelerate and stop. Then define a Car class Car, inherit Auto, and add member variables such as air conditioner and CD to cover the method of acceleration and deceleration.

Auto.java

package Object;

/**
 * @className:Auto;
 * @time:2022-04-18-11:09;
 * @author:Lee Ye;
 * @description:This is a java example;
 */
public class Auto {
    public int tire;
    public String color;
    public int weight;
    public int speed;
    public Auto(){

    }
    public Auto(int tire, String color, int weight, int speed){
        this.tire = tire;
        this.color = color;
        this.weight = weight;
        this.speed = speed;
    }
    public void speedup(int i){

        System.out.printf("the speed in auoto is %d", i);
        this.speed += i;
    }
    public void speeddown(int i){
        this.speed -= i;
    }

}

Car.java

package Object;

/**
 * @className:Car;
 * @time:2022-04-18-11:23;
 * @author:Lee Ye;
 * @description:This is a java example;
 */
public class Car extends Auto {
    private String condition;
    private String CD;
    public Car(int tire, String color, int weight, int speed, String condition, String CD){
        super(tire, color, weight,  speed);
        this.condition = condition;
        this.CD = CD;

    }
    public void speedup(int i){
        System.out.printf("the i in class Car is %d\n", i);
        this.speed *= i;
        System.out.printf("the current speed in class Car is %d\n", this.speed);
    }
    public void speeddown(int i){
        this.speed -= 2 * i;
    }

}

Test_Auto.java

package Object;
import Object.Car;
import Object.Auto;

/**
 * @className:Test_Auto;
 * @time:2022-04-18-11:31;
 * @author:Lee Ye;
 * @description:This is a java example;
 */
public class Test_Auto {
    public static void main(String[] args) {
        Auto benchi = new Car(4, "Black", 200, 70, "geli", "2s");
        System.out.printf("Start speed is  %d\n", benchi.speed);
        benchi.speedup(10);
        System.out.printf("stop speed is %d\n", benchi.speed);

    }
}

Tags: Java

Posted by The1PatO on Mon, 18 Apr 2022 13:14:01 +0930