• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting  RSS feed

 
Vani Bhatia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam new to Java and Iam facing the following problem.


I have a superclass,SuperCl, with subclasses SubCl1,SubCl2 and SubCl3.

One of the methods in my project returns me an object of SuperCl.But this objcet is actually an instance of one of the subclasses ie., if I call the method getClass().getName() on the returned objcet it will give me the name of one of the subclasses.

Now my problem is the following. I have to cast the returned object to the correct subclass(which I will know only during run time).
I do not want to use instanceof.

I want to do something like the following:
(returnedobject.getClass()) abc = (returnedobject.getClass()) returnedobject;

I know this is not the correct way ..but is there any way of doing this ie., cast a superclass object to a subclass(which particular subclass will be known only at run time)

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
beginner java wrote:

Please check your private messages for an important administrative matter.
 
Manu Ullas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
beginner java wrote:
I have a superclass,SuperCl, with subclasses SubCl1,SubCl2 and SubCl3.

One of the methods in my project returns me an object of SuperCl.But this objcet is actually an instance of one of the subclasses ie., if I call the method getClass().getName() on the returned objcet it will give me the name of one of the subclasses.

Now my problem is the following. I have to cast the returned object to the correct subclass(which I will know only during run time).

I do not want to use instanceof.

I know this is not the correct way ..but is there any way of doing this ie., cast a superclass object to a subclass(which particular subclass will be known only at run time)



Since you can refer to instances of subclasses with a super-class type reference, you can include a function in the superclass that returns something that uniquely identifies the instance type. This could be a function returning a string. You could then compare this value against known values and then determine the type of function.

(I haven't been through the API documentation but I think this is what instanceof might actually do)

So, this is how it would go:






 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have to cast anything? Correct use of polymorphism might mean you can dispense with all the casts.
 
Vani Bhatia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. I will try to do without using 'casting'
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!