• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Object

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
System.out.println(a.i);
a.check();
}
}

"This code compiles fine but giving runtime error. Can anybody explain me.
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
regards
deekasha
 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deekasha,
Can u help me understand object reference casting? I read in RHE
but still confused. I think u could help me.
- Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
--Shallender
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic