• 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

about 'cast',please explain for me

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lookin the code:
public class Test {
public static void main(String s[]) {
Child c =new Child();
Parent p = new Parent();
c=(Child)p;
}
}
class Parent {
public static void doStuff() {
}
}
class Child extends Parent {
public static void doStuff() {
}
} //why will occur the ClassCastException in runtime?I just know the super class can cast sub class,it's right?why can't do that in this case?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have it backwards - the derived class can be cast to the parent because it inherits the variables and methods from the parent.
Bill
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tr100,
Welcome to Javaranch.
Please read the Javaranch Naming Policy and change your publicly displayed name.
Thank you.
[Edited: Thanks Satya]
[ February 09, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I think its enough if you can edit your profile and modify the Publicly Displayed Name. Try this first and if it still doesn't follow the naming conventions, you will have to re-register.
- satya
 
reply
    Bookmark Topic Watch Topic
  • New Topic