This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

casting

 
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 ?
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • 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 ?
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!