aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion" Watch "Conversion" New topic
Author

Conversion

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello friends

i never seen this before, like conversion to Interface class to implementor class or conversation of Super class to Sub Class. I don't get it, meaning by doing this what is the use ? anyway after being casted to subclass they can't really use the members of subclass unless they have 'em (members as sub class). then what is this all about converting object of one type to another type. never come across any use of it. if you know single one, please explain.

here's an example, though it's legal , never generating compiler error but don't know actual use of them.


interface X {}
class Sup implements X {}

public class Face extends Sup {

void display(){System.out.println("Hello");



X x1 = new Face(); // what is the use of doing this
Sup s = new Sup();
Face f = new Face();
s = (Sup)f; // what is use of doing that
x1 = f;
}
}
thanks
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Hi chintan ramavat

In your given code snippet, U are not casting a Superclass reference to a subclass reference..
Instead U are casting a subclass reference to a superclass reference..
Am i write..??
See your code carefully, f is the reference of type face which is subclass of Sup.. And s is the reference of your super class Sup.. And U are casting f(subclass reference) to s(superclass reference) by writing..

s = (Sup)f;


I don't think, there is any need of casting here..
Even if U don't do that, implicit casting will be done by itself..
That doesn't going to make any difference to compiler..
However, now after casting to Sup type, U will try to invoke display() method on s, U will get Runtime Error.. But no error at compile time


rgrds,
Khushhal
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

hi khushal,
could please use real words instead of "U" ? Thank you.

http://faq.javaranch.com/view?UseRealWords


[My Blog]
All roads lead to JavaRanch
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by khushhal yadav:
However, now after casting to Sup type, U will try to invoke display() method on s, U will get Runtime Error.. But no error at compile time


No. It won't compile.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

No. It won't compile.


It compiles properly.. U can try..
I have tried..
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

snippet below is compiling properly..
interface X {}
class Sup implements X {}

public class Face extends Sup {

void display(){System.out.println("Hello");



X x1 = new Face(); // what is the use of doing this
Sup s = new Sup();
Face f = new Face();
s = (Sup)f; // what is use of doing that
x1 = f;
}
}
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

I was refering to calling the method on the superclass reference:

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, ranchers,

may I return to the original question?
What's the use of (...) this and (...) that?


Part of the answer is "Coding to an Interface". Perhaps also google or wiki(pedia) for this. It is a way of using Polymorphy.

this
Instead of your original example:
X x1 = new Face(); // what is the use of doing this?

I'll use a more readable example about the same topic.

Imagine you have:


And you want to have an array, that contains things that swim. The type of the array should be of the interface type (Swimable). You cannot use Boat[] or Fish[] as they both could contain only boats or fish respectively.
Using the interface type Swimable[] ensures that all elements of this array are able to swim.
In a class that uses this class hierarchy, you could have a method that randomly returns a Swimable object, e.g.





that
Why the casting? In the first example of class SwimUser, we only had output from the different swim() methods. That's fine, as you can call method swim() from any object of type Swimable (i.e. of a Swimable implementing classes) directly.
They ARE swimables.
But in the array, there are also ClownFish and you want to know how many stripes they have.
To run method getStripes(), you have to have an object of type ClownFish, because neither Swimable nor Boat no Fish has this method.
Therefor a cast is required. To check if your Swimable is really a Clownfish, use the instanceof operator:





In real life you may not have much to do with all those frutti di mare, but imagine you have a graphics user interface class that contains a list of objects of - say - type MenuContainer (that is an interface), and in these List some JLabel and JButton objects and you want to get the tool tip text of all these. MenuContainers doesn't have such a method, but the individual classes (JLabel and JButton)have. Just loop through the list, check, if they are instanceof JLabel or JButton, then cast (perhaps use their common super class JComponent) and invoke getToolTipText() on these.

OK, this posting is a bit long. Never mind.


Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion