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 590049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:29:17+00:00 2026-05-13T15:29:17+00:00

All the examples I look at for reflection show creating a new instance of

  • 0

All the examples I look at for reflection show creating a new instance of an unknown implementation, and casting that implementation to it’s interface. The issue with this is that now you can’t call any new methods (only overrides) on the implementing class, as your object reference variable has the interface type. Here is what I have:

Class c = null;
try {
    c = Class.forName("com.path.to.ImplementationType");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
InterfaceType interfaceType = null;
try {
    interfaceType = (InterfaceType)c.newInstance();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

If I only have a reference to “com.path.to.ImplementationType”, and I don’t know what that type might be (it is coming from a config file), then how can I use the class name to cast it to ImplementationType? Is this even possible?

  • 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-13T15:29:17+00:00Added an answer on May 13, 2026 at 3:29 pm

    This line seems to sum up the crux of your problem:

    The issue with this is that now you can’t call any new methods (only overrides) on the implementing class, as your object reference variable has the interface type.

    You are pretty stuck in your current implementation, as not only do you have to attempt a cast, you also need the definition of the method(s) that you want to call on this subclass. I see two options:

    1. As stated elsewhere, you cannot use the String representation of the Class name to cast your reflected instance to a known type. You can, however, use a String equals() test to determine whether your class is of the type that you want, and then perform a hard-coded cast:

    try {
       String className = "com.path.to.ImplementationType";// really passed in from config
       Class c = Class.forName(className);
       InterfaceType interfaceType = (InterfaceType)c.newInstance();
       if (className.equals("com.path.to.ImplementationType") {
          ((ImplementationType)interfaceType).doSomethingOnlyICanDo();
       } 
    } catch (Exception e) {
       e.printStackTrace();
    }
    

    This looks pretty ugly, and it ruins the nice config-driven process that you have. I dont suggest you do this, it is just an example.

    2. Another option you have is to extend your reflection from just Class/Object creation to include Method reflection. If you can create the Class from a String passed in from a config file, you can also pass in a method name from that config file and, via reflection, get an instance of the Method itself from your Class object. You can then call invoke(http://java.sun.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object, java.lang.Object…)) on the Method, passing in the instance of your class that you created. I think this will help you get what you are after.

    Here is some code to serve as an example. Note that I have taken the liberty of hard coding the params for the methods. You could specify them in a config as well, and would need to reflect on their class names to define their Class obejcts and instances.

    public class Foo {
    
        public void printAMessage() {
        System.out.println(toString()+":a message");
        }
        public void printAnotherMessage(String theString) {
            System.out.println(toString()+":another message:" + theString);
        }
    
        public static void main(String[] args) {
            Class c = null;
            try {
                c = Class.forName("Foo");
                Method method1 = c.getDeclaredMethod("printAMessage", new Class[]{});
                Method method2 = c.getDeclaredMethod("printAnotherMessage", new Class[]{String.class});
                Object o = c.newInstance();
                System.out.println("this is my instance:" + o.toString());
                method1.invoke(o);
                method2.invoke(o, "this is my message, from a config file, of course");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException nsme){
                nsme.printStackTrace();
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
            } catch (InstantiationException ie) {
                ie.printStackTrace();
            } catch (InvocationTargetException ite) {
                ite.printStackTrace();
            }
        }
    }
    

    and my output:

    this is my instance:Foo@e0cf70
    Foo@e0cf70:a message
    Foo@e0cf70:another message:this is my message, from a config file, of course
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, Booger's solution would be to switch to sprintf(). It's… May 13, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer You're getting NameError Because there's no such name core defined… May 13, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer Whatever you do you don't want to edit the actual… May 13, 2026 at 8:49 pm

Related Questions

I'm just getting started with Hibernate, and all the examples I'm seeing so far
I'm a .NET web developer, who's bought a Mac, and is interested in doing
I have a component derived from ErrorProvider . I have a BaseForm and BaseUserControl
Can you create a delegate of an instance method without specifying the instance at
I have a program i frequently use that is made with .NET. This program

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.