Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-20-2007, 11:46 AM
Member
 
Join Date: Dec 2007
Posts: 9
ajaygargnsit is on a distinguished road
'Casting' couch !!!!
Well, what I need to do this.

I have designed an interface; and two classes c1 and c2 that implement this interface. Now, I need to create an object of one of the classes (dynamically), but assign it as an interface instantiation. That is,

interface i
{
void f1();
void f2();
}

class c1 implements i
{
void f1(){}
void f2(){}
}

class c2 implements i
{
void f1(){}
void f2(){}
}


Now, I know the following will work ::

i i_obj = new c1();
i i_obj = new c2(); // Fine till here !!!!!!!!!!!!!!!!!!!


But suppose I now want to the same thing, but the 'class type' must be known at run time, through the 'Properties class' object, by making use of Class.forname() function.

NOW, how do i proceed ??

Looking forward to reply.
Ajay Garg
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-20-2007, 12:32 PM
Member
 
Join Date: Dec 2007
Posts: 9
ajaygargnsit is on a distinguished road
knock-knock !!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-20-2007, 02:29 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
What about checking it with instance of operator?

Code:
if (i_obj instanceof c1) ...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-03-2008, 01:18 AM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Assuming the name of the class that you want to create an instance of is in a properties object called props under the key "abc" and that the class named supports an interface called MyInterface. Code below will work as long as the class specified has an empty constructor.

String className = props.getProperty("abc");
Class c = Class.forName(className);
MyInterface myInstance = (MyInterface) c.newInstance();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-04-2008, 06:54 PM
Member
 
Join Date: Dec 2007
Posts: 9
ajaygargnsit is on a distinguished road
Vote of thanks
Thanks jelly; your solution is correct. Its working for me.

This thread may now be closed.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting an int value into a char kurtulas New To Java 2 02-16-2008 10:03 PM
Type Casting Help rhm54 New To Java 2 02-07-2008 02:06 PM
'Casting' couch !! ajaygargnsit New To Java 1 12-22-2007 03:05 PM
Casting leebee New To Java 5 08-10-2007 02:24 PM


All times are GMT +3. The time now is 11:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org