• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

casting classes and interface - need some clarification

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I wrote this small code ad now I am really confused.
Here is the code

so why does Face f = (Face) a; compile. What am I missing. and yes it gives ClassCastException.



Thanks in advance.

Anu
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. That's because you can legally cast any class to any non related interface with no problem:

The reason behind this behaviour is that compiler think one of the sub classes of that class might implement the interface so it lets the cast to be done. Same thing with instanceof (try this: a instanceof Face)

BUT if the class is final, then the compiler would not let you to do the cast casue there is no subclass for that final class that might implements the interface:

Integer i2 = 2;
Runnable runabble = (Runnable)i2;

Make sense?
 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understand now. Thank you Morteza for clarifying this.

Anu
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I decided this was really interesting so I tried to explore all the possible combinations of assignment involving superclass, subclass and interfaces.

Please comment/correct me or add more examples if you think they will add to the example.

 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for detailed explanation on all possible assignments.
Anu
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Anderson wrote:I decided this was really interesting so I tried to explore all the possible combinations of assignment involving superclass, subclass and interfaces.

Please comment/correct me or add more examples if you think they will add to the example.



hello ryan ,
good explaination , except that i didn't get the following.

can you please explain what the below would mean in simple words?

Animal a = new Animal();
Horse h = (Horse)a; //COMPILES --> BUT RUNTIME CLASSCASTEXCEPTION

i thought that since horse is an animal , downcasting is possible , then y the classcast exception?? [ correct me if i am wrong]

Animal a1 = new Horse();
Horse h1 = (Horse)a1; //WORKS 100% (downcasting to Horse)

why does downcasting work in this case but not the previous one

 
Ryan Anderson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this confused me at first also and I'm still slightly confused.

So in the first case:

Animal a = new Animal();
Horse h = (Horse)a; //COMPILES --> BUT RUNTIME CLASSCASTEXCEPTION

1) "Animal a" is a Reference variable to an Animal object OR any of Animal's subclasses (Frog,Dog,Cat,etc..)
2) Any object you assign to the Animal reference MUST implement all the methods declared in Animal.....you do not want to do something like a.method() and method is not defined
3) You create "new Animal()" object and assign it to the reference
4) "Horse h" is a Reference variable to a Horse object OR any of Horse's subclasses (Stallion,other types of horses) --> Not its superclasses --> make a special note that a Horse reference cannot refer to a superclass object (ie. Animal object)
5) The compiler will always let you assign a superclass reference (Animal) to Horse WITH an explicit cast --> compiler does not look at the actual object that "a" is referring to --> by casting you are telling the compiler that the object should implement all the methods in Horse --> but it does not check you
6) At runtime --> when you try to do "Horse h = (Horse)a" --> the JVM goes and looks at the object that "a" is pointing to and sees that it is an Animal --> not a Horse
7) It throws an exception because you are basically trying to do the following: Horse h = new Animal() --> this is very illegal because you cannot assign a superclass object to a subclass reference
 
Ryan Anderson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So in the second case:

Animal a = new Horse();
Horse h = (Horse)a; //works fine

1) "Animal a" is a Reference variable to an Animal object OR any of Animal's subclasses (Horse,Frog,Dog,Cat,etc..)
2) Any object you assign to the Animal reference MUST implement all the methods declared in Animal.....you do not want to do something like a.method() and method is not defined
3) You create "new Horse()" object and assign it to the reference
4) "Horse h" is a Reference variable to a Horse object OR any of Horse's subclasses object (Stallion,other types of horses) --> Not its superclasses --> make a special note that a Horse reference cannot refer to a superclass object (ie. Animal object)
5) The compiler will always let you assign a superclass reference (Animal) to Horse WITH an explicit cast --> compiler does not look at the actual object that "a" is referring to --> by casting you are telling the compiler that the object should implement all the methods in Horse --> but it does not check you
6) At runtime --> when you try to do "Horse h = (Horse)a" --> the JVM goes and looks at the object that "a" is pointing to and sees that it is a Horse
7) It is happy because you are basically doing: Horse h = new Horse() --> everything is aok


Please add anything I left out or you think you be helpful.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic