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

There are numerous times you have an interface that accepts similar type arguments that
I have a interface that defines some methods with attributes. These attributes need to
I have an interface that defines a method for returning an IList<PropertyInfo> : public
I have what I imagine to be a pretty standard web-interface. There are 4
If I have interface IFoo, and have several classes that implement it, what is
Lets say you have interface definition. That interface can be Operation . Then you
I have a class that needs to be sortable in a couple of different
I have an interface called ICommand from which various classes inherit. These classes all
I have a class that implements multiple interfaces. I would like to register these
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {

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.