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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:45:53+00:00 2026-05-24T10:45:53+00:00

I’m new to using generics (very late to the party, I know), and aren’t

  • 0

I’m new to using generics (very late to the party, I know), and aren’t sure if what I am trying to achieve is possible, and if so, how.

OK, this is a contrived example of what I am try to achieve, that hopefully shows the pattern without any clutter:

// Pet and Cats
interface Pet { }
interface Cat extends Pet { }
class TabbyCat implements Cat { }

// Pet food and cat food
interface PetFood<T extends Pet> { }
interface CatFood extends PetFood<Cat> {}
class DryCatFood implements CatFood {}
class WetCatFood implements CatFood {}

// PetBowl
interface PetBowl<T extends Pet> {
    public void addFood(PetFood<T> food);
}

// CatBowl
class CatBowl implements PetBowl<Cat> {

    // this doesn't override method, why not?
    public void addFood(CatFood food) { }

    // this does
    public void addFood(PetFood<Cat> food) { }

}

So, basically what I am trying to do is make CatBowl implement PetBowl, but so that the addFood() method only allows CatFood to be added.

The only solution I have been able to come up with is:

...

// PetBowl
interface PetBowl<T extends Pet, F extends PetFood<T>> {
    public void addFood(F food);
}

// CatBowl
class CatBowl implements PetBowl<Cat, CatFood> {

    public void addFood(CatFood food) { }

}

However, in my non-contrived case there are more parameters, and this ends up making for a lot of boilerplate all over the place, so I’m hoping for a “neater” solution.

  • 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-24T10:45:57+00:00Added an answer on May 24, 2026 at 10:45 am

    This is not really linked to generics. The interface PetBowl<Cat> has a method addFood(PetFood<Cat> food). So, any class implementing this interface should have such a method. addFood(CatFood food) is not acceptable because it doesn’t allow adding any kind of PetFoo<Cat>, but only CatFood.

    It’s the same as if you had

    interface A {
        void add(Object o);
    }
    
    class AImpl implements A {
        public void add(String s);
    }
    

    Obviously, AImpl.add doesn’t override A.add, because it only accepts Strings, and not Objects.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.