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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:34:28+00:00 2026-05-16T20:34:28+00:00

I have an abstract class AbstractEvent and some real classes extending it. I want

  • 0

I have an abstract class AbstractEvent and some “real” classes extending it. I want to make an abstract class AbstractListener with a method process(??? event) so that non-abstract classes extending AbstractListener would be required to have at least one method accepting a class extending AbstractEvent. Is that possible?

  • 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-16T20:34:28+00:00Added an answer on May 16, 2026 at 8:34 pm

    You’ve already got the name of the mechanism you want – generics!

    First, make your event class:

    abstract class AbstractEvent {
        // Insert fields/methods common for all events here
    }
    

    Nothing strange about that. Next, create a parameterized listener class/interface, and give its type parameter an upper bound to your event object class:

    interface Listener<T extends AbstractEvent> {
        void process(T event);
    }
    

    You can now go on making your specific event classes:

    class PonyEvent extends AbstractEvent {
        // Pony-specific stuff goes here
    }
    

    And, well, that should be pretty much all you need. Go on and implement your listener classes:

    class LoggingPonyListener implements Listener<PonyEvent> {
        @Override
        public void process(PonyEvent event){
            System.out.println("Pony event occurred: " + event);
        }
    }
    

    Now, you may be tempted to write a generic event dispatching class like this:

    class EventDispatcher<T extends AbstractEvent> {
        private final List<Listener<T>> listeners =
            new CopyOnWriteArrayList<Listener<T>>();
        public void addListener(Listener<T> listener) {
            listeners.add(listener);
        }
        public void dispatchEvent(T event) {
            for (Listener<T> listener : listeners) 
                listener.process(event);
        }
    
    }
    

    Looks pretty sweet, eh? You can do stuff like this:

    EventDispatcher<PonyEvent> dispatcher = new EventDispatcher<PonyEvent>();
    dispatcher.add(new LoggingPonyListener());
    dispatcher.dispatchEvent(new PonyEvent());
    

    Totally sweet, we can just use this stuff, and then when we’ve used it, just keep on reusing it. There’s one problem though. Assume you’ve got a clever developer who wants a super-simple listener that doesn’t actually do anything with the event object, but just prints a specified message whenever the event occurs.

    Not really considering your awesome EventDispatcher utility class, it was written thus:

    class DebugListener implements Listener<AbstractEvent> {
        private final String msg;
        public DebugListener(String msg) { this.msg = msg; }
        @Override
        public void process(AbstractEvent event){
            System.out.println(msg);
        }
    }
    

    This should be reusable, right? No. This wouldn’t work:

    EventDispatcher<PonyEvent> dispatcher = new EventDispatcher<PonyEvent>();
    dispatcher.add(new DebugListener("pony event"));
    

    Because DebugListener is a Listener<AbstractEvent>, not a Listener<PonyEvent>. The way to solve this would be to use a lower bound for the parameter type:

    class EventDispatcher<T extends AbstractEvent> {
        private final List<Listener<? super T>> listeners =
            new CopyOnWriteArrayList<Listener<? super T>>();
        public void addListener(Listener<? super T> listener) {
            listeners.add(listener);
        }
        public void dispatchEvent(T event) {
            for (Listener<? super T> listener : listeners) 
                listener.process(event);
        }
    
    }
    

    This gives you the behaviour you’re after: Just like you can send a PonyEvent to the process method of a Listener<AbstractEvent> (because a PonyEvent is-an AbstractEvent), you can now use the event dispatcher class parameterized with a type to fire listeners parameterized with one of its supertypes.

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

Sidebar

Related Questions

I have an abstract class Camera. There are some derived classes that will handle
I have an abstract class that has a virtual method. The method is virtual
I have one abstract class -let's say myBase. And I want all the classes
I have abstract base class that contains some fields and some methods that act
I have some abstract base class that defines abstract functions. For example (barebones), abstract
I have an abstract base class from which many classes are derived. I want
I have an abstract class MotherClass and two abstract classes ChildClass1 and ChildClass2 that
I have an abstract class that has a shared method which creates a new
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have one abstract class and many child classes. In child classes are from

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.