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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:58:10+00:00 2026-05-16T00:58:10+00:00

Some of my classes should be treated differently. Which solution is better: Introduce new

  • 0

Some of my classes should be treated differently. Which solution is better:

  1. Introduce new interface to my class hierarchy and check whether the class implements it or not, using RTTI (runtime time identification)
  2. Add a method which returns boolean value that indicates whether this class should be treated normally or deserves special treatment

The following examples illustrates the above situations:

1.

interface SpecialTreatment {}

class Base {}    

class Special extends Base implements SpecialTreatment {}

class Normal extends Base {}


Base reference = new Special();
if(reference instanceof SpecialTreatment)
    // do something special
else
    // normal class

2.

interface Treatment {
    boolean special();
}

class Base {}

class Special extends Base implements Treatment {
    boolean special() { return true; }
}

class Normal extends Base implements Treatment {
    boolean special() { return false; }
}

Treatment reference = new Special();
if(reference.special() == true)
    // do something special
else
    // normal class
  • 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-16T00:58:11+00:00Added an answer on May 16, 2026 at 12:58 am

    If the ‘reason’ for treating the class differently is a design decision then mark it as so via an interface signature as in solution 1.

    If there is a chance that some state in your application would govern this, then use solution 2.

    And example of this, is the java.io.Serializable interface with no methods or fields that is used to identify implementations that are serializable. You decide at design time whether the object was serializable.

    A note on the performance of instanceof, after additional comment.

    You could also look at using Annotations as an alternative…

    — EDIT AFTER TAKING COMMENTS ON BOARD —

    If you really want to not go down the instanceof (which is very fast) or annotations you could do this..

    interface Special {
        bool isSpecial();
    }
    
    abstract class RuntimeSpecial implements Special {
        protected abstract bool _determineSpecial();
    
        public isSpecial() { return _determineSpecial(); }
    
    }
    
    class IsSpecial implements Special {
        public isSpecial() { return true; }
    }
    
    class IsNotSpecial implements Special {
        public isSpecial() { return false; }
    }
    

    Now extend whatever is appropriate….

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

Sidebar

Related Questions

I'm testing with EF 4.3 (beta) I have some new classes which should generate
I am trying to build some classes that inherit from a parent class, which
I am wondering should I write unit test for everything. There are some classes
I have developed some classes with similar behavior, they all implement the same interface.
I have some classes layed out like this class A { public virtual void
I'm packing some classes into ptr_map with any typed value. class EventManager { ptr_map<string,
I have a queue that should be used by some classes. it is the
I've read around about const and static readonly fields. We have some classes which
I have some classes which have Fonts and Brushes as private fields. Something says
I have some classes extended this way: class Baseresidence extends CActiveRecord { public static

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.