Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

casting

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry.. now i got it. it was tricky for me.
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic