This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
If I uncomment the line in the main, code does not compile. Could someone please explain the reason. Explanation in the test says since it is final class and it does not implement interfae I, that's why it does not compile. I know that final class can not be overridden but I am clear about this explanation.
Originally posted by Himanshu Gupta: I will try to point out the logic which i got from the above discussion:
We can cast user defined class to any user defined interface. (Explicit casting necessary. There will be no compiler error but will through ClassCastException if it fails to pass 'IS A' test.)
We cannot cast already present(Java API) class to already given Interfaces as their relationship is already defined.
There is no special magic between the core Java classes and user defined classes in regard to this. The compiler does not make special exceptions -- it checks for legallity of casting, in the same way, in regards to both.
Now it means that casting can be done anywhere and anyhow.Its upon the developer to check that it will adhere to the 'IS A' relation or not. It is applicable when programming with interfaces.(Extra care has to be given for final classes).
Moreover if we code by using classes references then we do have to take care of the 'IS A' relation even at the compile time.
Am i right this time? Please help.
Thanks Henry for correcting me. [ November 21, 2008: Message edited by: Himanshu Gupta ]