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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:54:24+00:00 2026-06-06T00:54:24+00:00

Suppose you have a Java class hierarchy of about 30 classes, with a base

  • 0

Suppose you have a Java class hierarchy of about 30 classes, with a base class BaseClass and two main subclasses (SubclassA, SubclassB) with several subclasses each. A few of these subclasses have a certain behavior. Let’s say you can “poke” them, changing their state. (This is a new requirement adding behavior to an existing hierarchy. “Poke” is meaningless to the majority of classes.)

interface Pokeable {
  void poke();
  int getTimesPoked();
}


public class Pokey extends SubclassB 
implements Pokeable {
  private int timesPoked = 0;
  public void poke() {
    timesPoked++;
  }
  public int getTimesPoked() {
    return timesPoked;
  }
}

Should this be done by implementing Pokeable in only those classes that need it, and then doing the following in all code that must poke any object that’s pokeable?

public void process(BaseClass b) {
  if (b instanceof Pokeable) {
    ((Pokeable)b).poke();
  }
}

Or should the entire hierarchy implement Pokeable for the sake of the few that really ARE Pokeable?

interface Pokeable {
  void poke();
  int getTimesPoked();
  boolean isReallyPokeable();
}

public class BaseClass implements Pokeable {
  public void poke() {}
  public int getTimesPoked() { return 0; }
  public boolean isReallyPokeable() { return false;}
}

public class Pokey {
  private int timesPoked = 0;
  @Override
  public void poke() {
    timesPoked++;
  }
  @Override
  public int getTimesPoked() {
    return timesPoked;
  }
  @Override
  public boolean isReallyPokeable() {
    return true;
  }
}

public void process(BaseClass b) {
  b.poke();
}

Edit added: It’s kind of a Double Dispatch problem. Where “poker” code does something to an object, it must call “poke()” if the object is “pokeable” but can’t if not. Whether you “poke()” or not depends on whether something wants to poke and whether the object accepts being poked. I could use the Visitor pattern but that seems to make it more complicated.

  • 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-06T00:54:26+00:00Added an answer on June 6, 2026 at 12:54 am

    If a class is not pokable, I would recommend against it implementing the Pokeable interface, simply because that would be confusing.

    Take one of the following approaches instead.

    • Create an abstract or concrete subclass of BaseClass that implements Pokable, and have all of your to-be-Pokeable subclasses extend it. This approach works well if poke()ing is achieved via the same logic in all of the implementing classes.
    • Have each subclass implement Pokeable individually
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two questions about java.awt.Shape . Suppose I have two Shape s, shape1
Suppose I have a Java class with multiple constructors: class Base { Base(int arg1)
Suppose I just created a package example and have two classes inside it, Main
so suppose I have a java package.... it's got the main class with the
Say suppose I have the following Java code. public class Example { public static
Suppose I have two Java projects in Maven / Eclipse: An open source application
Suppose, I have a lot of classes, which are constructed using Java reflection (for
Suppose we have the Java code: Object arr = Array.newInstance(Array.class, 5); Would that run?
Suppose I have a Java class that has 100K worth of method code containing
Suppose i have 3 java classes A , B and C I need to

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.