Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:07:33+00:00 2026-05-24T03:07:33+00:00

I have an interface and there are couple of implementations for that Interface. Now

  • 0

I have an interface and there are couple of implementations for that Interface. Now I need to invoke the correct Implemented method dynamically.

I get the Implementation Class name from a property file. Now I have to invoke the method using reflection.

Could you please suggest the best approach to do it?

//This is my Interface.

public interface ITestInterface{
    public CustomVO customMethod(CustomObj1 obj1,CustomObjec2 obj2);
}

//This class implements the above interface

public class TestInterface implements ITestInterface{
   public CustomVO customMethod(CustomObj1 obj1,CustomObjec2 obj2){

   //some logic
  }
}

Now I need to invoke customMethod(obj1,obj2) using Reflection. I’ve the class name of TestInterface.

This is what I did.
I created an instance of TestInterface using Class.forName(className).newInstance();

Class[] paramTypes = new Class[ 2 ];
paramTypes [ 0 ] = CustomObj1.class;
paramTypes [ 1 ] = CustomObj2.class;
Object obj=Class.forName(className).newInstance();

Class.forName(className).getMethod( "customMethod", paramTypes ).invoke( obj, obj1,obj2);

I do not know if this is the correct way to do this? Could you please guide me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:07:34+00:00Added an answer on May 24, 2026 at 3:07 am

    Creating the object via reflection is fine as you did it (barring error handling, which I assume you omitted here for brevity).

    But once you have the object created, why not simply downcast it to ITestInterface and call its method(s) directly?

    ITestInterface obj = (ITestInterface) Class.forName(className).newInstance();
    obj.customMethod(param1, param2);
    

    (again, handling ClassCastException is omitted here, but it should be dealt with in production code.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that defines a method for returning an IList<PropertyInfo> : public
Let's say we have interface window_creator that responsible for creation of windows. For simplicity
I have an interface that has large numbers of controls, see image below. Interface
I have a ViewController that has a couple of IBOutlet properties defined for UITextView.
I have an interface called IAddress, and a class called Address that handles street,
I have a couple of scenarios that cover this, but I'll write specifically about
I have a class that needs to be sortable in a couple of different
I have a couple of Android and iOS native mobile application that I wrote
Please consider the following three .NET types: I have an interface, an abstract class,
I have a class A, and B like these: @interface A : UIViewController <UITableViewDelegate,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.