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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:32:10+00:00 2026-05-24T03:32:10+00:00

I have a superclass and child classes. I am in a child class and

  • 0

I have a superclass and child classes.
I am in a child class and in a super method I want to check if a certain parameter object is of the certain child class.

I want something like

 if (MyObj instanceof this)

what is the most efficient way to do it?
I know I can do

   if (MyObj.getClass.equals(this.getClass)

But that isnt too eficient

  • 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-24T03:32:11+00:00Added an answer on May 24, 2026 at 3:32 am

    Referencing subclasses in superclass methods is indicative of a design problem in need of refactoring. While you can do it (if (this instanceof SubClass)), it tightly couples the superclass to the subclass(es). In class-based OOP, subclasses are of course tightly coupled to the superclass (inheriting is inherently tightly-coupling), but the converse is rarely true and almost never appropriate.

    In terms of efficiency, if (this instanceof SomeClass) is fine.

    Edit: From your edit, it almost seems like you’re trying to find out in the superclass’s method whether it’s being called on an instance whose final run-time class is the superclass, and not a subclass. That’s a slightly different thing, and AFAIK the most efficient way to do it pretty much as you’ve quoted:

    if (this.getClass().equals(TheClass.class))
    

    It still suggests that the class wants to be refactored a bit, though.

    Edit 2: But from your comment on the question, it sounds like you want to compare it with the class of a member you’re holding. If the member is an instance of the class itself, use the above. If it may vary, then:

    if (this.getClass().equals(theMember.getClass()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes which extends a superclass, and in the JSP I want
I have this object which is an instance of a superclass. I want to
I have done a few projects lately using a Database Object super class which
I have a super super (grandparent) abstract bean class calling loadData() method in its
Assuming I have the following classes in different files: <?php namespace MyNS; class superclass
Suppose I have two classes like so: class Parent def say I am a
I have an AbstractEntity class as superclass for all my entites that defines an
I have an object of a subclass extending its superclass. There is an overridden
I have a subclass that calls a method from a superclass. The method in
I have several classes, that all derives from SuperClass. When the classes are created,

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.