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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:12:57+00:00 2026-05-24T12:12:57+00:00

I have 3 classes A, B, and C. The only thing they have in

  • 0

I have 3 classes A, B, and C. The only thing they have in common is a getName() function, nothing else.

I have an array of type Class which stores the above classes.

Class[] classes = {A.class,B.class,C.class};

I then have a List that contains objects of types A, B, and C

List<?> list = new ArrayList<?>();
list.add(new A());
list.add(new B());
list.add(new C());

I want to access the objects in the list through a loop. I also want to access methods of the objects A, B, and C. So, I try casting them using the classes array.

for(int i = 0; i < classes.length;i++)
{
    classes[i].cast(list.get(i)).getName(); //A,B,and C have method getName() in common
}

This gives me a compiler error: “The method getName() is not defined for type Object

Is there a way for the compiler to ignore this type of casting until runtime (I am sure it will work at runtime)? Or is there another solution that does what I expect(I am looking for a solution that does not involves changing the code of classes A, B, and C)

  • 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-24T12:12:57+00:00Added an answer on May 24, 2026 at 12:12 pm

    An answer is to use the instanceof operator. This seems reasonable if the classes in the List are known. Here is some code:

        public static void main(String[] args)
        {
            A aObject;
            B bObject;
            C cObject;
            List list = new ArrayList();
    
            list.add(new A());
            list.add(new B());
            list.add(new C());
            list.add(new String());
    
            for (Object current : list)
            {
                if (current instanceof A)
                {
                    aObject = (A)current;
                    System.out.println(aObject.getName());
                }
                else if (current instanceof B)
                {
                    bObject = (B)current;
                    System.out.println(bObject.getName());
                }
                else if (current instanceof C)
                {
                    cObject = (C)current;
                    System.out.println(cObject.getName());
                }
                else
                {
                    System.out.print("Unexpected class: ");
                    System.out.println(current.getClass());
                }
            }
        }
    

    Here is a (slightly) more verbose (then in other answers) example of doing this using Reflection. This seems reasonable if the classes in the List are not known or there are many of them. The commented out line below uses the Apache Commons Lang class MethodUtils.

        public static void main(String[] args)
        {
            List list = new ArrayList();
            Method getNameMethod;
    
            list.add(new A());
            list.add(new B());
            list.add(new C());
            list.add(new String());
    
            for (Object current : list)
            {
    //          getNameMethod = MethodUtils.getAccessibleMethod(current.getClass(), "getName", (Class[])null);
                try
                {
                    getNameMethod = current.getClass().getMethod("getName");
                }
                catch (SecurityException exception1)
                {
                    getNameMethod = null;
                }
                catch (NoSuchMethodException exception1)
                {
                    getNameMethod = null;
                }
    
                if (getNameMethod != null)
                {
                    try
                    {
                        System.out.println(getNameMethod.invoke(current, (Object[])null));
                    }
                    catch (IllegalArgumentException exception)
                    {
                        // TODO Implement this catch block.
                    }
                    catch (IllegalAccessException exception)
                    {
                        // TODO Implement this catch block.
                    }
                    catch (InvocationTargetException exception)
                    {
                        // TODO Implement this catch block.
                    }
                }
                else
                {
                    System.out.print("Unexpected class: ");
                    System.out.println(current.getClass());
                }
            }
        }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which have automatic properties only like public customerName {get; set;}. They
i have two data classes which hold only data members(no functions). One is CallTask
If I have classes of Type A and B: public class A { public
I have two very similar classes that do essentially the same thing. The only
In my program, I've got a bunch of classes that will only ever have
Many library classes in AS3 have read only properties. Is it possible to create
I have classes structured like this: Public MustInherit Class A ' several properties End
I often take the classes that linq2sql generates and create a simple data-only class
I have two classes, basically one holds Members and the other Sessions. They are
I have two classes - Task (which implements Comparable) and DeadlinedTask (where DeadlinedTask extends

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.