aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Marcus Green's Exam 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Marcus Green Watch "Question on Marcus Green New topic
Author

Question on Marcus Green's Exam 1

bryce johnson
Greenhorn

Joined: Mar 21, 2003
Posts: 13
On Marcus Green's Exam 1, question 17 says:
What will happen if you attempt to compile and run the following code?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){Base b=new Base();
Sub s=(Sub) b;
}
}
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
The answer was 3.
The reason:
Without the cast to sub you would get a compile time error. The cast tells the compiler that you really mean to do this and the actual type of b does not get resolved until runtime. Casting down the object hierarchy is a problem, as the compiler cannot be sure what has been implemented in descendent classes. Casting up is not a problem because sub classes will have the features of the base classes. This can feel counter intuitive if you are aware that with primitives casting is allowed for widening operations (ie byte to int).

I thought that as long as the casting made since you could would be able to explicitly cast down. Am I wrong. I am having problems and can't run the code.
fuji wara
Greenhorn

Joined: Mar 23, 2003
Posts: 4
hi
sometimes u encounter this, during runtime the casting function fail.
sometimes i encounter this but do not know what is the reason.
somebody please enlighten (any reference)
Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
b can refer to an object of base class or any of the sub-classes down the hierarchy. So it gives no error at compile time. The actual object reference is checked at run time and hence u get class cast exception if there is a mismatch.
Consider the following code..it will compile and run perfectly coz s1,s2 and b all refer to object of Sub


regards<br />Rajeshwari. N
Chetan M
Ranch Hand

Joined: Dec 18, 2002
Posts: 43
Very good example Rajeshwari.
[ March 23, 2003: Message edited by: Chetan M ]

No matter how much you know today, you will have to know more tomorrow.<br /> Hakunamatata !!!
bryce johnson
Greenhorn

Joined: Mar 21, 2003
Posts: 13
It seems that if 'b' is cast to Sub, then there should be no problem because Sub extends Base and they are basically the same class. There should be no missmatch. Right?
Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
Originally posted by bryce johnson:
It seems that if 'b' is cast to Sub, then there should be no problem because Sub extends Base and they are basically the same class. There should be no mismatch. Right?

Sub extends Base but they are not the same classes. Sub inherits all the properties of Base but it may define its own additional properties which are unique to Sub and the Base has no way of knowing it.
 
jQuery in Action, 2nd edition
 
subject: Question on Marcus Green's Exam 1