public class BeispielVererbung { public static void main(String[] args) { Baum b = new Baum(); b.alter = 10; // Baum enthält auch alter aus Pflanze b.hoehe = 2; // Baum hat eine eigene Variable hoehe, // die in Pflanze noch nicht enthalten ist. Pflanze p = new Pflanze(); p.alter = 1; // Variable alter aus Pflanze // p.hoehe = 2; // Geht nicht, da hoehe nur in Baum // vorkommt. } } class Pflanze { public int alter; } // Baum ist eine spezielle Pflanze, und ERWEITERT (extends) die // Kategorie Pflanze daher um zusätzliche Eugenschaften. class Baum extends Pflanze { public int hoehe; } // Vergleich mit structs in C: // // struct Pflanze // { // int alter; // }; // // struct Baum // { // int alter; // WIE in Pflanze, aber DIREKT in Baum // int hoehe; // NUR in Baum // };