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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:27:48+00:00 2026-06-01T19:27:48+00:00

So I have this interface public interface EventHandler<E extends EventObject> { public void handleEvent(E

  • 0

So I have this interface

public interface EventHandler<E extends EventObject>
{   
    public void handleEvent(E event);
}

and I want to derive classes for it that handle events, like so:

public class WorkingHandler implements EventHandler<MouseEvent>
{
    public void handleEvent(MouseEvent event)
    {
        //handle event
    }
}

Above example works fine. But compiler won’t let me implement EventHandler multiple times, due to the
extremely frustrating Type Erasure.

public class NotWorkingHandler implements EventHandler<MouseEvent>, EventHandler<KeyEvent>
{
    public void handleEvent(MouseEvent event)
    {
        //handle mouse event
    }

    public void handleEvent(KeyEvent event)
    {
        //handle key event
    }
}

Several questions…

  1. Why does Java not allow Type Erasure? IIRC it is something to do with being compatible with earlier versions of Java, correct?

EDIT: I mean why does Java have Type Erasure (had it the other way around)

  1. Is there any sort of “mod”/”extension” (for lack of a better word) or programming languages that allow me to get around Type Erasure? As I am not using older versions of Java I don’t care about my code being compatible with older code.

  2. Are there any workarounds, within the Java language, that I can use to get around Type Erasure?

  3. If no, what are some alternate ways to code event handling in my program that keeps the compiler happy?

  • 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-01T19:27:50+00:00Added an answer on June 1, 2026 at 7:27 pm

    I’m not sure that your understanding of Type Erasure in Java is correct. As explained here, the type of your generic class is lost at runtime.

    In response to your questions:

    1. I think you are right that Java implements type erasure to enable compatibility with previous versions of the langauge.
    2. No I don’t think there is a “mod.”
    3. No there are no workarounds (types are removed at compile time), but you can use reflection.
    4. See below.

    I think a simpler solution would be to so something like this:

    public class WorkingHandler implements EventHandler<EventObject>
    {
        public void handleEvent(EventObject event)
        {
            // handle event
            if (event instanceof MouseEvent)
                handleMouseEvent((MouseEvent) event);
            ...
        }
    
        ...
    
        private void handleMouseEvent(MouseEvent event)
        {
            // handle mice
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
i have this code public interface Interfcae1 { void OP1(); } public interface Interfcae2
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
If I have this classes: public interface IBinaryCalculator{ double addition(double numb1, double numb2); }
Is there a .NET framework interface like this? public interface IEvent { event EventHandler
So lets say I have this interface: public interface IBox { public void setSize(int
I have this interface: public interface ISectionListItem { public int getLayoutId(); public void setProps(View
I have an interface -- EventHandler -- that declares several methods. public interface EventHandler
I have this code (which is way simplified from the real code): public interface
Suppose I have this annotation @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Name { String value(); }

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.