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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:55:04+00:00 2026-06-02T22:55:04+00:00

I have this generic interface: public interface TjbListener<T> { public void hearChange(T t); }

  • 0

I have this generic interface:

public interface TjbListener<T> {
    public void hearChange(T t);
}

Which I use like this:

public interface ObjectOneListener extends TjbListener<ClassOne> {

}

I would like to write an abstract generic class A which takes a generic type U as a parameter and has a method (1) which itself calls a method (2) on U. Below is my attempt U should extend (or implement maybe?) the generic TjbListener interface.

public abstract class ListenerInformer<U extends TjbListener<"what should I write here">> {
    List<U> mListeners = new ArrayList<U>();

    public void addListener(U u){
        mListeners.add(u);
    }

    public void informAll("what should I write here"){
        for(U u:mListeners){
            u.hearChange("what should I write here");
        }
    }
}

One solution I thought of as I was writing this question is below, but I don’t know if it’s really a solution, or if it has subtle problems I don’t understand:

public abstract class ListenerInformer<U extends TjbListener<T>,T> {
    List<U> mListeners = new ArrayList<U>();

    public void addListener(U u){
        mListeners.add(u);
    }

    public void informAll(T t){
        for(U u:mListeners){
            u.hearChange(t);
        }
    }
}

UPDATE: BEWARE

I have just discovered that this approach is almost useless for my particular case because the same class cannot implement the same interface with different parameters. See the question linked below. This means that I cannot have one class be a listener of two different types with my (or Johanna’s) solution, without using a different strategy like composition.

How to make a Java class that implements one interface with two generic types?

  • 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-02T22:55:06+00:00Added an answer on June 2, 2026 at 10:55 pm

    Your second example should work. But if it is as simple as that, then there is no need for the Generic U, because every instance of a subclass of TjbListener also is an instance of TjbListener.

    You can do more simple:

    public abstract class ListenerInformer<T> {
        List<TjbListener<T>> mListeners = new ArrayList<TjbListener<T>>();
    
        public void addListener(TjbListener<T> u){
            mListeners.add(u);
        }
    
        public void informAll(T t){
            for(TjbListener<T> u:mListeners){
                u.hearChange(t);
            }
        }
    }
    

    That works as your code does and is easier to handle.

    Two generic types is necessary if you need the final implementation type of the subclass of TjbListener as return value of parameter, for example if you have

     public U informAll2(T t){
        for(U u:mListeners){
            u.hearChange(t);
            if (...)
                return u;
        }
    }
    

    In this case your declaration with two generic types is correct (just I’m not sure if it is possible to declare the generic U, which depends of T, before you declare T, of if you have to declare T first, like public abstract class ListenerInformer<T, U extends TjbListener<T>> )

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

Sidebar

Related Questions

I got a generic Interface like this : public interface IResourceDataType<T> { void SetResourceValue(T
I have a generic interface for a mathematics library, something like this: [ContractClass(typeof(MathsDoubleContracts))] public
Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void
I have an interface which is generic: public interface DataTable < T > {
I have this interface public interface IDataPoint<T> extends Comparable<T> { public T getValue(); }
I have a generic interface public interface Consumer<E> { public void consume(E e); }
Ok I have a generic interface public IConfigurationValidator<T> { void Validate(); } a class
I have a C# project with a generic interface public interface IMyFoo<T> { void
I need to use a generic interface like the following: public interface IContainer<T> {
Lets say I have a class, which implements a generic interface public interface IItem

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.