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

  • Home
  • SEARCH
  • 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 8237019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:19:53+00:00 2026-06-07T19:19:53+00:00

I have a newbie question. interface Animal { void partner(Animal other); } class Lion

  • 0

I have a newbie question.

interface Animal {
    void partner(Animal other);
}

class Lion implements Animal {
    int areaUnderControl;
    @Override
    public void partner(Animal other) {
        Lion lion = (Lion) other;
        this.areaUnderControl += lion.areaUnderControl; 
    }
}

class Human implements Animal {
    int money;
    @Override
    public void partner(Animal other) {
        Human human = (Human) other;
        this.money += human.money;
    }
}

I want that the partner method in the concrete implementations only receive parameters of its type or its subtype. One solution is to use generics to define Animal as:

interface Animal<T extends Animal<T>> {
    void partner(T other);
}

class Lion implements Animal<Lion> {
    int areaUnderControl;
    @Override
    public void partner(Lion other) {
        this.areaUnderControl += other.areaUnderControl; 
    }
}

class Human implements Animal<Human> {
    int money;
    @Override
    public void partner(Human other) {
        this.money += other.money;
    }
}

However someone can still abuse this as

class Bear implements Animal<Lion> {
    int honey;
    @Override
    public void partner(Lion other) {
        this.honey += other.areaUnderControl; //Problem
    }
}

where a Bear is aspiring to partner with Lion. Is there someway that I can tie it to the type and subtypes of this

I tried searching through the existing questions and somehow I feel the design of the interface itself may be flawed. If someone can point out why this is wrong or the right way to achieve this, it would be greatly appreciated.

Thanks!

  • 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-07T19:19:55+00:00Added an answer on June 7, 2026 at 7:19 pm

    There is no way to enforce this at the interface level, because there is no reason to do so from a type safety point of view. So what if it’s possible to define a class Bear implements Animal<Lion>? By itself, it’s type safe (i.e. will not throw unexpected ClassCastException, etc.) — that’s the whole point that Generics is for.

    In fact, from a type safety point of view, most likely you just need interface Animal<T> (analogous with interface Comparable<T>). Yes, this will allow someone to define strange classes that implement Animal with a type parameter that is a completely unrelated class. So what? There is nothing wrong with that per se.

    In the places that use this interface, they can enforce the relationship that you want, like public <T extends Animal<? super T>> void someMethodThatUsesAnimal(T animal). Because it is only in the places that use it that really cares whether the partner method takes itself as a parameter. This is analogous to how Comparable is used (e.g. public <T extends Comparable<? super T>> void sort(List<T> list))

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

Sidebar

Related Questions

I have a newbie question: How do I invoke the following method: public void
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question. I have two c# classes - a code class (say, CodeClass) and
Django Newbie question. I have the following model: class Leg(models.Model): drive_date = models.DateField() startpoint
I have a newbie android question: Why arent strings and other resources like colors
I have a newbie question about how to assign class member (setter). I am
I have a newbie question about integrating two iOS apps. I have created an
I have a newbie question regarding when to release the elements of a NSArray.
I have a newbie WPF question. Imagine my user control has a namespace declaration
Swing newbie question... I have a system where there is a large number of

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.