Day 27 : Polymorphism

Chi-Tse Chiang
2022-10-12 21:17:34

Intro

Yesterday's last mentioned that Java actually allows subclasses to inherit multiple interfaces of a parent class member.

What if, conversely, a parent class is inherited by multiple child classes?
This is called polymorphism.

As the name suggests, polymorphism refers to many types, which means providing a uniform interface for different data types.

Polymorphism uses this to accomplish different functions through inherited functional attributes.

Example

class Grade {
  void grade_show() {
    System.out.println("Here comes Student's grades : ");
  }
}

class S1 extends Grade {
  void grade_show() {
    System.out.println("Student 1 got : 98 ");
  }
}

class S2 extends Grade {
  void grade_show() {
    System.out.println("Student 2 got : 87 ");
  }
}

public class Main {
  public static void main(String[] args) {
    Grade main_obj = new Grade();

    Grade S1_obj = new S1();   //將S1當Grade來看
    S2 S2_obj = new S2();    //S2用自己物件名稱和自己的建構子初始化內容

	main_obj.grade_show();
	S1_obj.grade_show();
	S2_obj.grade_show();
  }
}

S1 and S2 inherit the members of the Grade class. When declaring the objects in the main function, since both subclasses inherit the properties of the parent class, we will treat both objects as Grade and initialize the content using their respective constructors.

So far, why use multiple inheritance for multiple subclasses? This approach:

makes objects and classes logically independent, which increases program maintainability and reusability

Abstract Polymorphism

Then it mentions polymorphism of abstract classes, which cannot create objects by themselves, and is slightly different from inheritance.

abstract class Animal{
    public abstract String identify();
}

class mammals extend Animal {
		public abstract String identify(){
			return 'Its mammals';
		}
}

class Amphibians extend Animal {
	  public abstract String identify(){
			return 'Its Amphibians';
		}
}

public class Main{

		public String identification(Animal animal){
			if(animal.identify() == 'Its mammals'){
                return 'Mammals';
                    }else if(animal.identify() == 'Its Amphibians'){
                        return 'Amphibians';
                            }else{
                                return 'Nothing';
                            }
		 }

		public static void main(String[] args){
			Amphibians Frogs = new Amphibians();
			System.out.println(identification(Frogs));  //Outputs :  Amphibians
		}
}

Program analysis:
This program is designed to identify whether an animal is a mammal, an amphibian, or neither.Since abstract classes cannot create objects, the concept of polymorphism is used here to allow the two subclasses to inherit their members.Then overrides are performed in the subclasses.

This part is also worth noting: the main program:

the function identification() is used to distinguish between animal species

the abstract Animal is used to declare the parameters, so we only need to determine the inherited object constructor as the argument to be passed in

Abstract or interface polymorphism allows different concepts to be inherited from the same general class

(Resource:什麼是oo-物件導向與多型)