Polymorphism: method-call binding

 All method binding in Java uses late binding(polymorphically) unless the method is static or final ( private methods are implicitly final ). This means that ordinarily you don’t make decisions about whether late binding will occur—it happens automatically.

Producing the Right behavior

// polymorphism/shape/Shape.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package polymorphism.shape;

public class Shape {
  public void draw() {
    System.out.println("Base Shape.draw()");// I added to test
  }

  public void erase() {
    System.out.println("Base Shape.erase()");// I added to test
  }
}
// polymorphism/shape/Circle.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package polymorphism.shape;

public class Circle extends Shape {
  @Override
  public void draw() {
    System.out.println("Circle.draw()");
  }
  @Override
  public void erase() {
    System.out.println("Circle.erase()");
  }
}
// polymorphism/shape/Square.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package polymorphism.shape;

public class Square extends Shape {
  @Override
  public void draw() {
    System.out.println("Square.draw()");
  }
  @Override
  public void erase() {
    System.out.println("Square.erase()");
  }
}
// polymorphism/shape/Triangle.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package polymorphism.shape;

public class Triangle extends Shape {
  @Override
  public void draw() {
    System.out.println("Triangle.draw()");
  }
  @Override
  public void erase() {
    System.out.println("Triangle.erase()");
  }
}

 A "factory"

// polymorphism/shape/RandomShapes.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// A "factory" that randomly creates shapes
package polymorphism.shape;

import java.util.*;

public class RandomShapes {
  private Random rand = new Random(47);

  public Shape get() {
    switch (rand.nextInt(3)) {
      default:
      case 0:
        return new Circle();
      case 1:
        return new Square();
      case 2:
        return new Triangle();
    }
  }

  public Shape[] array(int sz) {
    Shape[] shapes = new Shape[sz];
    // Fill up the array with shapes:
    for (int i = 0; i < shapes.length; i++) {
        shapes[i] = get();
    }
    return shapes;
  }
}
// polymorphism/Shapes.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Polymorphism in Java

import polymorphism.shape.*;

public class Shapes {
  public static void main(String[] args) {
    Shape shape1 = new Shape();
    shape1.draw();
    shape1.erase();
    Shape circleShape = new Circle();
    circleShape.erase();

    System.out.println("Make polymorphic method calls:");
    RandomShapes gen = new RandomShapes();
    // Make polymorphic method calls:
    for (Shape shape : gen.array(9)) shape.draw();
  }
}
/* My Output:
Base Shape.draw()
Base Shape.erase()
Circle.erase()
Make polymorphic method calls:
Triangle.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Square.draw()
Triangle.draw()
Circle.draw()
*/
public class A {
    public void draw() { 
        //... 
    }
    public void spin() { 
        // ... 
    }
}

public class B extends A {
    public void draw() { 
        // ... 
    }
    public void bad() { 
        // ... 
    }
}

...

A testObject = new B();

testObject.draw(); // calls B's draw, polymorphic
testObject.spin(); // calls A's spin, inherited by B

// note this error
testObject.bad(); // compiler error, you are manipulating this as an A

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/shape/Shape.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/shape/Circle.java

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/shape/Square.java

5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/shape/Triangle.java

6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/shape/RandomShapes.java

7. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/polymorphism/Shapes.java

8. https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值