Jump to content

Recommended Posts

Posted

Can any one give me a real life , meaningful example of object casting using java?

 

because I never find object casting useful during programming, although I know how to use them............

 

Albert

Posted

This is one situation where I had to use it :

 

I defined a generic "number" class and then inherted several types from that such as integer, complex, co-ordinate etc.

All the numbers were stored as lists (to allow 100 digit numbers and so), next the simple functions like addition and subtraction were written generically for the number class. When I neede to add say complex numbers, I'd first use the generic addition on real and then on imaginary part, in both cases object casting is required from complex to number.............

 

Another example is when you try to make a simulation.....the various objects in the simulation may often need tobe type casted into each other, or into a generic type.

Posted

pulkit, a class which inherits other classes means it has the functions and data of the being-inherited classes................... why just use the class that inherits to make one big object, instead of doing object casting............?

 

any way, maybe asking in this way will make me understand better, can any one give me a meaningful example given with java codes that shows object casting is highly useful, or needed?

 

thx in advance

 

Albert

Posted

I am sorry but I've not touched java for the last 3 months and its kinda difficult to fish thru the many files i've made earlier, i'll still try and fish out a more obvious example from somewhere (probably sans java code, i have to re-learn java....)

  • 3 weeks later...
Guest liqweed
Posted

Umm... I might be missing somehting here...

Casting is a very useful feature for object oriented programming languages as it makes generic uses possible. In Java, every object (type other than primitives) derives from (extends) java.lang.Object. This make the usage of generic collections (for example) very handy - you can add/remove any type to a generic collection and then get it back and use it as the original type (not just a java.lang.Object type). Basic example:

 

----------------

Person sampleDude = new Person("mike");

Person anotherDude = new Person("jim");

List people = new ArrayList();

people.add(sampleDude);

people.add(anotherDude);

....

....

 

Person tempDude;

Iterator i = people.iterator();

while (i.hasNext()) {

Person tempDude = (Person)i.next();

tempDude.doSomething();

}

----------------

Iterator.next() returns an java.lang.Object, which may then be casted to any other type. Notice the casting to a Person class, this is necessary in order to be able to use the doSomthing() method (which belongs to Person, not just any Object). Typically these kind of problems were solved back-in-the-days with C++ using templates. Java in my opinion is far more agile in this sense as generics are embodied in it's very syntax.

Posted

"Back-in-the-day" with C++, still my favorite language although I use Java at work on a daily basis (C++ wasn't made for the web), implementing a generic container was, in fact, done with templates. However, polymorphism worked essentially the same way it did in Java. The reason the STL in C++ uses templates for its containers is that it's much FASTER than doing a dynamic cast. Casting's only really good if you're doing it quick and dirty C-style but a true, object oriented, dynamic cast (as done in java or optionally done in C++ when you want flexibility over speed) is not a cheap process and accounts for some of Java's lag. That and object creation on the heap, of course.

 

Honestly, I only use casting in java when dealing with their Collections framework or when creating objects from .class files at runtime. And in terms of the Collections framework, it would be awful nice if they had an ArrayList that could be fed a Class object in its constructor so you wouldn't have to cast what it spits out every time. After all, I've very infrequently (if EVER) had to store different types of objects in a java container that weren't related by a class or interface that I wrote anyhow.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.