Saturday, 12 April 2014

Class Cast Exception


class Animal {
    public void eat(String str) {
        System.out.println("Eating for grass");
    }
}

class Goat extends Animal {
    public void eat(String str) {
        System.out.println("blank");
    }
}

class Another extends Goat{
  public void eat(String str) {
        System.out.println("another");
  }
}

public class InheritanceSample {
    public static void main(String[] args) {
        Animal a = new Animal();
        Another t5 = (Another) new Goat();
    }
}



o/p:
D:\anvesh>java InheritanceSample
Exception in thread "main" java.lang.ClassCastException: Goat cannot be cast to
Another
        at InheritanceSample.main(InheritanceSample.java:22)

No comments:

Post a Comment