This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry.. now i got it. it was tricky for me.
 
Henry Wong
author
Marshal
Pie
Posts: 21116
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neha. bajaj wrote: thanks Henry.. now i got it. it was tricky for me.


Glad to help...

Henry
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic