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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:06:41+00:00 2026-05-13T10:06:41+00:00

Not sure how to title this… So I’ve got three child classes of Event

  • 0

Not sure how to title this…

So I’ve got three child classes of Event: WeightEvent, TimedEvent, RepEvent. Through whatever means, I get an object of one of the children. Now I want to send that child event to a method in another object so it can pull the data from it with the getSavedEvents() method. The method only exists in the children since pulling the data is specific to the type of event.

I started with

public void setEvent(Event e) {

but that cast my child object to an Event (parent) object.

Is there any way around this short of writing three different methods. One each for the children?

public void setEvent(WeightEvent e) {
public void setEvent(TimedEvent e) {
public void setEvent(RepEvent e) {

Thanks for any advice.

-John

  • 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-13T10:06:42+00:00Added an answer on May 13, 2026 at 10:06 am

    Instead of switching on the type you should call a method on the event that’s defined differently for each type of event type. This is called the Template method pattern. (It has nothing to do with C++ templates, BTW)

    Using this pattern, your EventTable class becomes something like this:

    public class EventTable {
      public void setEvent(Event e) {
        int x = 0;
        columns = e.getFields();
        Event[] savedEvents = e.getSavedEvents();
        for(Event ev : savedEvents) {
          tempdata[x] = ev.getTempData();
          x++;
        }
      }
    }
    

    Note that the entire switch has been replaced with a single call to getTempData(). This method is then abstract in Event, just like getSavedEvents:

    public abstract class Event {
      public Date getDate() { return(_date); }
      public abstract Event[] getSavedEvents();
      public abstract int[] getTempData();
      public int[] getFormattedDate() {
        ...
    
    }
    

    Then you define the getTempData() method in each subclass. For example:

    public class WeightEvent extends Event {
      public int getWeight() { return(_weight); }
      public int getReps() { return(_reps); }
      public int[] getTempData() {
        return new int[]{
          getFormattedDate()[0],
          getWeight(),
          getReps()
        };
      }
    }
    
    public class TimedEvent extends Event {
      public String getTimeInHMS() { return(_timeString); }
      public int[] getTempData() {
        return new int[]{
          getFormattedDate()[0],
          getTimeInHMS()
        };
      }
    }
    
    public class RepEvent extends Event {
      public int getReps() { return(_reps); }
      public int[] getTempData() {
        return new int[]{
          getFormattedDate()[0],
          getReps()
        };
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to title this question, but i'v got such a problem: Up
Not entirely sure of a good title for this, feel free to edit it
Not sure if I've just missed something but this doesn't work: $(this).children('td.threadtitle a').html('thread title');
My apologies for an inaccurate title, but I'm not sure what this is called
for sure this is not the best title. I'm creating a system to generate
I am not sure if the title is accurate. I have several classes like
I'm not even sure about how to title this qn. But, hope there is
Sorry for the somewhat confusing title. Not sure really how to title this. My
Not sure what the correct title for this question should be. I have the
I'm not even sure what title to give this question; hopefully the code will

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.