aspose file tools*
The moose likes Beginning Java and the fly likes how can we convert the type of the object using type casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can we convert the type of the object using type casting" Watch "how can we convert the type of the object using type casting" New topic
Author

how can we convert the type of the object using type casting

subhadra tatavarthy
Greenhorn

Joined: Jul 04, 2010
Posts: 5
object is the main class string and stringbuffer are the child classes

String s =new String("java");
object o=(object)s


Rule: A b=(C)d


Rule1: the type C and d must hava some relationship either same type or parent to child or child to parent, otherwise we will get compile time error

Rule2: C must be same are derived type od A other we will get compile time error

Rule3: the underlying object type of d must be same or derived type of c other wise we will get run time exception




any one can explain me in short and easy method
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

subhadra tatavarthy wrote:
Rule1: the type C and d must hava some relationship either same type or parent to child or child to parent, otherwise we will get compile time error

Correct. more specifically C and d both must be a class type. what happen if any one of them is interface?

subhadra tatavarthy wrote:
any one can explain me in short and easy method

almost you are clear with points. what example would you expect ? I can tell you one thing.*instanceof* operator behave same like what you explained above.

hth
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that you never actually "convert the type of the object" itself. You only change the type of the reference to that object.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

I'd be a little cautious on your Rule 1.

class Grandparent {}

class Parent extends Grandparent {}

class Child extends Parent {}

Parent p = new Parent();

Child c = (Child) p; //this won't work


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

To answer your original question "how can we convert the type of the object using type casting": You cannot.

Casting is not a way to convert objects from one type to another. Since String and StringBuffer are two different (unrelated via inheritance) classes, there is no way to convert a String to a StringBuffer (or vice versa) via casting.

The only thing that casting is, is a way to tell the compiler: "look, I have a variable that refers to an object of type X here, but the object is really of type Y, so I want you to treat it as if it's an Y". But this does not automatically somehow convert an X to an Y - if the object really isn't an Y, you will get a ClassCastException at runtime.

For example, suppose that you have a method that for some reason has Object as its return type:

Suppose that you want to call this:

Now result is of type Object, but you know that the method really returns a String. You can cast the result to String:

Now result is a String, despite that the method says that it returns Object.

Note that if you cast it to something else, for example a StringBuffer, you'll get a ClassCastException:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can we convert the type of the object using type casting