1

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

3
  • 6
    You should try to avoid having to do that. Override the method in the child class, call super.theMethod(), and then put any child-class-specific processing after that. Commented Jul 25, 2011 at 13:25
  • 2
    Superclass should not know it's child classes even exist. There is almost certainly a better way to achieve what you are doing, but you can't be told which one it is if you don't say what you are actually trying to do. Commented Jul 25, 2011 at 13:28
  • Wait. I do know that . But I am holding the super class in the super class itself as a member. than I want to know if the parameter that I am getting is of the same type of the data member. I dont actually write the child class name . just to see if it is of the same type as the member.10x Commented Jul 25, 2011 at 13:32

2 Answers 2

7

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()))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I do use that. But I thought that there is a better solution since this is a string comparison
@user: It's not a string comparison (at least, not at the API level). getClass returns a Class instance, not a String. I don't have the JDK sources handy, but I doubt that Class#equals is using strings.
Actually, Class.equals() just uses the default Object.equals() implementation which is instance equality (this.getClass() == theMember.getClass()) since the classloader guarantees there are not two class objects representing the same class. It's extremely efficient.
@user: We don't actually need the sources. Class doesn't override equals, so it's using the default Object#equals, which is a == on the instances. So not a string compare. (I was silly enough that I went and got the sources first, then realized I didn't need them...)
1

Remember the difference between getClass and instanceof. instanceof will work if your object inherits from the class you're comparing it to, the comparison with getClass will not. So if you use getClass you will be cutting yourself off from being able to extend that subclass, while with instanceof you will still be able to make new subclasses of the subclasses without breaking this code. So you should consider whether the efficiency of using equals and getClass() is worth painting yourself into a corner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.