iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0

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.
https://ithelp.ithome.com.tw/upload/images/20221012/20151216U4D2wBuEB0.png


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-物件導向與多型)


上一篇
Day 26 : Interface
下一篇
Day 28 : Inner class
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言