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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:20:42+00:00 2026-05-31T18:20:42+00:00

classes: public abstract class BaseHolidayPackageVariant { private Integer variantId; private HolidayPackage holidayPackage; private String

  • 0

classes:

public abstract class BaseHolidayPackageVariant {
    private Integer variantId;
    private HolidayPackage holidayPackage;
    private String holidayPackageType;
}

public class LandHolidayPackageVariant extends BaseHolidayPackageVariant{

}

public class FlightHolidayPackageVariant extends BaseHolidayPackageVariant{
    private Destination originCity;
}

public class HolidayPackage{
    ArrayList<BaseHolidayPackageVariant> variants;

    BaseHolidayPackageVariant defaultVariant;
}

At runtime, how can I know if a given Object in variants[] is of Type LandPackageVariant or FlightPackageVariant without doing something of the sorts of:

if(holidayPackageType.equals("LandHolidayPackageVariant")
 obj = (LandHolidayPackageVariant)variant[i];
else if(holidayPackageType.equals("FlightHolidayPackageVariant")
 obj = (FlightHolidayPackageVariant)variant[i];

This question stems from a design question I asked here

  • 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-31T18:20:43+00:00Added an answer on May 31, 2026 at 6:20 pm

    In a good object-oriented design, you shouldn’t ever need to know if the object is of a particular type. You just call methods on it, and the object does the right thing.

    For example, FlightHolidayPackageVariant has a field originCity that isn’t in the other HolidayPackageVariant classes, and you want to render that in the UI. The object-oriented way to solve this is to make the HolidayPackageVariant responsible, in some way, for controlling its own rendering. Let’s say your UI is going to show a list of properties for each variant. You can let the variants supply those lists:

    public abstract class BaseHolidayPackageVariant {
        private int cost;
    
        public Map<String, String> getDetails() {
            HashMap<String, String> details = new HashMap<String, String>();
            details.put("cost", String.format("%.2f", cost / 100.0));
            return details;
        }
    }
    
    public class FlightHolidayPackageVariant extends BaseHolidayPackageVariant {
        private Destination originCity;
    
        @Override
        public Map<String, String> getDetails() {
            Map<String, String> details = super.getDetails();
            details.put("origin city", originCity.getName());
            return details;
        }
    }
    

    Now, your UI code can simply ask each variant object for its details, without having to know what kind of variant it is.

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

Sidebar

Related Questions

I have these classes: public abstract class CustomField { public String Id { get;
I have just implemented some classes: public abstract class Job<T extends ViewerUnion>{ [...] }
I have the following classes: public class CollectionCustomClass extends ArrayList<CustomClass> public abstract class CustomClass
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have a problem with these two classes: Creature: public abstract class Creature extends
I have the following classes: public abstract class BaseClass { private readonly double cachedValue;
Here are the domain model classes: public abstract class BaseClass { ... } public
These are my domain and view classes: public abstract class Entity : IEntity {
I have the following classes in my Model: public abstract class Entity : IEntity
I'm looking at some Java classes that have the following form: public abstract class

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.