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

  • Home
  • SEARCH
  • 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 8986029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:27:16+00:00 2026-06-15T21:27:16+00:00

I have multiple types of object instances that inherit from a common interface. I

  • 0

I have multiple types of object instances that inherit from a common interface.
I would like to access the common methods from each objects by iterating through a list or arraylist or collections. how do I do that?

    {

    interface ICommon
    {
        string getName();
    }

    class Animal : ICommon
    {
        public string getName()
        {
            return myName;
        }
    }

    class Students : ICommon
    {
        public string getName()
        {
            return myName;
        }
    }

    class School : ICommon
    {
        public string getName()
        {
            return myName;
        }
    }


   }

When I add the animal, student, and School in an object[], and try to access
in a loop like

for (loop)
{
   object[n].getName // getName is not possible here. 
   //This is what I would like to have.
or 
   a = object[n];
   a.getName // this is also not working. 
}

is it possible to access the common method of different types in from a list or collections?

  • 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-06-15T21:27:17+00:00Added an answer on June 15, 2026 at 9:27 pm

    You need to either cast the object to ICommon

    var a = (ICommon)object[n];
    a.getName();
    

    Or perferably you should use an array of ICommon

    ICommon[] commonArray = new ICommon[5];
    ...
    commonArray[0] = new Animal();
    ...
    commonArray[0].getName();
    

    Or you might want to consider using a List<ICommon>

    List<ICommon> commonList = new List<ICommon>();
    ...
    commonList.Add(new Animal());
    ...
    commonList[0].getName();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object Customer that can have multiple customer types and each customer
I have an element that can have multiple types (not my design). The element
I have multiple function objects declared from the same function that deal with their
I have a superclass called City and a bunch of subclasses that inherit from
I have an application which supports multiple types and versions of some devices. It
I love NHibernate's ability to have one table to store multiple types based on
A single interface may also have multiple IPv6 addresses of any type(unicast, anycast, and
I have a class ( MyClass ) that inherits most of its functionality from
I have some code that I want to refactor. I have lots of methods
I have a template class that looks something like this: template<class T> class C

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.