• 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

can you explain me classCastException ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this line throws an class cast exception
i know that class cast exception occurs when you try to cast an object that is not an instance of that class
but here clark is an instance of Employee class

and the big thing is it cast an employee to manager in the test() with no class cast exception so , why it cannot do the same in main ()

 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Employee is a superclass of clark. This means you cannot cast from employee to clark. For example I can't cast from Animal to Dog unless my variable happens to be a Dog. If it is an Animal directly, this doesn't makes sense.

Also, it is a convention for class name to being with a capital. So it would be Clerk. (also fixing the typo for the vowel.)
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

Employee is a superclass of clark. This means you cannot cast from employee to clark. For example I can't cast from Animal to Dog unless my variable happens to be a Dog. If it is an Animal directly, this doesn't makes sense.

Also, it is a convention for class name to being with a capital. So it would be Clerk. (also fixing the typo for the vowel.)


thanks for the reply , but if this cant be done then i suppose this
should also throw the classCastException

but this code doesnt throw any classCastException , now what you wanna say ?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no problem assigning null to a Clerk reference. null always satisfies "IS-A".

I suppose one way of looking at it is to say that while it's not true to say that an Animal IS-A Dog, a hole where an Animal could be is also a hole where a Dog could be.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:There's no problem assigning null to a Clerk reference. null always satisfies "IS-A".

I suppose one way of looking at it is to say that while it's not true to say that an Animal IS-A Dog, a hole where an Animal could be is also a hole where a Dog could be.


  • object ref is null not clarks
  • if so , then why we dont get classcastException in test()

  • i just want to ask why it doesnt throw class cast exception in test()
    and throws classCastException when i do same thing which i have done in the first if statement under test() and that is

    can any one explain me why this both things are considered different ?
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You don't get a ClassCastException if the object is actually of the type you're trying to cast it to. In your code there, you are checking whether the Employee referenced by e is actually a Manager (using instanceof). If that's true, then the cast is safe. If it's not true, then the cast isn't even attempted.

    Same with Clerk - because you're using instanceof, you're checking that the cast is safe before attempting it. If it's not safe, you skip it. Using instanceof like this is a way to avoid ClassCastExceptions.

    The difference in your original main method is that you're not carrying out this safety check...and then you create a new Employee(), which can't be cast to a Manager.
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matthew Brown wrote:You don't get a ClassCastException if the object is actually of the type you're trying to cast it to. In your code there, you are checking whether the Employee referenced by e is actually a Manager (using instanceof). If that's true, then the cast is safe. If it's not true, then the cast isn't even attempted.

    Same with Clerk - because you're using instanceof, you're checking that the cast is safe before attempting it. If it's not safe, you skip it. Using instanceof like this is a way to avoid ClassCastExceptions.

    The difference in your original main method is that you're not carrying out this safety check...and then you create a new Employee(), which can't be cast to a Manager.


    but at the end of the day ..can i cast a base class to its super class ?
    like
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    naved momin wrote:but at the end of the day ..can i cast a base class to its super class ?


    Careful with your terminology - "base class" and "super class" are the same thing.

    To clarify: you can cast a reference to a class to a reference to its superclass - but it's not necessary, as those casts are always safe and so can be done implicitly:

    You can cast a reference to a class to a reference to its sub class, but whether it works or not (i.e. whether you get a ClassCastException or not) depends on the actual object:
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matthew Brown wrote:

    naved momin wrote:but at the end of the day ..can i cast a base class to its super class ?


    Careful with your terminology - "base class" and "super class" are the same thing.

    To clarify: you can cast a reference to a class to a reference to its superclass - but it's not necessary, as those casts are always safe and so can be done implicitly:

    You can cast a reference to a class to a reference to its sub class, but whether it works or not (i.e. whether you get a ClassCastException or not) depends on the actual object:


    thanks for this
    but i wanted to ask you , it works fine for the reference
    this works fine

    but why this isnt working i wanted to know the reason ,
    what i know is that employee is a super class and manager is a derived class , so it should be safe to cast a derived class to a super class ? dont you think ? ya vice versa is not possible i know that

     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    naved momin wrote:what i know is that employee is a super class and manager is a derived class , so it should be safe to cast a derived class to a super class ? dont you think ?


    But that's not what you're doing!

    naved momin wrote:


    That is casting a superclass (Employee) to its derived class (Manager) - the opposite of what you're saying you're doing.
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matthew Brown wrote:

    naved momin wrote:what i know is that employee is a super class and manager is a derived class , so it should be safe to cast a derived class to a super class ? dont you think ?


    But that's not what you're doing!

    naved momin wrote:


    That is casting a superclass (Employee) to its derived class (Manager) - the opposite of what you're saying you're doing.


    oh i see thanks dude for making me correct ...thanks once again
     
    I can't take it! You are too smart for me! Here is the tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic