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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:05:35+00:00 2026-06-09T12:05:35+00:00

I’ve got a question regarding the best-practices of Java inheritance & composition: Say you’ve

  • 0

I’ve got a question regarding the best-practices of Java inheritance & composition:

Say you’ve got a Fruit which gets extended by Apple and Lemon.

Next to these you also got a Basket that gets extended by AppleBasket
and LemonBasket.

Fruits have a bidirectional one-to-many relationship with their
respective baskets, so Fruits have a basket, and baskets have a list
of fruits.

Now the thing I’d like to do is enforce the AppleBasket to only have Apples at compile time, and likewise for LemonBaskets. Similarly the Apple should only have an AppleBasket as property, and likewise for Lemons.

I’ve tried using generics, abstract methods and protected variables, yet I always find reasons not to go for a certain approach. It’s important we don’t break the SOLID principles, so for example the superclasses should contain the has-a and has-many properties to avoid code duplication and so on.

It’s key to get compiler errors when someone tries to put a Lemon inside an AppleBasket and vice versa.

Cheers,
Andreas

  • 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-09T12:05:37+00:00Added an answer on June 9, 2026 at 12:05 pm

    You might find some useful information about more complex typing by googling for covariance and contravariance.

    If you don’t mind using Scala, which compiles to bytecode you can use with Java programs, Scala’s gives you some nice syntax for doing this sort of thing (Java later):

    class Fruit
    class Basket[T <: Fruit ] {
        def addToBasket(fruit:T) =  {}
    }
    
    class Apple extends Fruit
    class Lemon extends Fruit
    
    class AppleBasket extends Basket[Apple]
    
    val applebasket = new AppleBasket
    
    applebasket.addToBasket(new Apple)
    
    applebasket.addToBasket(new Lemon)
    

    The last line of this code snippet gives the compile time error:

     $ scala Test.scala
    C:\cygwin\home\user\Test.scala:18: error: type mismatch;
     found   : this.Lemon
     required: this.Apple
    applebasket.addToBasket(new Lemon)
                            ^
    one error found
    

    I’ve been playing with Scala so much lately I’m having trouble thinking about this in Java, but in Java I’d probably do something more like this:

    class Fruit{}
    class Apple extends Fruit{}
    class Lemon extends Fruit{}
    class Basket<T extends Fruit>{
        public void addToBasket(T foo) {}
    }
    class Blah {
        void doIt() {
            Apple a = new Apple();
            Basket b = new Basket<Apple>();
            b.addToBasket(a);
            Lemon l = new Lemon();
            b.addToBasket(l);
        }
    }
    

    The b.addToBasket(l) line gives the following compile time type error:

    The method addToBasket(Apple) in the type Basket<Apple> is not applicable for the arguments (Lemon)
    

    Another option that gives the same compile error (copying from comments to make it clearer):

    //same definition of Fruit, Apple, and Basket, but with:
    class AppleBasket extends Basket<Apple> {}
    class Blah {
        void doIt() {
            Apple a = new Apple();
            Basket<Apple> b = new AppleBasket();
            b.addToBasket(a);
            Lemon l = new Lemon();
            b.addToBasket(l);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to

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.