• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Selftest10 {
public static void main(String[] args)

{
class Horse {
public String name;
public Horse(String s)

{
name = s;
}
}
Object obj = new Horse

("Zippo");
Horse h = (Horse) obj;
System.out.println(h.name);
}
}
Can you explain why iam getting an error of casting here
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
sravanthi
i have executed your program i am not getting any error
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its showing incomaptible types
why is that so??
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pulukuri,

Code just compiles fine and gives the desired result.
If you think, error must come, ask yourself why?

From my part, everything is fine there.



Thanks and Regards.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have changed the class name as self
i got this output

C:\practice>javac Self.java

C:\practice>java Self
Zippo

check it once
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pulukuri,


Post the error generated by the compiler, including the line number!
Do you have a class named "Object" (in the current directory) or somewhere coming in the classpath. It can be one and only reason as I guess, you program is not getting compiled well.

Thanks and Regards,
cmbhatt
[ April 02, 2007: Message edited by: Chandra Bhatt ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

You got it Correct. She maight have Object class of her own. thats why casting fails.

Good yaar.
 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
What chandra said is absolutely correct, i had other class file with same name,now its working fine
Thanks so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic