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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:42:02+00:00 2026-05-23T13:42:02+00:00

Is using the instanceof keyword against the essence of object oriented programming ? I

  • 0

Is using the instanceof keyword against the essence of object oriented programming?
I mean is it a bad programming practice?
I read somewhere that using instanceof keyword means that the design may not be that good. Any better workaround?

  • 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-23T13:42:03+00:00Added an answer on May 23, 2026 at 1:42 pm

    Generally speaking yes. It’s best to keep all code that depends on being a specific class within that class, and using instanceof generally means that you’ve put some code outside that class.

    Look at this very simple example:

    public class Animal
    {
    }
    
    public class Dog extends Animal
    {
    }
    
    public class Cat extends Animal
    {
    }
    
    public class SomeOtherClass
    {
      public abstract String speak(Animal a)
      {
        String word = "";
    
        if (a instanceof Dog)
        {
          word = "woof";
        }
        else if (a instanceof Cat)
        {
          word = "miaow";
        }
    
        return word;
      }
    }
    

    Ideally, we’d like all of the behaviour that’s specific to dogs to be contained in the Dog class, rather than spread around our program. We can change that by rewriting our program like this:

    public abstract class Animal
    {
      public String speak();
    }
    
    public class Dog extends Animal
    {
      public String speak()
      {
        return "woof";
      }
    }
    
    public class Cat extends Animal
    {
      public String speak()
      {
        return "miaow";
      }
    }
    
    public class SomeOtherClass
    {
      public String speak(Animal a)
      {
        return a.speak();
      }
    }
    

    We’ve specified that an Animal has to have a speak method. Now SomeOtherClass doesn’t need to know the particular details of each type of animal – it can hand that off to the subclass of Animal.

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

Sidebar

Related Questions

When we say we lock on an object using the synchronized keyword, does it
I know that using the synchronize keyword before a method brings synchronization to that
Is there a preference or behavior difference between using: if(obj.getClass().isArray()) {} and if(obj instanceof
How can I create an instance of an object using a class reference, and
Is it possible to inject an instance of logger class object using Managed Extensibility
I'm trying to create an instance of a generic class using a Type object.
I read an article about using C# 3 features in C# 2 where you
Why can't I start a line using a parenthesis followed by the keyword new??
I understand the volatile keyword in Java can make the read/write operations of reference
I need some clarificaton on how the syncronized keyword works in java 6. Using

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.