• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic