Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting required??

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :
s1=s2;
Sonir
 
Junilu Lacar
Bartender
Posts: 7483
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://www.javaranch.com/ubb/Forum24/HTML/013803.html
Thanks for your cooperation.
---
Junilu Lacar
(off-duty barkeeper)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic