Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing methods which returns different Objects

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I had written one function which Returns Object and takes parameter as Object also


Here the function is working fine and while calling the function i had to type cast the Value which the function returns Object
i want to alter my code such that i need not to type cast in the calling function is this possible and i had also gone through wild cars also there it suits for objects which extends Number but here im using Vectors and String also so please tell me the way to com out of this
 
Campbell Ritchie
Sheriff
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will that actually compile? It doesn't look at all good to me. You might do well to find the Class.forName("foo.bar.Class").newInstance() idiom. But that (I think) requires a no-arguments constructor.
 
Campbell Ritchie
Sheriff
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe a Map<String Class><?>> which returns the Class object for a particular name?
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya its compiled and running and currently i'm type casting the in the calling function but i want to make not to type cast for all the four objects
 
Aneesh Vijendran
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to do this?



also does the same thing.

Where ever you call any of these methods, you need to do a typecast as the signature is still Object.


Moreover:


wont compile for sure, rather it should be


 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i had used


also does the same thing.

Where ever you call any of these methods, you need to do a typecast as the signature is still Object.


I'm using different logic based upon the Objects(Integer,Boolean,Vector ,String)
 
Campbell Ritchie
Sheriff
Posts: 48646
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhosh.R gowda wrote:Sorry i had used . . .
I suspected that, but we can't be sure. Please read this FAQ.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here i'm returning the Object as a return value and in my calling method i want to type cast as per my requirement
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic