aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions about casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions about casting" Watch "Questions about casting" New topic
Author

Questions about casting

frank yang
Greenhorn

Joined: Feb 19, 2002
Posts: 22
public class SuperClass{
class SubClassA extends SuperClass{}
class SubClassB extends SuperClass{}
public void test(SubClassA foo){
SuperClass bar=foo;
}
}
which statement is true about the assignment in line 5?
A. The assignment in line 5 is illegal
B. The assignment in line 5 is legal, but throw a ClassCastException
C. legal and will always executes without throw an Exception
Which answer is correct?
Who can explain and give an example to run this code?
Thanks a lot
[ February 17, 2003: Message edited by: frank yang ]

Look at me and hear me<br />Because I am here<br />Just for that
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
I believe that the correct answer is C. See 5.1.4 Widening Reference Conversions
where S is short for source (here SuperClass bar)and T is short for target (here SubClassA foo).
[ February 17, 2003: Message edited by: Dan Culache ]
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
PS. What do you mean by "..give an example to run this code?"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about casting
 
Similar Threads
static block and empty block inside any class.
��Question, Class��
Accessibility Example from Khalid Mughal-getting compilation errors
Abstract class with main method