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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:12:35+00:00 2026-05-10T23:12:35+00:00

I have a java class which fires custom java events. The structure of the

  • 0

I have a java class which fires custom java events. The structure of the code is the following:

public class AEvent extends EventObject { ... }  public interface AListener extends EventListener {    public void event1(AEvent event);  }  public class A {    public synchronized void addAListener(AListener l) {   ..   }    public synchronized void removeAListener(AListener l) {   ..   }    protected void fireAListenerEvent1(AEvent event) {   ..   } } 

Everything works correctly, but I’d like to create a new subclass of A (call it B), which may fire a new event. I’m thinking of the following modification:

public class BEvent extends AEvent { ... }  public interface BListener extends AListener {    public void event2(BEvent event); }  public class B extends A {    public synchronized void addBListener(BListener l) {   ..   }    public synchronized void removeBListener(BListener l) {   ..   }    protected void fireBListenerEvent2(AEvent event) {   ..   }  } 

Is this the correct approach? I was searching the web for examples, but couldn’t find any.

There are a few things I don’t like in this solution:

  1. BListener has two methods one uses AEvent the other uses BEvent as a parameter.
  2. B class both has addAListener and addBListener methods. Should I hide addAListener with private keyword? [UPDATE: it’s not possible to hide with private keyword]
  3. Similar problem with fireAListenerEvent1 and fireBListenerEvent1 methods.

I’m using Java version 1.5.

  • 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. 2026-05-10T23:12:35+00:00Added an answer on May 10, 2026 at 11:12 pm

    I don’t see a reason why BListener should extend AListener.

    Do you really want to force everyone interested in B events to also implement event1()?

    Also you can’t add addAListener(), since a derived class can not reduce the visibility of a method that’s present in the parent class. Also, you shouldn’t need to, or you would violate the Liskov substitution principle (every B must be able to do everything an A can do).

    And as a last remark, I’d make the fire*() methods protected. There’s usually no reason at all to keep them public and reducing the number of public members keeps your public interface clean.

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I keep everything in a source control repository, indexed by… May 11, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer Here's a good article on the subject. May 11, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer In life, how do you distinguish the good from the… May 11, 2026 at 6:21 pm

Related Questions

I have a Java program that loads thirdparty class files (classes I did not
I want to use custom jsp tags to help build a menu in my
I have an exsisting Java/JSF website all the text on the screen is coming
My question pertains to multi-threading in Java. I'm translating an app I wrote in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.