File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can somebody help me ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can somebody help me ?" Watch "Can somebody help me ?" New topic

Can somebody help me ?

Milind Mahajan
Ranch Hand

Joined: Oct 23, 2000
Posts: 77
This is the first time I am posting a message.I have been going through the topics discussed in this forum for a couple of weeks and it has helped me a lot( Specifically tips from people who have passed SCJP exam after October 4 and a discussion about weird behaviour of ++ operator. I was tired of searching a logical answer for that when, I quite accidentally, ran into it in Javaranch ). Hopefully I will be taking SCJP exam in November first week. I have some doubt about the following Java code. Can somebody help me ?
1. class Base {
//some stuff
2. }
3. interface I {
4. }
5. class Derived extends Base implements I {
6. public static void main(String args[]){
7. Base base = new Base();
8. Derived derived = new Derived();
9. base = derived;
10. I i = (I)base;
11. System.out.println("Fine");
12. }
The code is running fine. But I had expected it throw run time ClassCastException at line 10 because class Base does not implement I. When I swapped line 9 and 10 runtime ClassCastException is being thrown. So it seems that because reference of Derived class is being held in refernce of Base class, JVM is not throwing runtime exception. But I have read in many books that objects 'know' who they are. So, at runtime base class reference 'base' knows that it is Base class object and does not implement I. Hence, JVM should throw an exception. It doesn't. What is the explanation ?
Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
the base variable now points to an Derived object which the JVM comes to know at the run-time only. hence since derived implements I, there is no error.
hope this helps...

Milind Mahajan
Ranch Hand

Joined: Oct 23, 2000
Posts: 77
Thank you very much for your answer Sagar. Can you please find time to explain it ?
I am sorry, but I am not very clear what is happening. What I think is that if object of Base class can behave as if it imlements interface because it is pointing to reference of Derived class object, then what does an object 'knowing' about its class mean ? For example, if a base class reference is pointing to a derived class reference and if we call a method meth() on base class reference, that method is called only if it is overridden in derived class. Any other method, which is implemented by derived class but not implemented/defined in base class will not be called even if base class reference is pointing to derived class reference because it 'knows' which methods it has.
Also, can somebody tell me under when exactly is ClassCastException thrown ? I don't seem to be very sure about it.
Thanks a lot,
I agree. Here's the link:
subject: Can somebody help me ?
It's not a secret anymore!