File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes question  about object casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "question  about object casting " Watch "question  about object casting " New topic
Author

question about object casting

maary bill
Greenhorn

Joined: Feb 01, 2012
Posts: 2
hi every body I just want to know what happen when we do an casting in object does it change in it structure or in it values ???
ex: suppose fruit is object of super class Fruit and apple is object of sup class Apple what happen if we do that
fruit = apple //this an implict casting it same fruit = (Fruit)apple;

apple = (Apple)fruit;
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Object is generic
Fruit is less generic, some what specific
Apple is more specific

So,
when you refer Apple as Object ... you can only know that its an object and its having a hashcode
when you refer Apple as Fruit ... you know that its an Apple and may know if its a seeded fruit or jello type depending on the properties of the class Fruit
when you refer Apple as Apple ... you know that its an Apple and if its a seeded fruit or jello type and you may calculate its nutrient values using its own methods

Basically, nothing in structure changes... but it depends on what you can see from your point. Programatically, if the referring class has a property / method of same type and signature and it is one of the parent classes of the assigned object, it can access the assigned objects property / method.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

maary bill wrote:hi every body I just want to know what happen when we do an casting in object does it change in it structure or in it values ???

Casting one reference type to another reference type does not do any kind of conversion. So no, it does not change the structure or the value of what you are casting.

Casting is nothing more than telling the compiler that you know better than the compiler what the actual type of an object is. Suppose that you have a class Fruit and a subclass Apple. Somewhere in your code, you create an Apple like this:

You can do this without a cast because an Apple is a Fruit (Apple is a subclass of Fruit).

Suppose that later on in the program you need to access some specific method of class Apple on todaysFruit, and you happen to know that todaysFruit is really an Apple, even though the type of the variable is Fruit and not Apple. You could then cast it:

What you are doing here is telling the compiler: "I have something here that you think is a Fruit, but I know that it is an Apple, so treat it as if it is an Apple." Note that the check if it really is an Apple is still done at runtime; so when you run the program and Java notices that the object is in fact not an Apple, you will get a ClassCastException.

So, a cast defers a type check from compile time to runtime. You are giving up some of the type safety that the compiler provides. In principle you should avoid casts as much as possible because of this. The less casts you have, the less chance that your program will crash with an unexpected ClassCastException.

With primitive types, casting works a little differently; primitive types are indeed converted when you do a cast. For example when you cast a float to an int, the value of the float is converted to an integer (and you loose the digits after the decimal point).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
maary bill
Greenhorn

Joined: Feb 01, 2012
Posts: 2
thank very much to tarun and jesper but iwant to know what happen here whan we use comparable interface


// Max.java: Find a maximum object

public class Max {
/** Return the maximum of two objects */
public static Comparable max
(Comparable o1, Comparable o2) {
if (o1.compareTo(o2) > 0)
return o1;
else
return o2;
}

//this is in main

String s1 = "abcdef";
String s2 = "abcdee";
String s3 = (String)Max.max(s1, s2); //what happen here it ligal

}

//or this
// Max.java: Find a maximum object


public class Max {
/** Return the maximum of two objects */
public static Object max
(Object o1, Object o2) {
if (((Comparable)o1).compareTo(o2) > 0)
return o1;
else
return o2;
}

//this is in main
Date d1 = new Date();
Date d2 = new Date();
Date d3 = (Date)Max.max(d1, d2);

}

and thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

maary bill wrote:thank very much to tarun and jesper but iwant to know what happen here whan we use comparable interface


You mean here?


(In the future, please UseCodeTags.(⇐click)

It's exactly what Jesper just told you. You're telling the compiler, "Even though you know o1 as type Object, I want you to treat it as Comparable." The compiler allows it because it's possible the object could be a Comparable. (Remember implementing an interface is inheritance, and it's an IS-A relationship, just like extending a class.) And it's fine at runtime if the object pointed to actually happens to BE a Comparable.

So what are you not understanding?
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
If you want to know how to use the references, read on, you know that every class inherits Object class. The problem is Object class is too generic to use in a real app. So you create classes according to your need. Now, since Object class is a parent class of your "created classes", you can refer the objects of your classes (the app classes you have created) with Object class reference. But Object class doesn't know any of the new properties in your app class. So you must use a reference that knows more about your app class object to refer the property. That's where the casting comes into picture. You cast the object into a more specific type to pickup the properties in it. Same applies to methods too.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Tarun Bolla wrote:You can put it this way.


Huh? Are you responding to me? If so, then, yes, I can put it that way because that is correct and accurate.

Or did you just accidentally hit post before you finished composing your reply?
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Yeah Jeff, I accidentally clicked on the submit button.
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
My previous comment was about using references. And regarding execution, its an entirely different story.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about object casting