3

The solutions I've seen online make sense; if you know the type of the variable, then you know the type of its value. Java makes it that way; however, if I have a system of inherited classes such as this ...

DynastyPQ (base class)
FirstPQ (inherited class)

And create the objects in this manner ...

DynastyPQ pq = new FirstPQ();

Is there a way to get the type of FirstPQ so that I can use it in a cast so that I can access the class's exclusive methods? Maybe something akin to this?

(typeof(pq's value)pq).exclusiveMethod()
Jacob Macallan
  • 959
  • 2
  • 8
  • 29
  • 3
    No, not without using reflection (which bypasses type safety). It's not quite as per your code, but you could do `if (pq instanceof FirstPQ) ((FirstPQ)pq).exclusiveMethod();` – Bohemian Dec 25 '15 at 23:30
  • 1
    @Bohemian Could use a visitor. It may be excessive for what he wants, but technically you could check the type without reflection – Vince Dec 25 '15 at 23:54
  • try the 'getClass()' method – Vishal Vijayan Dec 26 '15 at 01:55

3 Answers3

1

You have a few options.

  1. You could use reflection
  2. You could use instanceof
  3. You could use the visitor pattern

For these examples, we will attempt to find the type of this variable:

Object obj = new TargetType();

We want to see if the object referenced by obj is of type TargetType.


Reflection

There are a couple ways you could do this:

if(obj == TargetType.class) {
    //do something
}

The idea behind the code above is that getClass() returns a reference to the Class object used to instantiate that object. You can compare the reference.

if(TargetType.class.isInstance(obj)) {
    //do something
}

Class#isInstance checks to see if the object value passed to method is an instance of the class we are calling isInstance on. It will return false if obj null, so no null check is needed. This requires casting to perform operations on the object.


instanceof

This one is simple:

if(obj instanceof TargetType) {
    //do something
}

instanceof is part of the language specification. This returns false if obj is null. This requires casting to perform operations on the object.


Visitor Pattern

I have explained this in detail in one of my other answers. You would be in charge of handling null. You should look deeper into the pattern to see if it's right for your situation, as it could be an overkill. This does not require casting.

Community
  • 1
  • 1
Vince
  • 14,470
  • 7
  • 39
  • 84
1

Try the getClass() method. This will return the run time class of the particular object. http://www.tutorialspoint.com/java/lang/object_getclass.htm

Vishal Vijayan
  • 308
  • 1
  • 4
  • 13
  • The question is, how to cast to this runtime class at runtime – smac89 Dec 26 '15 at 02:12
  • @Smac89 `getClass().cast()`? Note: I have no idea...This is all unknown territory for me, but It's a lot of interesting info I'm learning through the concepts and answers to this question. =) – CosmicGiant Dec 26 '15 at 02:25
0

There are a few options:

  1. With the instanceof operator:

    • if (pq instanceof FirstPQ) {((FirstPQ)pq).exclusiveMethod();}
  2. With the Class.isInstance(Object obj) instance method:

    • FirstPQ firstPQ; if(pq.getClass().isInstance(firstPQ)) { firstPQ = (FirstPQ)pq; }
      (Note: Not yet tested. Confirmed.)
  3. With a visitor pattern:
Community
  • 1
  • 1
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
  • Not 100% true, check out my answer – Vince Dec 26 '15 at 01:49
  • @VinceEmigh What isn't (100% true)? – CosmicGiant Dec 26 '15 at 02:07
  • "*this can't really be done in a dynamic, non-declarative way in Java without bypassing type-safety with reflection.*" - That's the statement I was referring to. Your answer is valid, it's just that statement I found to be a bit off – Vince Dec 26 '15 at 02:10