2

I have a base class called class Base and two children classes

class A extends Base

and

class B extends Base

I have a method foo in Base.

Rather than putting the implementation of foo in class A and class B, so that I can do

void foo (Object o)
{
    // A's implementation
    assert o instanceof A;     
}


 void foo (Object o)
 {
     // B's implementation
     assert o instanceof B; 
 }

Is there anyway to put foo in Base, and still still be able to check for the runtime class? I've thought of something like this:

 void foo (Object o)
 {
    // Check that o is instanceof a runtime class
    assert o instanceof this.getClass(); // ????
 }

Thanks.

2
  • 1
    Why on earth would you want to do something like that ? Commented Jun 18, 2012 at 15:16
  • yes, it's possible with the getClass().isInstance(obj) method, but why would you like to do this? If the foo overload is specific for each subclass, then you should do implement that in the subclass. Making a superclass aware of it's subclasses is bad. If the superclass needs to do something that only the subclass will know, consider using an abstract method declaration instead. Commented Jun 18, 2012 at 15:27

5 Answers 5

6

You can implement your method like this:

public void foo() {
    if (this instanceof A) {
        // implementation for A
    }
    else if (this instanceof B) {
        // implementation for B
    }
}

But the point of polymorphism is to put the A implementation in A, so that this implementation can use A's private fields to implement the method (same for B, or course).

Sign up to request clarification or add additional context in comments.

Comments

2
getClass().isInstance(o)

That said, perhaps you want to constrain the type in a way the compiler can check? Generics can do that:

class Base<T extends Base<B>> {
    void foo(T o) { ... }
}

class A extends Base<A> {
    @Override void foo(A o) { ... }
}

Then,

new B().foo(new A()); 

will not compile.

Comments

1

There is class method isAssignableFrom()

   getClass().isAssignableFrom(o.getClass())

3 Comments

Erm, shouldn't it be the other way around? As in getClass().isAssignableFrom(o.getClass())?
should it not be the reverse?
Yes, it is my mistake. Updated
1

instanceof will not work since the parameter can not be 'dynamic'.

You could use the isInstance method of Class

void foo (Object o)
{
    // Check that o is instanceof a runtime class
    assert getClass().isInstance(o);
}

but this has at least one possible problem:
it will result in an AssertionException if this is an instance of a subclass of A and the object is just a direct instance of A!

Comments

0

In Java, you can check the class of an object using the instanceof operator: object instanceof M

When you have a Class object, it would make sense to write: object.getClass().isAssignableTo(MyClass.class)

http://www.ralfebert.de/blog/java/isassignablefrom/

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.