aspose file tools*
The moose likes Java in General and the fly likes can you explain me classCastException ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "can you explain me classCastException ?" Watch "can you explain me classCastException ?" New topic
Author

can you explain me classCastException ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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 ()



The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158



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.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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 ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4465
    
    8

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

Joined: Jul 03, 2011
Posts: 692

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

    Joined: Apr 06, 2010
    Posts: 4465
        
        8

    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

    Joined: Jul 03, 2011
    Posts: 692

    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

    Joined: Apr 06, 2010
    Posts: 4465
        
        8

    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

    Joined: Jul 03, 2011
    Posts: 692

    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

    Joined: Apr 06, 2010
    Posts: 4465
        
        8

    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

    Joined: Jul 03, 2011
    Posts: 692

    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
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: can you explain me classCastException ?