• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Would someone kindly look at the code below and explain why a class cast exception is thrown at runtime. Thank you!!

class SuperTest {
}

class SubTest extends SuperTest {
}

public class Inheritance {
public static void main(String[] args) {
SubTest subRef = (SubTest) new SuperTest();
// THE ABOVE LINE CAUSES A ClassCastException AT RUNTIME
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The question that we should be asking you is... why do you think that it should *not* throw a class cast exception ?

Henry
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JavaDoc
Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance

Can you apply the IS-A test here? Is SuperTest an instance of SubTest?

Details here:
http://java.sun.com/javase/6/docs/api/java/lang/ClassCastException.html
 
Ali Sadiq
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info. I guess i need to do more reading on "casting". btw, i changed my screen name.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing you learn is that class-casting is hazardous, risking Exceptions. Another thing you learn is that class-casting is totally different from casting primitives.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
One thing you learn is that class-casting is hazardous, risking Exceptions. Another thing you learn is that class-casting is totally different from casting primitives.



This is why, I think, it is a good reason to use the instanceof keyword before casting.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just changed the names of the class, but structure is same.

here InheritenceS is base class and SubTest is derieved class ,so in Inheritance class you were doing down casting which is not possible so, i have changed one line in Inheritance class, i.e. i have used Up casting now its working fine.
And bug less code is here.


class InheritenceS {
}

class SubTest extends InheritenceS {
}

public class Inheritance {

public static void main(String[] args) {


InheritenceS subRef = (InheritenceS) new SubTest();//yashpal changed this line

// THE ABOVE LINE CAUSES A ClassCastException AT RUNTIME
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic