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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:35:24+00:00 2026-06-05T07:35:24+00:00

I am learning java generics, and trying to implement it for events before someone

  • 0

I am learning java generics, and trying to implement it for events
before someone asks, i have considered using polymorphism, but since I want every event to send different values to the handler’s start method, I can’t do this.

This is my event handler:

    public class MyHandler {
      public void startEventAction(int num) {
        //do something with num
      }  
    }

This is my template event:

     public abstract class SomeEvent<E> {
       protected List<E> handler_list = new LinkedList<E>();
       public SomeEvent() {
       }
       public Boolean addHandler(E handler) {
          return handler_list.add(handler);
       }
       public Boolean removeHandler(E handler) {
          return handler_list.remove(handler);
       } 
     }

and the event itself is: (now i know it’s probably wrong to extend the SomeEvent like this, but it seems to work)

    public class MyEvent<MyHandler> extends SomeEvent<MyHandler> {
      public void executeHandlers() {   
        for (MyHandler handler : handler_list) {
            //Exception here!!
            handler.startEventAction(num);
        }
      }
    }

My question is how is it right to extend it and why does the function startEventAction doesn’t exist for MyHandler in the last code block(for MyEvent)
Answers will be very much appreciated.

  • 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-05T07:35:26+00:00Added an answer on June 5, 2026 at 7:35 am

    I think it should be

    public class MyEvent<T extends MyHandler> extends SomeEvent<T> {
          public void executeHandlers() {   
            for (T handler : handler_list) {
                //Exception here!!
                handler.startEventAction(lvc_count);
            }
        }
    

    If you use MyEvent<MyHandler> you’re considering MyHandler as a type parameter, not as a type argument. That’s why I typically rather use letters as type paramers instead of entire words, this avoids the confusion between them and classes and interfaces names.

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

Sidebar

Related Questions

I've been learning and experimenting with Java Generics for a while but I have
I am learning java on my own. I have books to read. but i
I'm a C# guy learning Java and I'm trying to understand how generics work
I learned java generics some time ago, but now I'm learning collections and found
just learning Java and I know this may sound stupid but I have to
I'm learning java and have a bit of code I am trying to write
I'm learning generics in Java from C++ and am confused how to do math
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
I'm learning Java by reading the online Java book, but im finding it hard
I am learning java. I am trying to use composite design pattern. I am

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.