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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:43:37+00:00 2026-05-15T18:43:37+00:00

I have an abstract class A and several implementations of it. I expect this

  • 0

I have an abstract class A and several implementations of it. I expect this to evolve over time, by adding more implementations.

I also have an interface that does something at instances of the above class hierarchy (eg print them).

I want implementations of the interface to provide some special functionality for some of the subclasses of A and a default functionality for the rest of them.

I hope this example clarifies things:

abstract class A { }
class B extends A { }
class C extends A { }

interface Processor  {
    public void process(A a);
}

class SimpleProcessor implements Processor {

    //I want this to be called when argument is instance of A or C or any
    //new class that will be added in the future
    public void process(A a) {
        //Line 14
        System.out.println("Default processing");
    }

    //I want this to be called when argument is instance of B
    public void process(B b) {
        System.out.println("Special processing");
    }

}

public class Runner {

    public static void main(String[] args) {
        B b = new B();
        Processor p = new SimpleProcessor();
        p.process(b);
    }

}

The example prints “Default processing”. The problem is that the method to be executed is chosen based at the compile-time type of the interface’s method. Is there a way (or design pattern) to make this program print “Special processing” without adding at line 14 a list of

if (a instance of B)
  process( (B) a );

for every class that needs special processing?

I had a look at the visitor pattern but it doesn’t seem like an improvement because I don’t want to “pollute” the Processor interface with methods for every subclass of A because more subclasses of A will be added.

To put it another way, I want the implementations of the interface to:

  • provide custom implementation of the method for specific subclasses of A
  • provide a default implementation for classes that will be added in the future
  • avoid listing all the classes in a large if-then-else list

Thanks!!

  • 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-15T18:43:38+00:00Added an answer on May 15, 2026 at 6:43 pm

    How about if you create an Adapter which take the object you want to process and return the processor for that object?

    if A -> return ProcessorA
    if B -> return ProcessorB
    

    code example:

    class Adapter {
    
        Processor getProcessor(Object o) {
            if (o instance of A) {
                return new ProcessorA();
            } else if ...
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface Tree and an abstract class RBTree which implements this interface.
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes
Let say I have abstract class called: Tenant and Customer. The tenant in this
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class that has a virtual method. The method is virtual
I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden
I have several queues that all extend a Queue class. I want to do
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have the following problem: 1) There is some abstract class A with several
I have an abstract entity base class defined like this: public abstract class SessionItem

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.