aspose file tools*
The moose likes Java in General and the fly likes Inheritance heirarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance heirarchy" Watch "Inheritance heirarchy" New topic
Author

Inheritance heirarchy

Sophie Angela
Ranch Hand

Joined: Jul 17, 2006
Posts: 46
class base
{
String s="jai";
Object o=s;
Object k=new Object();
String h=(String)o;
String s=o;/* can this be done? */
}
In the above coding.what is the difference between objects o and k.What will be the pointing.?Can i cast the object o to a string?If so what will be the contents of the objects o and h..?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
...Can i cast the object o to a string?If so what will be the contents of the objects o and h..?
What happened when you compiled/ tried to execute your code. The quickest way to find out if something can be done is to try and do it.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
Hello.

First of all, there are two problems with your code.
1. You are declaring String variable s twice which will fail compilation.
2. You are trying to assign a wider type of var to a narrower type which will again fail at compilation.

According to java rules, you cannot assign a broader type variable to narrower type variable. In this case you are trying to assing s=o where s is a String and o is an object which is a superclass of String. Although "o" contains a string but it is illegal to assign it to String type without a type cast.

In the 2nd line, Object o=s, you are assigning a String (which is also an object) to an Object which is perfectly legal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance heirarchy
 
Similar Threads
castNotSupportedException
How to pass Arraylist to the JSP from the Action Class
Doubt in arrays
JLS understanding...
How Are References Represented in Memory?