It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Object" Watch "Casting Object" New topic

Casting Object

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
public class abc{
int i= 34;
void check(){
System.out.println("Inside the parent method");
class def extends abc{
int i = 44;
void check(){
System.out.println("Inside the child method");
public static void main(String args[]){
abc a = new abc();
def d = new def();
a = d;

"This code compiles fine but giving runtime error. Can anybody explain me.
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi Shailender,
The code is perfect and does run on my m/c.
The mistake u seem to be doing is that instead of running the class def i.e. java def
u may be using java abc
which may be resulting in main :nosuchmethoderror in class abc.error at runtime.
so don't forget that u can run only that class which has got the main method correctly defined.
hope i guessed your problem right.
Ranch Hand

Joined: Aug 03, 2000
Posts: 169
Can u help me understand object reference casting? I read in RHE
but still confused. I think u could help me.
- Thanks
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by deekasha gunwant:
Thanx, Deekasha, u r right, The problem was with file name only. Now it is working fine. Thanx once again.
I agree. Here's the link:
subject: Casting Object
jQuery in Action, 3rd edition