• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting required??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic