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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:29:25+00:00 2026-06-07T14:29:25+00:00

I was wondering if I can model the relationship between the two class hierarchies

  • 0

I was wondering if I can model the relationship between the two class hierarchies below, in the way that I did:

car-engine composition relationship

The code that would represent the meaning of this would be something like:

public abstract class Car
{
    protected Engine engine;  
}
public class StreetCar extends Car
{
     public StreetCar()
     {
          engine = new StreetEngine();
     }
}

…and similarly for the class OffroadCar, it would do something like engine = new OffroadEngine(). I have not included anything about the accelerate() method, it’s not relevant.

I just want to know if the composition is modeled correctly or if it’s redundant or even wrong to add that many composition arrows.

  • 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-07T14:29:27+00:00Added an answer on June 7, 2026 at 2:29 pm

    Before answer question, this is one of those cases, where is a good idea to split hierarchy relationships from composition relationships, as if they where different kind of diagrams.

    Note: I added Engine to the diagram, as “protected”, and “Class” prefix, to avoid confusing.

    (1) Composition Diagram

    An object of “CarClass” its composed by 1 single object of “EngineClass”.

    ..............................................................................
    ..+-------------------------------------+........+-------------------------+..
    ..|           <<abstract>>              |........|      <<abstract>>       |..
    ..|             CarClass                |........|       EngineClass       |..
    ..+-------------------------------------+........+-------------------------+..
    ..| [#] Engine: EngineClass             |<*>-----+ [+] startEngine(): void |..
    ..+-------------------------------------+........+-------------------------+..
    ..| [+] acelerate(): void <<virtual>>   |.....................................
    ..+-------------------------------------+.....................................
    ..............................................................................
    

    (2.1) Inheritance Diagram

    An object of “CarClass” may have child classes, upon certain scenario.

    ...........................................
    ..+-------------------------------------+..
    ..|           <<abstract>>              |..
    ..|             CarClass                |..
    ..+-------------------------------------+..
    ..| [#] Engine: EngineClass             |..
    ..+-------------------------------------+..
    ..| [+] acelerate(): void <<override>>  |..
    ..+-----------------+-------------------+..
    .................../.\.....................
    ................../...\....................
    .................+--+--+..............................................................
    ....................|.................................................................
    ....................+------------------------------------------+......................
    ....................|..........................................|......................
    ..+-----------------+-------------------+....+-----------------+-------------------+..
    ..|           <<concrete>>              |....|           <<concrete>>              |..
    ..|            StreetCarClass           |....|           OffRoadCarClass           |..
    ..+-------------------------------------+....+-------------------------------------+..
    ..| [+] acelerate(): void <<override>>  |....| [+] acelerate(): void <<override>>  |..
    ..+-------------------------------------+....+-------------------------------------+..
    ......................................................................................
    

    (2.2) Inheritance Diagram

    An object of “EngineClass” may have child classes, upon certain scenario.

    ...........................................
    ..+-------------------------------------+..
    ..|           <<abstract>>              |..
    ..|             EngineClass             |..
    ..+-------------------------------------+..
    ..| [+] acelerate(): void <<override>>  |..
    ..+-----------------+-------------------+..
    .................../.\.....................
    ................../...\....................
    .................+--+--+..............................................................
    ....................+------------------------------------------+......................
    ....................|..........................................|......................
    ....................|..........................................|......................
    ..+-----------------+-------------------+....+-----------------+-------------------+..
    ..|           <<concrete>>              |....|           <<concrete>>              |..
    ..|          StreetEngineClass          |....|          OffRoadEngineClass         |..
    ..+-------------------------------------+....+-------------------------------------+..
    ..| [+] startEngine(): void <<override>>|....| [+] startEngine(): void<<override>> |..
    ..+-------------------------------------+....+-------------------------------------+..
    ......................................................................................
    

    3 Answer

    Now, this is a case, where a class has an unleast one composing member, and its type may be overriden, when the main class its overriden. This is sometimes called the “Paralell Hierarchy Software Pattern” or “Dual Hierarchy Software Pattern”.

    You have only mention 2 subclasses of each main class, but, in reality, there could be more.

    Usually I make charts like this 2 ways. One, I make the first diagram, adding a comment, that indicates that its this case.

    3.1 Main Paralell Hierarchy Diagram

    ..............................................................................
    ..+-------------------------------------+........+-------------------------+..
    ..|           <<abstract>>              |........|       <<abstract>>      |..
    ..|             CarClass                |........|        EngineClass      |..
    ..+-------------------------------------+........+-------------------------+..
    ..| [#] Engine: EngineClass             |<*>-----+ [+] startEngine(): void |..
    ..+-------------------------------------+........+------------+------------+..
    ..| [+] acelerate(): void <<virtual>>   |.....................|...............
    ..+--------------+----------------------+.....................|...............
    .................|............................................|...............
    .................+--------------------------------------------+...............
    .................|............................................................
    ........+--------+-------+....................................................
    ........|   Note:        |....................................................
    ........|   Paralell     /....................................................
    ........|   Hierarchy   /| ...................................................
    ........|              / |....................................................
    ........+-------------/--+....................................................
    ..............................................................................
    

    The second case, its when both classes has child classes that both add members.

    3.2 Additional Paralell Hierarchy Diagram

    ..............................................................................
    ..+---------------------------------------+........+-------------------------+..
    ..|             <<concrete>>              |........|       <<concrete>>      |..
    ..|            OffRoadCarClass            |........|    OffRoadEngineClass   |..
    ..+---------------------------------------+........+-------------------------+..
    ..| [+] createEngine(): void <<override>> |<*>-----+ [+] startEngine(): void |..
    ..+---------------------------------------+........| [+] nitroLevel(): void  |..
    ..| [+] useNitro(): void                  |........+------------+------------+..
    ..| [+] acelerate(): void <<override>>    |.....................|...............
    ..+--------------+------------------------+.....................|...............
    .................|..............................................|...............
    .................+----------------------------------------------+...............
    .................|............................................................
    ........+--------+-------+....................................................
    ........|   Note:        |....................................................
    ........|   Paralell     /....................................................
    ........|   Hierarchy   /| ...................................................
    ........|              / |....................................................
    ........+-------------/--+....................................................
    ..............................................................................
    

    I may add additional diagrams, if necesarilly.

    4 Show me the code

    In order, to manage this “parallel” hierarchies, usually, the creation of the composite members its managed by a overriden method.

    public abstract class EngineClass
    {
        public void startEngine() { ... }  
    } // EngineClass
    
    public abstract class CarClass
    {
        protected EngineClass engine;  
    
        public CarClass()
        {
          // ...
        }
    
        public EngineClass createEngine()
        {
          EngineClass Result = new EngineClass();
          return Result;
        }
    
        public void start()
        {
          this.Engine = createEngine();
        }
    } // CarClass
    
    public class StreetCarClass extends CarClass
    {
         public StreetCarClass()
         {
           // ...
         }
    
        @override
        public EngineClass createEngine()
        {
          EngineClass Result = new StreetCarEngineClass();
          return Result;
        }
    } // StreetCarClass 
    
    public class OffRoadCarClass extends CarClass
    {
         public OffRoadCarClass()
         {
           // ...
         }
    
        @override
        public EngineClass createEngine()
        {
          EngineClass Result = new OffRoadCarEngineClass();
          return Result;
        }
    } // OffRoadCarClass 
    
    public class ExampleClass
    {
        public static main()
        {
          EngineClass OffRoadCar = new OffRoadCarClass();
          OffRoadCar.start();
        }
    } // OffRoadCarClass
    

    Cheers.

    P.S. Can I, ur, haz a Fishburguer ?

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

Sidebar

Related Questions

I was wondering how I can get the graphics card model/brand from code particularly
I'm wondering how can I add attributes, that are not declared in a class,
I have a many-to-many relationship between User and Task model. A task can have
Im wondering if anyone can explain the following disparity in functionality between two ASP.Net
Wondering if any of you can help me: I've made a signup modal that
I'm having trouble deciding on a way to model this type of relationship... All
I'm wondering how I can find the number of entities in my model. Any
I'm wondering if this would be the best way to build a table for
I am wondering how I can make this work. Assuming I have a model
was wondering how can I create a sub drop down box upon selecting a

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.