This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting required?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting required??" Watch "casting required??" New topic

casting required??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
class Super { }
class Sub extends Super { }
public class TestClass
public static void main(String[] args)
Super s1 = new Super(); //1
Sub s2 = new Sub(); //2
s1 = (Super) s2; //3

Ans : It will compile and run without any errors
Explaination : SubClass can ALWAYS be assigned to a Super Class without any cast.
Does this sentence mean the line//3 given in the code can be replaced by saying :
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6528

Yes, but you could have easily figured that out yourself by deleting the cast and recompiling the code. For future reference, please read this thread for general posting guidelines:
Thanks for your cooperation.
Junilu Lacar
(off-duty barkeeper)
I agree. Here's the link:
subject: casting required??
jQuery in Action, 3rd edition