Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
    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

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
class Animal
{
public String noise()
{
return "animal";
}
}

class Dog extends Animal
{
public String noise()
{
return "dog";
}
}

class Cat extends Animal
{
public String noise()
{
return "cat";
}
}

class Demo
{
public static void main(String args[])
{
Animal a1 = new Dog();
Cat ca = (Cat)a1;
System.out.println(ca.noise());
}
}

Output is:
Exception is thrown at run time
Can anybody explain this please?...
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi ,

As per the above example Cat and Dog have no relation ship and both belong to Animal category so RuntimeException occurs ( Cat ca = (Cat)a1 because your trying to assign dog object to cat (Since Animal pointing to Dog)
and another point is that you can't assign a super class object to a sub class even thought you type cast it.

for Ex: Animal a1 = new Animal();
Cat ca = (Cat)a1;
 
srinivas.b
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi ,

As per the above example Cat and Dog have no relation ship and both belong to Animal category so RuntimeException occurs ( Cat ca = (Cat)a1 because your trying to assign dog object to cat (Since Animal pointing to Dog)
and another point is that you can't assign a super class object to a sub class even thought you type cast it.

for Ex: Animal a1 = new Animal();
Cat ca = (Cat)a1;
 
srinivas.b
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi ,

As per the above example Cat and Dog have no relation ship and both belong to Animal category so RuntimeException occurs ( Cat ca = (Cat)a1 because your trying to assign dog object to cat (Since Animal pointing to Dog)
and another point is that you can't assign a super class object to a sub class even thought you type cast it.

for Ex: Animal a1 = new Animal();
Cat ca = (Cat)a1;
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
let's close this duplicate...
 
Happily living in the valley of the dried frogs with a few tiny ads.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    Bookmark Topic Watch Topic
  • New Topic