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