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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:54:10+00:00 2026-05-30T06:54:10+00:00

I need to make a collection of several different classes. In my mind, the

  • 0

I need to make a collection of several different classes. In my mind, the sub classes inherit from a more general super class. So I have the class SuperClass, which currently has a member variable o of Object type, which will take a single instance of one of the subclasses class SubClassA and class SubClassB. SubClassA and SubClassB have some things in common, such as the need to create database tables to hold descriptions of themselves. Therefore, each subclass needs to have a method CreateDatabaseTables(). Currently I have a collection of SuperClass instances, each of them containing an Object o which can be one of several classes that I will not know at compile time. Maybe this isn’t the best way to do this…

My question is, when I iterate over the collection, will I be able to call something like

for (int i = 0; i < superClassArray.size; i++)
{
  superClassInstance[i].o.CreateDatabaseTables();
}

without having to cast Object o into one of the SubClasses?

Or have I gooned up the whole thing and I am just plain doing it wrong? Is this concept called inheritance, or is it something else? I don’t think it is inheritance because I do not intend to write a CreateDatabaseTables() method in the SuperClass. Each class will have to have its own method.

  • 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-30T06:54:11+00:00Added an answer on May 30, 2026 at 6:54 am

    You’re not using inheritance, but composition.

    You don’t need members in your base class:

    abstract class SuperClass
    {
        public abstract void CreateDatabaseTables();
    }
    
    class SubClassA extends SuperClass
    {
        public void CreateDatabaseTables() { /*do whatever*/ }
    }
    class SubClassB extends SuperClass
    {
        public void CreateDatabaseTables() { /*do whatever else*/ }
    }
    

    And simply call the overriden method on the base class:

    // Collection<SuperClass> superClassInstance;
    // you can populate superClassInstance with objects of type
    // SubClassA or SubClassB
    for (int i = 0; i < superClassInstance.length; i++)
    {
        superClassInstance[i].CreateDatabaseTables();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a collection with a lot of movies, but I was
I'd like to: Make commonly required services visible to all classes that need them,
Lets say I inherit a class, that has several public properties and/or methods, however
i have 500 excel sheets which are used for data collection from various organisations
I have parametrized junit test that reads from several XML input files. In the
My app makes several calls to getFromLocationName from the android.location.Geocoder; class, this is currently
I have the need to access a sybase database (12.5) from oversea. The high
Well, I have developed a java application using several objects relationships that make the
I need to make a connection to a Sybase (SQL Anywhere) database in a
I need to make a connection to an Access Database. In order to do

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.