*
The moose likes Beginning Java and the fly likes casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting" Watch "casting" New topic
Author

casting

neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31

It gives a runtime exception, but i am wondering why there isn't compilation error ?

class BlackInk doesn't implement Printable interface neither explicitly nor implicitly so this casting was explicitly impossible.Why compiler doesn't bother about that ?
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hi Neha,
Compiler will always bother to check type safety but you have explicitly mentioned here not to check
printable = (Printable) blackInk;

check in eclipse you will have 4 quick fixes suggested. Fourth one will be "Let BlackInk implement Printable"
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
If i do cast to anything explicitly,is it mean compiler shouldn't bother? i don't accept this.i assume compiler should allow compilation if there is chance of polymorphic behavior and it is only possible through inheritance or interface implementation.Whereas there is no connection of Class BlackInk and Interface Printable.

Printable p=(Printable)new BlackInk(); shouldn't compile even if do explicit casting because BlackInk is not a subclass of interface.


error: inconvertible types
A a=(A)new B();
^
required: A
found: B
1 error

Then why this above code doesn't compile ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


The reason that it is allowed is ... it is possible to have an instance that is both IS-A BlackInk and IS-A Printable. If it wasn't possible, say if the BlackInk class was declared as final, then the compiler will complain.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Henry Wong wrote:
The reason that it is allowed is ... it is possible to have an instance that is both IS-A BlackInk and IS-A Printable. If it wasn't possible, say if the BlackInk class was declared as final, then the compiler will complain.
Henry

Then it should be also possible with my last example.Why it isn't possible for that ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

neha. bajaj wrote:Then it should be also possible with my last example.Why it isn't possible for that ?


In your last example, it is *not* possible for an instance to be both IS-A A and IS-A B. Care to elaborate how it would be possible?

Henry
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Henry Wong wrote:
neha. bajaj wrote:Then it should be also possible with my last example.Why it isn't possible for that ?


In your last example, it is *not* possible for an instance to be both IS-A A and IS-A B. Care to elaborate how it would be possible?

Henry


I am confused."it is possible to have an instance that is both IS-A BlackInk and IS-A Printable." how it is possible please give me an example for this.if BlackInk class implements Printable then it was fine to assign BlackInk class object to Printable interface reference variable but there is nothing like that in my code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

neha. bajaj wrote:
I am confused."it is possible to have an instance that is both IS-A BlackInk and IS-A Printable." how it is possible please give me an example for this.thanks



For your first example, if you create a class that extends the BlackInk class and implements the Printable interface, it is possible to instantiate objects of this new class -- objects which are both IS-A BlackInk and IS-A Printable.

For your second example, since Java doesn't support multiple inheritance, it is *not* possible to create a class that extends the A class and extends the B class -- and hence, not possible to create objects which are both IS-A A and IS-A B.

Henry
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
thanks Henry.. now i got it. it was tricky for me.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

neha. bajaj wrote: thanks Henry.. now i got it. it was tricky for me.


Glad to help...

Henry
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Henry Wong wrote:
neha. bajaj wrote: thanks Henry.. now i got it. it was tricky for me.


Glad to help...

Henry


You always give quality answers than others.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting
 
Similar Threads
Reference variable casting mechanism
Accessing an object using a reference variable of an unrelated Interface
Question #1 from Doug's book
type conversion
why doesnt this compile?