public class Polymorphie { public static void main(String[] args) { A1 a = new A1(); B1 b = new B1(); a.hallo(); b.hallo(); // Polymorphie: Obwohl die Variable vom Typ A1 ist, wird // durch new B1() ein B1-Objekt zugewiesen, und das alte hallo() // aus A1 ist - weg! a = new B1(); // Was gibt das aus? hallo() aus A oder aus B? a.hallo(); System.out.println(a); // Ruft a.toString() auf } } class A1 { int i = 10; public String toString(){ // Überladen der Object-Funtion toString() return "Variable i des Objekts enthält: " + i; } public void hallo(){ System.out.println("Hallo"); } } class B1 extends A1 { public void hallo(){ System.out.println("Welt"); } }