Polymorphism and abstract classes and interfaces in JAVA object-oriented

Keywords: Java

polymorphic

Understanding: the same object has multiple forms

Function: unify different data types, so that the program has strong scalability

The premise of realizing polymorphism is that there must be inheritance

1, Class polymorphism

The role of polymorphism: unify different data types, so that the program has strong scalability

Suppose a person has pigs and fish

Create the corresponding class

Fish:

public class Fash {
    public void move(){
        System.out.println("feel just like a fish in water");
    }
}

Pig:

public class Ping {
    public void move(){
        System.out.println("People are afraid of being famous, pigs are afraid of being strong");
    }
}

Person:

public class Person {
    public void ping(Ping p){
        p.move();
    }
    public void fash(Fash f){
        f.move();
    }
}

Test class:

public class Test1 {
    public static void main(String[] args) {
        Fash f = new Fash();
        Ping p = new Ping();


        Person person = new Person();
        person.fash(f);
        person.ping(p);
    }
}

Execution results:

feel just like a fish in water
 People are afraid of being famous, pigs are afraid of being strong

Process finished with exit code 0

We can find that there must be corresponding methods in the corresponding person class for how many kinds of animals people keep

The method body of the writing method is to execute the move method, so the development efficiency is too low and the amount of code is too much.

Solution: we can classify fish and pigs as animals, and animals also have a move method. We just need to declare a method in the person class and call the move method to let pigs and fish inherit the animal class.

Code implementation:         

Here we compare cats with dogs

public class Cat extends Animal {
    public void eat(){
        System.out.println("Cats love catching mice!");
    }
  public void findeat(){
        System.out.println("Cats love to sleep in");
    }

}
public class Dog extends Animal{
    public void eat(){
        System.out.println("Dogs like to eat bones");
    }
}
public class Person {
    public void feed1(Animal animal){
        animal.eat();
    }

}
public class Test {
    public static void main(String[] args) {
        //Assign the object of the subclass to the reference of the parent type, or the reference of the parent type points to the object of the subclass
        //Upward Transformation: it will shield the unique methods in subclasses

        Animal a1 = new Cat();//Understanding: think of cats as animals
        Animal a2 = new Dog();
        Person p = new Person();
        p.feed1(a1);
        p.feed1(a2);


        //Downward transformation
        //Downward transformation can be risky: java requires that cast types must be written
        Cat c = (Cat)a1;
        c.findeat();
      

result:

Cats love catching mice!
Dogs like to eat bones
 Cats love to sleep in

This is class polymorphism

2, Abstract class

Abstract: nonexistent

Abstraction in java: only name, not implementation

characteristic:
*1. Abstract classes cannot create objects
 *2. If there are abstract methods in a class, the class must be an abstract class
 *3. Abstract classes can have abstract methods or ordinary methods
 *4. The subclass of an abstract class must override the abstract method of the parent class, otherwise the subclass must also be an abstract class
Function: through abstract classes, you can force which methods must be in subclasses to standardize methods.

abstract

Implementation: the method modified by abstract is an abstract method. The abstract method has no method body and ends directly

public abstract class Animal {
    //The method modified by abstract is an abstract method. The abstract method has no method body and ends directly
    public abstract void eat();
    public abstract void move();

}
public class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("Cats eat fish");
    }

    @Override
    public void move() {
        System.out.println("Cats are generally lazy to move");
    }
}
package com.study.demo.dt.cx;

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

        /**
         * At this point, the abstract class plays a normative role
         * Abstract methods defined in an abstract class. Subclasses must override these methods
         */
        Animal a = new Cat();//Here is polymorphism
        a.eat();
        a.move();


    }
}

result:

feel just like a fish in water
 People are afraid of being famous, pigs are afraid of being strong

3, Interface

*Interface: an interface is actually a special abstract class. All methods in the interface are abstract methods and cannot have ordinary methods
 *Interfaces use interface declarations
 *Interface inheritance:
*Interfaces can only be inherited by interfaces
 *Interfaces and classes can only be implementation relationships
 *Interfaces are also polymorphic
*
*Advantages of interface:
*Classes can only inherit from one class, and interfaces can be implemented more than one.
*Many irrelevant contents can be integrated through the interface
*
*Interface features:
*1. All methods in the interface are abstract and public
 *2. All variables in the interface are global static constants

Create the Animal interface and the implementation class AnimalAImpl of the interface

public interface Animal {
    //All methods in the interface are abstract methods, so abstract can be omitted
    //All contents in the interface are public and public.

     void getCat();
}
public class AnimalImpl implements Animal {
    @Override
    public void getCat() {
        System.out.println("I want a cat!");
    }
}

Test class

public class Test {
    public static void main(String[] args) {
        //Interfaces can be implemented in multiple ways
        AnimalImpl animal = new AnimalImpl();
        AnimalImpl2 animalImpl2 = new AnimalImpl2();
        AnimalImpl3 animalImpl3 = new AnimalImpl3();
        animal.getCat();
        animalImpl2.getCat();
        animalImpl3.getCat();

        System.out.println("---------------------------------");
      
        Cat cat = new Cat();
        cat.getCat();
        cat.move();
        cat.eat();
    }
}

result:

I want a cat!
Because cats are so cute
 But the cat is so lazy
---------------------------------
I want a cat
 Cats are lazy
 Animals eat quickly

Posted by hmogan on Fri, 17 Sep 2021 14:05:18 -0700