# Java多型

Java中的多型是一個概念，通過它我們可以通過不同的方式執行單個動作(方法)。 多型性派生自2個希臘詞：「poly」和「morphs」。 詞語「poly」意為許多，「morphs」意為形式。 所以多型表示為多種形式。

## 1. Java執行時多型性

class A{}
class B extends A{}
A a=new B();  //向上轉換...

Java執行時多型性範例1

class Bike {
void run() {
System.out.println("running");
}
}

class Splender extends Bike {
void run() {
System.out.println("running safely with 60km");
}

public static void main(String args[]) {
Bike b = new Splender();// upcasting - 向上轉換
b.run();
}
}

running safely with 60km.

Java執行時多型性範例2：Bank

class Bank {
float getRateOfInterest() {
return 0;
}
}

class SBI extends Bank {
float getRateOfInterest() {
return 8.4f;
}
}

class ICICI extends Bank {
float getRateOfInterest() {
return 7.3f;
}
}

class AXIS extends Bank {
float getRateOfInterest() {
return 9.7f;
}
}

class TestPolymorphism {
public static void main(String args[]) {
Bank b;
b = new SBI();
System.out.println("SBI Rate of Interest: " + b.getRateOfInterest());
b = new ICICI();
System.out.println("ICICI Rate of Interest: " + b.getRateOfInterest());
b = new AXIS();
System.out.println("AXIS Rate of Interest: " + b.getRateOfInterest());
}
}

SBI Rate of Interest: 8.4
ICICI Rate of Interest: 7.3
AXIS Rate of Interest: 9.7

Java執行時多型性範例3：Shape

class Shape { // 基礎類別(形狀)
void draw() {
System.out.println("drawing...");
}
}

class Rectangle extends Shape {
void draw() {
System.out.println("drawing rectangle...");
}
}

class Circle extends Shape {
void draw() {
System.out.println("drawing circle...");
}
}

class Triangle extends Shape {
void draw() {
System.out.println("drawing triangle...");
}
}

class TestPolymorphism2 {
public static void main(String args[]) {
Shape s;
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
s = new Triangle();
s.draw();
}
}

drawing rectangle...
drawing circle...
drawing triangle...

Java執行時多型性範例4：Animal

class Animal {
void eat() {
System.out.println("eating...");
}
}

class Dog extends Animal {
void eat() {
}
}

class Cat extends Animal {
void eat() {
System.out.println("eating rat...");
}
}

class Lion extends Animal {
void eat() {
System.out.println("eating meat...");
}
}

class TestPolymorphism3 {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.eat();
a = new Cat();
a.eat();
a = new Lion();
a.eat();
}
}

eating rat...
eating meat...

## Java執行時多型性與資料成員

class Bike {
int speedlimit = 90;
}

class Honda3 extends Bike {
int speedlimit = 150;

public static void main(String args[]){
Bike obj=new Honda3();
System.out.println(obj.speedlimit);//90
}
}

90

## Java執行時多型性與多級繼承

class Animal {
void eat() {
System.out.println("eating");
}
}

class Dog extends Animal {
void eat() {
System.out.println("eating fruits");
}
}

class BabyDog extends Dog {
void eat() {
System.out.println("drinking milk");
}

public static void main(String args[]) {
Animal a1, a2, a3;
a1 = new Animal();
a2 = new Dog();
a3 = new BabyDog();
a1.eat();
a2.eat();
a3.eat();
}
}

eating
eating fruits
drinking Milk

class Animal {
void eat() {
System.out.println("animal is eating...");
}
}

class Dog extends Animal {
void eat() {
System.out.println("dog is eating...");
}
}

class BabyDog1 extends Dog {
public static void main(String args[]) {
Animal a = new BabyDog1();
a.eat();
}
}

Dog is eating