So since all the classes are in the same hierachy, and all casts were marked explicitly then I believed the above code would compile correctly.
LINE 3 would run as it is explicitly cast to a Derived type.
The cast will result in no compilation error, but there will be a ClassCastException as you are trying to convert an object of super type to sub type. This is not allowed at run time. Try to make some codes and experiment with this...
So basically what you are saying is that if the classes are in the same hierarchy (IS-A or implements) then with explicit casts there should not be any compliler error. If I did someting like:
Inter i = (String)b;// LINE 1
This would give a compiler error.
Now in regards to LINE 3 the error is there because the object type is Base and its trying to cast it to a subtype. LINE 1 is ok as 'b' is of type Base, LINE 2 is ok as 'd' is being cast to a supertype and LINE 4 is ok as 'i' is already a Base object.
Yes you are right. Casting in same hierarchy doesn't gives a compilation error but if you cast an object (by object I mean actual object and not reference) of super-type to sub-type, there will be a ClassCastException...