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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:32:19+00:00 2026-05-22T14:32:19+00:00

I am having troubles with the instanceof operator. I’m trying to avoid it. Basically,

  • 0

I am having troubles with the instanceof operator. I’m trying to avoid it. Basically, I have the following structure:

class Shape {}
class Triangle extends Shape {}
class Rectangle extends Shape {}

ShapeParser s;
while (s.hasNext())
     parseShape(s.next()); // returns a Shape object

void parseShape(Triangle t) { // do stuff } // KEY POINT HERE
void parseShape(Rectangle t) { // etc }

The key point I’m making is: I want to do a parameter overload of the function, but it’s not working as I intend it to (compile-error). I am trying to avoid:

void parseShape(Shape s)
{
     if (s instanceof Triangle) ...
}

UPDATE: it seems that the consensus is to create a base class method: parseShape() to do the
lifting. I wanted to clarify my question: the motivation of this question is relative to the observer pattern. Suppose I have the following Observer object payload method:

    public void update(Observable obj, Shape objectPayload){} 
// note: the objectPayload is usually of type Object

Instead of performing:

public void update(Observable obj, Shape objectPayload)
{
       if (objectPayload instanceof Triangle)
          // do stuff
       else if (objectPayload instanceof Rectangle)
          // etc
}

I want to do:

public void update(Observable obj, Shape objectPayload)
{
       parseShape(objectPayload);
}

    void parseShape(Triangle t) {  } // do stuff
    void parseShape(Rectangle t) { }
  • 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-22T14:32:19+00:00Added an answer on May 22, 2026 at 2:32 pm

    You can move the parseShape into each Shape class. Alternatively, you can use the Visitor pattern. There’s a neat trick with reflection shown in the solution to this thread that avoids the complexity of a full visitor pattern in Java.

    UPDATE:

    Here’s a recipe for the visitor pattern:

    1. Declare an interface:

      public interface ShapeVisitor {  
          visit(Triangle);  
          visit(Rectangle);  
          // ...  
      }
      
    2. In Shape, declare an abstract method acceptVisitor:

      class Shape {
          public abstract void acceptVisitor(ShapeVisitor visitor);
      }
      
    3. In each concrete class, implement acceptVisitor:

      class Triangle extends Shape {
          public void acceptVisitor(ShapeVisitor visitor) {
              visitor.visit(this);
          }
      }
      
    4. Declare your ParseVisitor class to implement ShapeVisitor and implement all the required methods (simply rename each of the parseShape methods to visit).

    The nice things about this are, first, it keeps the parsing code out of your Shape hierarchy and centralizes it in a separate parsing class; second, if you later decide that you need to do some other operation (like, say, render), you can apply the same pattern without ever changing any Shape class. The big down side of this approach is that you will have to change all classes that implement ShapeVisitor if you decide to add another Shape subclass.

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

Sidebar

Related Questions

I am having trouble serializing an object to xml. I have one class that
I'm having trouble with classes in haskell. Basically, I have an algorithm (a weird
I'm having trouble assigning an instance of a class to the following UITableViewController subclass:
I'm having trouble the same instance of a C# class being used in multiple
I'm having troubles with HttpWebRequest/HttpWebResponse and cookies/CookieContainer/CookieCollection. The thing is, if the web server
I am having troubles migrating from OC4J 10.1.2.3 to 10.1.3.1.4. The problem is for
I'm having troubles with the order of my code in my program. right now
I'm having troubles using Blend with my visual studio solution. In Runtime, and compile
im currently having troubles on my codes in C#. I want to split strings
I am having troubles with jQuery's load function and am hoping for some help.

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.