Java Self-Learning-Interface and Inheritance Polymorphism

Java Polymorphism

Polymorphism of Operators
+can be used as an arithmetic operation or as a string connection

Class Polymorphism
Parent Reference Points to Subclass Object

Example 1: Operator polymorphism

The same operator has different roles in different situations
If both sides of the + sign are integers, then + represents the addition of numbers
If either side of the + sign is a string, + represents a string connection

package charactor;
   
public class Hero {
    public String name;
    protected float hp;
 
    public static void main(String[] args) {
         
        int i = 5;
        int j = 6;
        int k = i+j; //If both sides of the + sign are integers, then + represents the addition of numbers
         
        System.out.println(k);
         
        int a = 5;
        String b = "5";
         
        String c = a+b; //If either side of the + sign is a string, + represents a string connection
        System.out.println(c);
         
    }
       
}

Example 2: Observing class polymorphism

To observe the polymorphism of a class:

  1. Both i1 and i2 are Item types
  2. All call effect methods
  3. Output different results

Polymorphism: All are of the same type, calling the same method, but can present different states

package property;
 
public class Item {
    String name;
    int price;
 
    public void buy(){
        System.out.println("purchase");
    }
    public void effect() {
        System.out.println("Items can be effective after use ");
    }
     
    public static void main(String[] args) {
        Item i1= new LifePotion();
        Item i2 = new MagicPotion();
        System.out.print("i1  yes Item Type, execution effect Print:");
        i1.effect();
        System.out.print("i2 Also Item Type, execution effect Print:");
        i2.effect();
    }
 
}
package property;
 
public class LifePotion extends Item {
    public void effect(){
        System.out.println("You can return blood after using the blood bottle");
    }
}
package property;
 
public class MagicPotion extends Item{
 
    public void effect(){
        System.out.println("After using the blue bottle, you can return to magic");
    }
}

Example 3: Polymorphic condition of class

To achieve class polymorphism, the following conditions are required

  1. Parent (interface) references point to child class objects
  2. Method invoked has overrides

So what does polymorphism do?Learn more about using and not using polymorphisms by comparing them

Example 4: Class Polymorphism - Do Not Use Polymorphism

If polymorphism is not used,
Assuming that heroes want to use blood and magic bottles, there are two ways to design Hero

useLifePotion
useMagicPotion

There are many other things besides blood and magic bottles, so there are many ways to design them, such as
usePurityPotion Purification Drug Water
useGuard guard
useInvisiblePotion uses invisible potions
Wait and so on

package charactor;
 
import property.LifePotion;
import property.MagicPotion;
   
public class Hero {
    public String name;
    protected float hp;
 
    public void useLifePotion(LifePotion lp){
        lp.effect();
    }
    public void useMagicPotion(MagicPotion mp){
        mp.effect();
    }
 
    public static void main(String[] args) {
         
        Hero garen =  new Hero();
        garen.name = "Galen";
     
        LifePotion lp =new LifePotion();
        MagicPotion mp =new MagicPotion();
         
        garen.useLifePotion(lp);
        garen.useMagicPotion(mp);
         
    }
       
}

Example 5: Class Polymorphism - Usage Polymorphism

If there are many different types of items, there are many ways to design them.
For example, useArmor,useWeapon, and so on

This time polymorphism is used to solve this problem
Design a method called useItem whose parameter type is Item
If you are using a blood bottle, call this method
If you are using a magic bottle, or call the method
No matter what kind of items a hero wants to use, just one way is needed

package charactor;
 
import property.Item;
import property.LifePotion;
import property.MagicPotion;
   
public class Hero {
    public String name;
    protected float hp;
 
    public void useItem(Item i){
        i.effect();
    }
 
    public static void main(String[] args) {
         
        Hero garen =  new Hero();
        garen.name = "Galen";
     
        LifePotion lp =new LifePotion();
        MagicPotion mp =new MagicPotion();
         
        garen.useItem(lp);
        garen.useItem(mp);     
         
    }
       
}

Practice: Java Polymorphism

  1. Design an interface
    The interface is called Mortal, and one of the methods is called die
  2. Implement Interface
    Let the three classes ADHero,APHero,ADAPHero implement the Mortal interface. When different classes implement the die method, they all print out different strings.
  3. For the Hero class, add a method in which the die m method of M is called.

    public void kill(Mortal m)

  4. In the main method
    First instantiate a Hero object: Galen
    Three objects are then instantiated, which are instances of ADHero, APHero, and ADAPHero
    Then let Galen kill these three objects

Tags: Java

Posted on Wed, 11 Sep 2019 16:40:56 -0700 by Matt Phelps