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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:21:13+00:00 2026-06-14T01:21:13+00:00

I’m building a Contact Manager in Java. I have a superclass called Contact which

  • 0

I’m building a “Contact Manager” in Java.

I have a superclass called “Contact which has two base classes; PersonalContact and BusinessContact.

I have an interface called Event, which is implemented by classes Birthday and Meeting. (Birthday contains one DateTime object while Meeting has two for start and end time).

PersonalContact holds a TreeSet of Birthdays and BusinessContact holds a set of Meetings.

Now, in the superclass Contact, I want to create an abstract method called “getEventsWithinPeriod()” which will return a TreeSet of all birthdays and/or meetings within a given time span.

The problem is, I don’t know how to tell the abstract method, and then the base class methods what to return.

For example, this is the code I used in Contact;

public abstract Set<Event> getEventsWithinPeriod(DateTime start, DateTime end);

And in PersonalContact;

public Set<Birthday> getEventsWithinPeriod(DateTime start, DateTime end){

      Set<Birthday> birthdaysThatAreWithin = new TreeSet<Birthday>();
      //CODE
      return birthdaysThatAreWithin;

However, in the compiler, I’m getting an error on Set<Birthday> saying;

“The return type is incompatible with Contact.getEventsWithinPeriod(DateTime, DateTime)”

What are the proper terms and returns I should be using? Why is my current attempt wrong?

  • 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-14T01:21:14+00:00Added an answer on June 14, 2026 at 1:21 am

    You have 3 solutions.

    Solution 1

    First, you can make your classes generic, like so:

    public abstract class Contact<E extends Event> {
        // ...
    
        public abstract Set<E> getEventsWithinPeriod(DateTime start, DateTime end);
    }
    

    And then in your concrete implementation:

    public class PersonalContact extends Contact<Birthday> {
    
        public Set<Birthday> getEventsWithinPeriod(DateTime start, DateTime end) { ... }
    }
    

    This is the best solution, but you have some alternatives.

    Solution 2

    You can change the type of your birthdaysThatAreWithin field:

    Set<Event> birthdaysThatAreWithin = new TreeSet<Event>();
    

    as well as change the method signature:

    public Set<Event> getEventsWithinPeriod(DateTime start, DateTime end) {
    

    and return it like that. This restricts you because you can’t use the events as Birthday instances anymore.

    Solution 3

    You could also change your method signature (in both your abstract and concrete class) to this:

    public Set<? extends Event> getEventsWithinPeriod(DateTime start, DateTime end)
    

    and not change anything else. This has the same problem as solution 2, you won’t be able to use the events as Birthday instances without casting them.

    Edit: the downsides to 2 and 3 are that they will require casting. For example:

    PersonalContact contact = ... ;
    Set<Event> events = personalContact.getEventsWithinPeriod(start, end);
    // I know all the events are birthdays, but I still have to do this:
    for (Event event : events) {
        if (event instanceof Birthday) {
            Birthday birthday = (Birthday) event;
            // Do stuff with birthday
        } // else maybe log some error or something
    }
    

    With the first solution, you’d have this:

    PersonalContact contact = ... ;
    Set<Birthday> birthdays = personalContact.getEventsWithinPeriod(start, end);
    for (Birthday birthday : birthdays) {
        // Do stuff with birthday
    }
    

    The code looks cleaner and runs better because you don’t have to do instanceof checks to make sure you don’t get a ClassCastException. You can also have stuff like this:

    public static void processBirthdaysFor(Contact<Birthday> birthdayContact, DateTime start, DateTime end) {
        Set<Birthday> birthdays = personalContact.getEventsWithinPeriod(start, end);
        for (Birthday birthday : birthdays) {
            // Do stuff with birthday
        }
    }
    

    And if you ever have another implementation of Contact that has Birthday events, you can pass them to that processBirthdaysFor method without making any changes.

    However, if you only need the events and you don’t care what the types are in the code calling your Contact.getEventsWithinPeriod, then solutions 2 and 3 are definitely your best bets. I’d personally just use solution 2 if this was the situation.

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

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.