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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:25:28+00:00 2026-05-29T04:25:28+00:00

Say I have a class pet and it is composed of two variables: Class

  • 0

Say I have a class pet and it is composed of two variables:

Class Pet {
Category category; // be amphibian, fish, mammal, dog
Diet diet; //meat, vegetarian, omnivore
...

If category = dog I want to restrict diet to omnivore only (and vice versa). My actual problem is somewhat more complex, whats the best way to code this ? I started with nested enums and then inner builder class …

Is their a common pattern I can employ ?

  • 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-29T04:25:29+00:00Added an answer on May 29, 2026 at 4:25 am

    You have this trouble because you are not taking advantage of inheritance here. Dog IS A Pet. So let Dog extend Pet. Pet will have abstract setDiet() and Dog will implement this with applying the diet rules applicable for Dog.

    class Pet
    {
      Diet diet;
    
      abstract boolean satisfiesDietRules();
    
      public Pet()
      {
        if(!satisfiesDietRules())
        {
          throw CannotCreatePetException();
        }
      }
    }
    
    class Dog extends Pet
    {
      boolean satisfiesDietRules()
      {
        //rules
      }
    }
    

    Now, you can encapsulate the diet rules in their respective enum (your current approach) or leverage inheritance which is natural. If you go composition approach, then you can’t leverage polymorphism. So it depends upon your business requirements to understand which one to use. If you make sure that you doesn’t violate LSV then inheritance seems to be a natural solution for this.

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

Sidebar

Related Questions

Let's say I have: class Animal { } class Dog : Animal { }
Say I have: class Vector3 { float x, y, z; ... bunch of cuntions
say I have: class Test { public static int Hello = 5; } This
Lets say I have: class X { function a() { echo Hello, ; }
let's say I have class Dummy { String a, b; public Dummy(String a, String
Okay so let's say I have class A... CLASS A has a method that
Say I have classes class A{ //code for class A } class B{ //code
Say you have a class with an event property. If you instantiate this class
Say I have a class with three constructors, like the one below: public class
Say I have a class that looks like this (get/set omited): class InfoClass{ String

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.