javase - 08 - polymorphic - abstract class

Posted by gkep on Wed, 02 Feb 2022 08:27:44 +0100

1, Inherited memory parsing

 

2, Polymorphism

1. Scene analysis

 

At this time, students want to migrate data from java to mysql operating system with good polymorphism

2. What is polymorphism

A variety of manifestations of a thing. The F1 of the keyboard will get different results by pressing F1 under different software

3. Introduce polymorphic syntax

Case: animals, people, pigs, people are animals, pigs are animals

Use code to indicate:

public class Animal {

public void eat() {
    System.out.println("Eat something");
}

}

class People extends Animal{

public void eat() {
    System.out.println("Eat hot pot");
}
​
public void playGames() {
    System.out.println("Roll");
}

}

class Pig extends Animal{

public void eat() {
    System.out.println("Arch cabbage");
}
​
public void sleep() {
    System.out.println("Pig sleep");
}

}

ublic class AnimalTest { public static void main(String[] args) {

    //Man is an animal
    //Humans inherit animals, and humans are a subclass of animals
    Animal a1 = new People();
    a1.eat();//Eat hot pot
    //a1.playGames();// report errors! Animal is called compile time type. Code compilation depends on the compile time type. Only some methods of compile time type can be called
    //Runtime type the runtime type is People
    //Runtime can be understood as
    //People a1 = new People();
    People p = (People)a1;
​
    Animal a = new Animal();
    People p1 = (People)a;
​
    //Pigs are animals
    Animal a2 = new Pig();
    a2.eat();//Arch cabbage
    //a2.sleep;
}

}

4. Compile time type and runtime type

Object assignment. The left side of the = sign is the compile time type. The object type of new is the run-time type. When writing code, look at the type on the left (compile time type). Only some methods and properties of the compile time type can use the code run-time. If the subclass overrides the method of the parent class, it runs the method rewritten by the subclass

1. Type 1 on the left is called compile time type

2. Look at the left side of the method call

3. The type on the left can be used only if it is available

4. Type 2 on the right is called runtime type

5. Look at the right side when executing the method

6. If the subclass rewrites this method, it calls the method rewritten by the subclass

5. Type conversion

Shape up shape down

 

6. instanceof keyword

Determine whether the runtime type of an object is a type

Expression syntax:

 

Boolean expression that returns true or false

7. Practice

public class A { public String show(D obj) { return ("A and D"); } public String show(A obj) { return ("A and A"); } }

class B extends A { public String show(B obj) { return ("B and B"); } public String show(A obj) { return ("B and A"); } }

class C extends B { }

class D extends B { }

 

public class Test { public static void main(String[] args) {

    A a1 = new A();//Create an instance of A
    A a2 = new B();//Polymorphic compilation depends on A and B when running
    B b = new B();//Create an instance of B
    C c = new C();//Create an instance of C
    D d = new D();//Create an instance of D
​
    System.out.println(a1.show(b));//A and A
    System.out.println(a1.show(c));//A and A
    System.out.println(a1.show(d));//A and D
​
    //A little more complicated
    System.out.println(a2.show(b));//B and A
    System.out.println(a2.show(c));//B and A
    System.out.println(a2.show(d));//A and D
​
    System.out.println(b.show(b));//B and B
    System.out.println(b.show(c));//B and B
    System.out.println(b.show(d));//A and D
}

}

3, final

The variable modified by final becomes a constant, and the constant name is all capitalized math PI

4, Code block

1. Static code block

2. Local code block

3. Construct code blocks

5, Abstract class

Abstract method:

1. The method implementation of a method in a parent class is meaningless. We can write this method as an abstract method

What scenarios use abstract classes:

(1) Abstract method in class

(2) This class cannot be instantiated, it can only be inherited

Example code:

public abstract class Dog {

private String name;
private int age;
​
public Dog() {}
public Dog(String name, int age) {
    this.name = name;
    this.age = age;
}
​
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public abstract void fight();

public class DogTest { public static void main(String[] args) {

    Dog dog1 = new Husky("Ha'er",5);
    
    dog1.fight();
    
    //Abstract class cannot be instantiated - abstract class cannot create object!

// Dog dog2 = new Dog(); while(true) {

    }   
        }
        }

Summary of abstract classes and methods:

1. Methods modified by abstract are called abstract methods. Abstract methods must have no method body.

2. The class of the abstract method must be an abstract class, which is modified by abstract

3. Abstract methods do not necessarily exist in abstract classes

4. Abstract classes cannot instantiate objects

Topics: Java Polymorphism abstract class