• 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

Casting between classes.

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.

I'm playing around with casting between classes, i get the error: test.NewClass cannot be cast to test.NewClass1 when trying:





 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do you get the error? At compile-time?
Did you think you can cast a NewClass object to NewClass1? You are telling the compiler you are certain it will be a NewClass1 and the compiler replies, “Don’t believe you!”
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand exactly what casting means?

When you cast an object, Java isn't automatically going to convert the object from some type to the type that you're casting it to. Casting only means that you tell the compiler: "I've got an object here, and I want you to treat it as if it is an object of type X". If, at runtime, the object that you're casting turns out to not really be an instance of class X, then you will get a ClassCastException.

In your example you have a superclass NewClass and a subclass NewClass1 (that extends NewClass). You create an instance of NewClass, and then you try to cast it to a NewClass1 object. That is not going to work, because a NewClass object is simply not a NewClass1 object.

Let's give the classes better names, then this will become more clear. Let's call the superclass Animal, and the subclass Dog. You are creating a new Animal, and then you're trying to cast that to a Dog. That obviously is not going to work, because an Animal is not always a Dog.

Keep in mind that inheritance in object oriented programming means specialization, which implies that there is an "is a" relationship going from the subclass to the superclass. In other words: a Dog is an Animal. But the other way around is not necessarily true: an Animal is not always a Dog, it could also be some other kind of animal.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic