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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:26:36+00:00 2026-05-26T11:26:36+00:00

I have two abstract classes, lets call them Car ‘s and Wheel ‘s. I

  • 0

I have two abstract classes, lets call them Car‘s and Wheel‘s. I am handling different types of cars using some inheritance. So lets say there are two derivations MonsterTruckCar and ToyCar. Additionally there are different types of wheels that correspond to the Cars, say TreadedWheel‘s and PlasticWheel‘s (the mapping does not necessarily need to be one-to-one from types of cars to types of wheels). Further Car‘s are composed of Wheel‘s.

The way I thought I would be able to do this in Java is by using an attribute of Car‘s to be type ArrayList<Wheel>. The issue I am having is that now when I want to use polymorphism on the Wheel‘s I cannot because whenever I deal with a Wheel it is through an ArrayList which is not a derived class of Wheel‘s.

I thought I might be able to use bounded wildcards, or just a lot of switch statements to handle different combinations but I dont think either of those would be the greatest solutions. How can I handle such a structure?

Further, how do you add Wheel‘s to the composition/collection in Car‘s. That is, I need to be able to add a variable amount of concrete Wheel‘s to a Car, further this is going to be based off of some user input. so I would like to have a while loop in the default constructor that prompt the user if he/she wants to add another wheel and then if he/she does I add another Wheel to whatever is aggregating/collecting them in Car‘s.

Edit: Edited the names of the classes themselves from plural (Wheels) to singular with a ‘s (Wheel‘s) to clarify the relationship. Added the last paragraph which explains further the behavior I am looking for.

  • 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-26T11:26:37+00:00Added an answer on May 26, 2026 at 11:26 am

    Add a generic parameter to your Car class that specified the wheel type. Here’s a very basic implementation you can build on:

    Edited:

    Includes updated requirements, which is tricky, but doable…

    public interface Wheel {
        void setSize(int diameter);
    }
    
    public static abstract class Car<T extends Wheel> {
    
        private List<T> wheels = new ArrayList<T>();
    
        protected Car(int numberOfWheels, Class<T> wheelClass) {
            while (wheels.size() < numberOfWheels) {
                T wheel;
                try {
                    wheel = wheelClass.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e); // simplified exception handling
                }
                wheel.setSize(5); // Ask user for wheel details etc
                wheels.add(wheel);
            }
        }
    
        public List<T> getWheels() {
            return wheels;
        }
    }
    
    public static class PlasticWheel implements Wheel { // or extends Wheel
        @Override
        public void setSize(int diameter) {
        }
    }
    
    public static class ToyCar extends Car<PlasticWheel> {
        public ToyCar() {
            super(6, PlasticWheel.class); // or you could pass number of wheels in to this constructor
        }
    }
    
    public static class TreadedWheel implements Wheel { // or extends Wheel
    
        @Override
        public void setSize(int diameter) {
        }
    }
    
    public static class MonsterTruckCar extends Car<TreadedWheel> {
        public MonsterTruckCar() {
            super(4, TreadedWheel.class); // or you could pass number of wheels in to this constructor
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have three django model classes - lets call them A, B
Sometimes I wonder that we do have interfaces and abstract classes for two different
I have two abstract classes called Robot and GeometricElement . Each one of them
Consider the following example. I have an interface MyInterface, and then two abstract classes
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have a two generic abstract types: Entity and Association . Let's say Entity
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have two classes which represent a special kind of numeric value. Let's call
I have two classes: abstract Entity , abstract ClassA extends Entity , ClassB extends
Possible Duplicate: Pure virtual destructor in C++ I have two classes: the abstract Game

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.