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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:31:47+00:00 2026-05-11T14:31:47+00:00

For example suppose I have a class Vehicle and I wish for a subclass

  • 0

For example suppose I have a class Vehicle and I wish for a subclass ConvertibleVehicle which has extra methods such as foldRoof(), turboMode(), foldFrontSeats() etc. I wish to instantiate as follows

Vehicle convertible = new ConvertibleVehicle() 

so I still have access to common method such as openDoor(), startEngine() etc. How do I designed such a solution?

To clarify my two initial solutions, neither of which I am happy with are:

  1. Have dummy methods foldRoof(), turboMode(), foldFrontSeats() which I override in ConvertibleVehicle only, leaving them to do nothing in other subclasses
  2. Have abstract methods foldRoof(), turboMode(), foldFrontSeats() and force each subclass to provide an implementation even if it will be blank in all instances other than ConvertibleVehicle

The above seem slightly convoluted since they both pollute the base class as I add an increasing number of subclasses each with their own unique functions

After reading some of the responses perhaps there is some type of fundamental flaw in my design. Suppose I have a class VehicleFleet which takes vehicles and instructs them to drive as follows:

public VehicleFleet(Vehicle[] myVehicles) {      for (int i=0; i < myVehicles.length; i++) {         myVehicles[i].drive();     } } 

Suppose this works for dozens of subclasses of Vehicle but for ConvertibleVehicle I also want to fold the roof before driving. To do so I subclass VehicleFleet as follows:

public ConvertibleVehicleFleet(Vehicle[] myVehicles) {      for (int i=0; i < myVehicles.length; i++) {         myVehicles[i].foldRoof();         myVehicles[i].drive();     } } 

This leaves me with a messy function foldRoof() stuck in the base class where it doesn’t really belong which is overridden only in the case of ConvertibleVehicle and does nothing in all the other cases. The solution works but seems very inelegant. Does this problem lend itself to a better architecture?

I’m using Java although I would hope that a general solution could be found that will work in any object oriented language and that I will not need to rely upon language specific quirks

  • 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. 2026-05-11T14:31:48+00:00Added an answer on May 11, 2026 at 2:31 pm

    I’ve done this in similar situations.

    Option A)

    If the specialized operations are part of the same sequence as a base operation ( e.g. ConvertibleVehicle needs to be foldRoof before it can drive ) then just put the specialized operation inside the base operation.

    class Vehicle {       public abstract void drive(); }  class ConvertibleVehicle {       public void drive() {           this.foldRoof();          .... // drive       }      private void foldRoof() {           ....      }  } 

    So the effect of driving a fleet will be some of them will fold their roof before being driven.

     for( Vehicle v : vehicleFleet ) {        v.drive();  } 

    The specialized method is not exposed in the object public interface but is called when needed.

    Option B)

    If the specialized operation are not part of the same sequence and must be called under certain ‘special’ circumstances then let a specialized version of a client call those specialized operations. Warning, this is not so pure nor low coupling but when both objects ( the client and the service ) are created by the same ‘condition’ or builder then most of the times is ok.

    class Vehicle {      public void drive() {          ....     } } class ConvertibleVehicle extends Vehicle {           // specialized version may override base operation or may not.         public void drive() {            ...           }           public void foldRoof() { // specialized operation               ...          }  } 

    Almost the same as the previous example, only in this case foldRoof is public also.

    The difference is that I need an specialized client:

    // Client ( base handler )  public class FleetHandler {       public void handle( Vehicle [] fleet ) {             for( Vehicle v : fleet ) {                  v.drive();             }      } }  // Specialized client ( sophisticate handler that is )    public class RoofAwareFleetHandler extends FleetHandler {        public void handle( Vehicle [] fleet ) {             for( Vehicle v : fleet ) {                // there are two options.               // either all vehicles are ConvertibleVehicles (risky) then               ((ConvertibleVehicles)v).foldRoof();               v.drive();                // Or.. only some of them are ( safer ) .               if( v instenceOf ConvertibleVehicle ) {                    ((ConvertibleVehicles)v).foldRoof();               }                v.drive();             }        }   } 

    That instaceof look kind of ugly there, but it may be inlined by modern vm.

    The point here is that only the specialized client knows and can invoke the specialized methods. That is, only RoofAwareFleetHandler can invoke foldRoof() on ** ConvertibleVehicle**

    The final code doesn’t change …

     public class Main {       public static void main( String [] args ) {           FleetHandler fleetHandler = .....          Vehicles [] fleet =  ....            fleetHandler.handle( fleet );       }  } 

    Of course, I always make sure the fleethandler and the array of Vehicles are compatible ( probably using abstrac factory or builder )

    I hope this helps.

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

Sidebar

Related Questions

For example, suppose I have a class: class Foo { public: std::string& Name() {
Say suppose I have the following Java code. public class Example { public static
Suppose I have a string 'nvarchar(50)', which is for example the T-SQL string segment
I have class Window. This class has method onClick which gets argument the id
Suppose I have an empty interface class IBaseInterface which is used only to label
Suppose I have a class such as Value defined below. template <typename T> class
For example suppose I have class Parent { def method() { var myvar =
For example (and this is very simplified), suppose I have a class for every
Suppose we have abstract class A (all examples in C#) public abstract class A
How to create jinternalframes who's action depends on each other? example suppose i have

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.