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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:27:49+00:00 2026-05-25T01:27:49+00:00

Is it bad practice to use the instanceof operator in the following context? public

  • 0

Is it bad practice to use the instanceof operator in the following context?

public interface IWriter {
    public abstract void write(Dto dto);
}

public abstract class Dto {
    private long id;
    public void setId(long id) {this.id = id;}
    public long getId() {return id;}
}

public class DtoA extends Dto {
   ...
}

public class DtoB extends Dto {
   ...
}

public class MyWriterA implements IWriter {
   @Override
   public void writer(Dto dto) {
     if (!(dto instanceof DtoA))
        return;
     ...
   }
}

public class MyWriterB implements IWriter {
   @Override
   public void writer(Dto dto) {
     if (!(dto instanceof DtoB))
        return;
     ...
   }
}

There is a lot of myths about the use of that operator and I am not completely
sure that what I am doing is not bunk.

I have a lot of different writer implementations
which I want to combine in one interface. The problem is not every DTO is applicable for every writer. In my actual code there is a deep hierarchy of DTOs, which extend DtoA and DtoB, and either the
hierarchy branch of DtoA or DtoB is applicable for a writer, but only in a few cases both.

Should I avoid using the abstract class Dto as argument for the abstract write(Dto dto) method?

EDIT: Please read the comments on the accepted answer.

  • 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-25T01:27:50+00:00Added an answer on May 25, 2026 at 1:27 am

    How is the code calling the IWriter? Presumably you’ve got to determine the type up there too? In which case you already know what kind of writer you need. You’re already dispatching on the basis of Type.

    Your writers are not really substitutable WriterA only does As and so on. In which case you’re not gaining anything by claiming they are in an inheritance hierarchy.

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

Sidebar

Related Questions

Consider the following code. public interface IFoo { } public class Bar { public
Consider the following test case, is it a bad practice to use the hashCode
1) Isn't it a bad practice to use these attributes on all of my
Is type checking considered bad practice even if you are checking against an interface?
Is it bad practice to use mutable objects as Hashmap keys? What happens when
Would it be considered a bad practice to nest a private static class inside
Is it bad practice to use the generated objects from Entity Framework as business
Is it considered a bad practice to use code-behind with ASP.NET MVC Views? Got
Is it a bad practice to use domain objects in Sets or as keys
Is it a bad practice to use <div> tags to express gaps between elements?

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.