Java learning notes object oriented polymorphism

Keywords: Java network

The manifestation of polymorphism

A reference to a parent class points to its own child class object.
A reference to a parent class can also accept its own child class object.

Code embody

abstract class Animal {
    public abstract void eat();
}
class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("eat fish");
    }
}
class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("eat bone");
    }
}
class Monkey extends Animal {
    @Override
    public void eat() {
        System.out.println("eat banana");
    }
}
public class Demo {
    public static void main(String[] args) {
        /*
        Animal c = new Cat();
        function(c);
         */
        function(new Cat());
        /*
        Animal d = new Dog();
        function(d);
        */
        function(new Dog());
        /*
        Animal m = new Monkey();
        function(m);
        */
        function(new Monkey());
    }
    public static void function(Animal a) {
        a.eat();
     }
}

Operation result

eat fish
eat bone
eat banana

The premise of polymorphism

There must be a relationship between classes. Either inheritance or implementation.
There is a replication relationship.

Advantages and disadvantages of polymorphism

Profit place

The appearance of polymorphism greatly improves the expansibility of the program.

malpractice

It improves extensibility, but only the reference of the parent class can be used to access the members of the parent class, not the members of the child class in advance (because the child class does not exist at this time).

Polymorphism - Example

abstract class Student {
    public void eat() {
        System.out.println("eat rice");
    }
    public abstract void study();
}
class StudentToDo {
    public void Do(Student s) {
        s.eat();
        s.study();
    }
}
class StudentClass1 extends Student {
    @Override
    public void eat() {
        System.out.println("eat steak");
    }
    @Override
    public void study() {
        System.out.println("study English");
    }
}
class StudentClass2 extends Student {
    @Override
    public void study() {
        System.out.println("study Chinese");
    }
}
class StudentClass3 extends Student {
    @Override
    public void study() {
        System.out.println("study Japanese");
    }
}
public class Demo2 {
    public static void main(String[] args) {
        StudentToDo std =  new StudentToDo();
        std.Do(new StudentClass1());
        std.Do(new StudentClass2());
        std.Do(new StudentClass3());
    }
}

Operation result:

eat steak
study English
eat rice
study Chinese
eat rice
study Japanese

Features in polymorphic occurrence codes (considerations for polymorphic use)

Characteristics of member functions in polymorphism

At compile time, refer to whether there are called methods in the class to which the reference type variable belongs, and if there is compilation pass, if there is no compilation failure.
At run time, see if there are called methods in the class to which the object belongs.
Simple summary: when member functions are called in polymorphic mode, compile them to the left and run them to the right.

class Fu {
    void method1() {
        System.out.println("Fu_Method_1");
    }
    void method2() {
        System.out.println("Fu_Method_2");
    }
}
class Zi extends Fu {
    @Override
    void method1() {
        System.out.println("Zi_Method_1");
    }
    void method3() {
        System.out.println("Zi_Method_3");
    }
}
public class Demo3 {
    public static void main(String[] args) {
        Fu f =new Zi();
        f.method1();
        f.method2();
        //f.method3(); there is no method3() method in class Fu to which reference type F belongs, so compilation will fail.
    }
}

Operation result:

Zi_Method_1
Fu_Method_2

Characteristics of member variables in polymorphism

Whether compiling or running, refer to the left (the class to which the referential variable belongs)

class Fu {
    int num = 1;
}
class Zi extends Fu {
    int num = 2;
}
public class Demo4 {
    public static void main(String[] args) {
        Fu f = new Zi();
        System.out.println(f.num);   //Reference left
        Zi z= new Zi();
        System.out.println(z.num);
    }
}

Operation result:

1
2

Characteristics of static member functions (variables) in polymorphism

Whether compiling or running, refer to the left.
Static type, static binding.
Non static type, dynamic binding.

class Fu {
    static void method1() {
        System.out.println("Fu_Method_1");
    }
}
class Zi extends Fu {
    static void method1() {
        System.out.println("Zi_Method_1");
    }
}
public class Demo5 {
    public static void main(String[] args) {
        Fu f = new Zi();
        f.method1();
        Zi z = new Zi();
        z.method1();
    }
}

Operation result:

Fu_Method_1
Zi_Method_1

Polymorphism transformation

abstract class Animal {
    public abstract void eat();
}
class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("eat fish");
    }
    public void CatchMouse() {
        System.out.println("CatchMouse");
    }
}
class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("eat bone");
    }
    public void GuardHouse() {
        System.out.println("GuardHouse");
    }
}
class Monkey extends Animal {
    @Override
    public void eat() {
        System.out.println("eat banana");
    }
    public void PlayBall() {
        System.out.println("Play Ball");
    }
}
public class Demo1 {
    public static void main(String[] args) {
        function(new Cat());
        function(new Dog());
        function(new Monkey());
    }
    public static void function(Animal a) {
        a.eat();
        if (a instanceof Cat) { //The instanceof keyword is used to determine the type
            Cat c = (Cat)a;     //Downward transformation
            c.CatchMouse();
        } else if (a instanceof Dog) {
            Dog d = (Dog)a;     //Downward transformation
            d.GuardHouse();
        } else {
            Monkey m = (Monkey)a;//Downward transformation
            m.PlayBall();
        }
    }
}

Operation result:

eat fish
CatchMouse
eat bone
GuardHouse
eat banana
Play Ball

Application of polymorphism

An example of computer motherboard
When analyzing the main board of computer, we should consider the expansibility. pci interface is used. The board card (sound card, network card, etc.) and the main board follow the pci interface.

interface Pci {
    public void open();
    public void close();
}

class MainBoard {
    public void run() {
        System.out.println("Main Board run");
    }
    public void usePci(Pci p){ //Interface type points to its subclass object
        if(p != null){    //Add judgment to prevent null pointer
        p.open();
        p.close();
        }
    }
}
class Netcard implements Pci {
    @Override
    public void open() {
        System.out.println("Netcard open");
    }

    @Override
    public void close() {
        System.out.println("Netcard close");
    }
}
class Soundcard implements Pci {
    @Override
    public void open() {
        System.out.println("Soundcard open");
    }

    @Override
    public void close() {
        System.out.println("Soundcard close");
    }
}
public class Demo6 {
    public static void main(String[] args) {
        MainBoard mb = new MainBoard();
        mb.run();
        mb.usePci(null);
        mb.usePci(new Netcard());
        mb.usePci(new Soundcard());
    }
}

Operation result

Main Board run
Netcard open
Netcard close
Soundcard open
Soundcard close

Posted by immobilarity on Sat, 15 Feb 2020 05:52:24 -0800