Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why doesnt this compile?

 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here at compile time line 1 doesnt compile....why?
some time back we had discussed that the compiler allows this to compile because at runtime test_one can refer to one of its subclasses which implements face....
 
Rein de Boer
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as the compiler is concerned, test_one does not implement face (only test40), so cannot be assigned to face.
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think we had discussed that casting between a class and an interface which doesnt implement that interface is legal....
 
rohan yadav
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it because of downcasting, i am not sure about this, as you downcast your tester class to test40 class.
Thats why its giving comilation error if you try following, it works,

 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this works because both are compatible with each other...the one before did not because they werent....

Ans: we cant assign one thing to another that arent in the hierarchy.... but casting between a class and an interface that arent is legal
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:


We have to cast explicitly.


Minhaj
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because it is determined at runtime that test_one actually refers to tester class' object.
compiler doesn't know about it and as test 40 doesn't implements face, the code doesn't compile.

explicitly casting test_one to face will work here.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic