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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:28:08+00:00 2026-06-09T09:28:08+00:00

Is it possible to store different objects that have been extended from the same

  • 0

Is it possible to store different objects that have been extended from the same abstract object in a single container and be able to access all of the custom fields of those objects. Let me present an example:
Suppose I have two cars: ford and honda. Since all cars have weight and color, I could make the following abstract class:

public abstract class Car{

  private double weight; 
  private String color;

  public Car(){
    this.weight=0;
    this.color="";
  }
  ...getters and 
  ...setters
}

Now suppose my Ford class has an additional field ‘radio‘ and looks like this:

public Ford extends Car(){
private String radio;

public Ford(){
super();
this.radio = "generic";
}
}

Similarly, the Honda class has a custom field ‘camera‘

public Honda extends Car(){
  private String camera;

  public Honda(){
    super();
    this.camera= "analog";
  }
}

Is it possible to keep the instances of these classes in a single container, something like the following below. Please note the code below works but this is not quite what I want. see comments below:

Ford ford = new Ford();
Honda honda = new Honda();
ArrayList<Car> myCars = new ArrayList<Car>();
myCars.add(ford);
myCars.add(honda);

HERE IS MY PROBLEM:
When I do myCars.get(0) I get an object of class Car (obviously) and am never able to get the custom field radio. Of course I could do something like ((Ford) myCars.get(0)) and will be able to see the radio field, but is it possible to create a better container what would reveal all of the fields without downcasting etc, something like:
myContainer.get(0) – would return the Ford class and I would have an access to weight, color and radio
myContainer.get(1) – would return the Honda class and I would have an access to weight, color and camera.

My hunch tells me that the solution is near and it is possible, but my expertise in Java is not up there yet. Maybe I have do to some sort of generics or hashmaps??

Thank you guys!!

  • 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-09T09:28:09+00:00Added an answer on June 9, 2026 at 9:28 am

    There are several alternatives, none of which perfectly meets your ideal.

    • You could add all the getters for specific properties in your Car class and either return Null or throw an exception where they are not applicable.
    • You could apply the Visitor pattern.
    • You could create a CarContainer class that wraps your list and has methods that return all the elements of a specific type, e.g. List<Ford> getAllTheFords(). In this way you can hide the ugly casts in your container class.

    I listed alternatives according to my personal preference 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following problem: I need to store generic objects with different type
Is it possible to store a Class that contains a List inside an array?
Is it possible to store the credentials that I use to connect to my
We have some search functionality that can return tens of thousands of results from
I have 2 different object types stored in RavenDb, which are a parent/child type
The whole story; I have some KeyValuePairs that I need to store in a
I am building a cache that has to store as much data as possible.
I am using Core Data to store data from the server that is sent
I have a Singleton class handling a kind of cache with different objects in
I have a website that needs to store two sets of user data into

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.