package net.gerhut.tests;
 
/**
 * Parent
 * @author Gerhut
 */
class Parent {
    /**
	 * Instance variable
	 */
	String value = "Parent";
	/**
	 * Instance method
	 * @return Value of instance method
	 */
	String getValue() {return "Parent";}
}
 
/**
 * Son
 * @author Gerhut
 */
class Child extends Parent {
	/**
	 * Instance variable
	 */
	String value = "Child";
	/**
	 * Instance method
	 * @return Value of instance method
	 */
	String getValue() {return "Child";}
}
 
/**
 * Testing of polymorphism.
 * @author Gerhut
 */
public class PolymorphismTest {
	/**
	 * Entrance of the program.
	 * @param args No use.
	 */
	public static void main(String[] args) {
		Parent p = new Parent();
		Parent pc = new Child();
		Child c = new Child();
		System.out.println(p.value + ", " + p.getValue());
		System.out.println(pc.value + ", " + pc.getValue());
		System.out.println(c.value + ", " + c.getValue());
	}
}

运行结果

Parent, Parent
Parent, Child
Child, Child