/** * Fail Loomad_ClassCastException.java * @author Jaanus Poial * @version 0.1 kevad 99 */ //================================================= // Na"ide ka"itusaegsest erindist (kompileerub Ok) //================================================= class Loomad_ClassCastException { // pohiprogramm static public void main (String[] argumendid) { System.out.println ("Mida loomad ytlevad?"); Loom minuLoom = new Loom ("Patu"); System.out.println (minuLoom); //kasutab toString() meetodit Kass minuKass = new Kass ("Kiti"); System.out.println (minuKass); minuLoom = minuKass; System.out.println (minuLoom); Koer minuKoer = new Koer ("Muri"); System.out.println (minuKoer); minuLoom = minuKoer; minuKass = (Kass) minuLoom; //vea tekkepo~hjus on siin System.out.println (minuKass); } //main lopp } //Loomad.... lopp class Loom { String nimi; Loom (String s) { //konstruktor paneNimi (s); } public String votaNimi() { return nimi; } public void paneNimi (String s) { nimi = s; } public String toString() { //katame yle return "Olen loom " + votaNimi(); } } // Loom lopp class Kass extends Loom { //alamklass int vurrupikkus; Kass (String s) { super (s); } public String toString() { //katame kaetu veel yle return "Olen KASS " + votaNimi(); } } // Kass lopp class Koer extends Loom { //teine alamklass boolean sabaga; Koer (String s) { super (s); sabaga = true; } } // Koer lopp //faili lopp