File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion

 
chintan ramavat
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi khushal,
could please use real words instead of "U" ? Thank you.

http://faq.javaranch.com/view?UseRealWords
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It won't compile.


It compiles properly.. U can try..
I have tried..
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was refering to calling the method on the superclass reference:

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic