Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Casting Objects

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code listed below compiles fine but fails on execution giving the following the error message.
Exception in thread "main" java.lang.ClassCastException: Animal
at CastExample.main(CastExample.java:37)
Could someone explain why this is happening.
Thanks
Arifa
import java.io.*;
import java.lang.*;
class Animal
{
public void Barks()
{
System.out.println("Animal Barks");
}
}
class Dog extends Animal
{
public void Barks()
{
System.out.println("Dog Barks");
}
}
class Spaniel extends Dog
{
public void Barks()
{
System.out.println("Spaniel Barks");
}
}
public class CastExample
{
public static void main (String args[])
{
Spaniel aPet = new Spaniel();
Animal theAnimal = aPet;
theAnimal.Barks();
Animal myAnimal = new Animal();
Dog myDog = (Dog)myAnimal;
myDog.Barks();
}
}


 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like this is due to the last three lines in main(). True, a Dog instance is an Animal instance, but the reverse is not true in this case. The cast that narrows the Animal instance to the Dog instance is incorrect.
 
Doug Gschwind
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should have elaborated a bit more.
No cast is needed to widen from a Dog instance to an Animal instance as the first three lines show in main(). However, to narrow from an Animal instance to a Dog instance requires that the Animal instance actually be a Dog or Spaniel instance to be a legal narrowing.
In addition, when narrowing, the instanceof operator should be put to use. e.g.
Dog spanky = new Dog();
doSomethingMethod(spanky);
public void doSomethingMethod(Animal a)
if (a instanceof Dog)
{
// Here the typecast will not cause a run time type exception.
// a must be non-null as well for the condition to test true.
Dog d = (Dog) a;
}
}
 
Arifa Riaz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Doug.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic