Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2005, 12:27 PM
Registered User
 
Join Date: Nov 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Casting question

I have a requirement to process a number of different instances of different classes in a similar fashion, like so:

doClass1()
{
..
  Class1 obj = (Class1)wigit.get();
  process(obj);
..
}

doClass2()
{
..
  Class2 obj = (Class2)wigit.get();
  process(obj);
..
}

Is there some way that I can abstract out the different classes to give me something like:

doClassX(something??)
{
..
  Something?? obj = (something??)wigit.get();
  process(obj);
..
}

so that I can call method doClassX() twice with an argument rather than having two almost identical methods.

Thanks in advance.
 
Old July 22nd, 2005, 11:06 AM
Authorized User
 
Join Date: Jul 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have been trying to do this for an age but can't figure it.

You can do:

Object obj = myVector.get(0);
process(obj);

And in process you would have:

Class theClass = obj.getClass();
if (theClass.getName().equals("java.lang.String"))
{
  String stringObject = (String)obj;
  System.out.println(stringObject.length());
}
else if (theClass.getName().equals("java.lang.Boolean"))
{
  Boolean boolObject = (Boolean)obj;
  System.out.println(boolObject.booleanValue());
}

But I have found no way of casting an unknown class so that you can call class-specific methods on it. I don't think it can be done.

Chris J
 
Old August 2nd, 2005, 02:19 AM
Authorized User
 
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default


Hi....

Yes you cam much simplier....using polymorphism

Something must be the superclass of class1 and class2....
(or class1 and class2 implement Something interface)
Due the "late binding" process(something) the appropiate process method is executed.

Hope this helps,

LOOP

 
Old August 2nd, 2005, 02:29 AM
Registered User
 
Join Date: Nov 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the direction. I think my snippet was not clear enough, as the process(), doClass1(), doClass2() are methods of a handler class.

I simply replaced the casting of Class1 & Class2 to Object and everything worked fine, my method doClassX() now looks like this.

doClassX()
{
..
Object obj = wigit.get();
process(obj);
..
}


Regards,

Bluespud.
 
Old August 2nd, 2005, 02:40 AM
Authorized User
 
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default


Yes..a polymophic call indeed :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting pfrigo BOOK: Professional .NET 2.0 Generics 3 November 19th, 2007 04:49 PM
Casting - Can you help koco C# 1 September 24th, 2007 08:41 AM
Casting Problem liorlankri ASP.NET 1.x and 2.0 Application Design 2 January 2nd, 2007 03:31 AM
casting ravibodani Access 1 April 4th, 2006 01:19 PM
Casting issue skin C# 2 March 19th, 2004 04:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.