• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
subhadra tatavarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you never actually "convert the type of the object" itself. You only change the type of the reference to that object.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic