Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Conversion

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi khushal,
could please use real words instead of "U" ? Thank you.

http://faq.javaranch.com/view?UseRealWords
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was refering to calling the method on the superclass reference:

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic