package Polymorphie; class F { String m() { return "F.m()" + this.s(); } String m(F f) { return "F.m(F)" + s(); } private String m(G g) { return "F.m(G)"; } String k() { return "F.k()" + this.m(this); } String k(G g) { return "F.k(G)"; } static String s() { return "F.s()"; } static String t() { return "F.t()"; } } class G extends F { String m() { return "G.m()" + super.s(); } String m(F f) { return "G.m(F)"; } String m(G g) { return "G.m(G)"; } String k(F f) { return "G.k(F)" + this.m((G)f); } static String s() { return "G.s()"; } } class H extends G { String k(F f) { return "H.k(F)"; } String k(G g) { return "H.k(G)" + super.m((F)g); } } class Polymorphie { public static void main(String[] args) { F a = new F(); G b = new G(); F c = b; // out( b.m() ); // 1 // out( b.m(a) ); // 2 // out( b.m(b) ); // 3 // out( b.m(c) ); // 4 // out( b.k() ); // 5 // out( b.k(a) ); // 6 // out( b.k(b) ); // 7 // out( b.k(c) ); // 8 // out( b.s() ); // 9 // out( b.t() ); // 10 // out( a.m() ); // 11 // out( a.m(a) ); // 12 // out( a.m(b) ); // 13 // out( a.m(c) ); // 14 // out( a.k() ); // 15 // out( a.k(a) ); // 16 // out( a.k(b) ); // 17 // out( a.k(c) ); // 18 // out( a.s() ); // 19 // out( a.t() ); // 20 // out( c.m() ); // 21 // out( c.m(a) ); // 22 // out( c.m(b) ); // 23 // out( c.m(c) ); // 24 // out( c.k() ); // 25 // out( c.k(a) ); // 26 // out( c.k(b) ); // 27 // out( c.k(c) ); // 28 // out( c.s() ); // 29 // out( c.t() ); // 30 // out( ((F)b).m((F)a) ); // 31 // out( ((G)c).m((F)b) ); // 32 // out( ((F)c).m(c) ); // 33 // out( ((F)b).k((F)b) ); // 34 // out( ((G)a).k(c) ); // 35 // out( ((G)c).k((G)c) ); // 36 H h = new H(); // out( h.k() ); // 37 // out( h.k(a) ); // 38 // out( h.k(b) ); // 39 // out( h.k(c) ); // 40 // out( ((F)h).k(b) ); // 41 // out( ((F)h).k(c) ); // 42 // out( ((G)h).k(a) ); // 43 // out( ((G)h).k(b) ); // 44 // out( ((G)h).k(c) ); // 45 // out( ((G)h).k((G)c) ); // 46 // out( h.k(h) ); // 47 // out( h.s() ); // 48 a = h; // out( ((H)a).k(b) ); // 49 // out( ((H)a).k(a) ); // 50 // out( ((H)a).m(h) ); // 51 // out( ((G)a).k(h) ); // 52 // out( a.k(a) ); // 53 // out( a.m(a) ); // 54 // out( a.m(h) ); // 55 } // nur zur Ausgabe: static int call = 49; public static void out(String str) { System.out.println(call++ + ": " + str); } /* Lösung: 1: G.m()F.s() 2: G.m(F) 3: G.m(G) 4: G.m(F) 5: F.k()G.m(F) 6: Exception 7: F.k(G) 8: G.k(F)G.m(G) 9: G.s()F.t() 10: F.t() 11: F.m()F.s() 12: F.m(F)F.s() 13: F.m(F)F.s() 14: F.m(F)F.s() 15: F.k()F.m(F)F.s() 16: Compiler-Fehler 17: F.k(G) 18: Compiler-Fehler 19: F.s() 20: F.t() 21: G.m()F.s() 22: G.m(F) 23: G.m(F) 24: G.m(F) 25: F.k()G.m(F) 26: Compiler-Fehler 27: F.k(G) 28: Compiler-Fehler 29: F.s() 30: F.t() 31: G.m(F) 32: G.m(F) 33: G.m(F) 34: Fehler 35: Exception 36: F.k(G) 37: F.k()G.m(F) 38: H.k(F) 39: H.k(G)G.m(F) 40: H.k(F) 41: H.k(G)G.m(F) 42: Compiler-Fehler 43: H.k(F) 44: H.k(G)G.m(F) 45: H.k(F) 46: H.k(G)G.m(F) 47: H.k(G)G.m(F) 48: G.s() 49: H.k(G)G.m(F) 50: H.k(F) 51: G.m(G) 52: H.k(G)G.m(F) 53: Compiler-Fehler 54: G.m(F) 55: G.m(F) */ }