Get child class name from parent

60,382

Solution 1

Use instanceof operator.

Supposing you have a base class and two subclasses named Base, SubOne and SubTwo, if you want to check if a variable ref is an instance of SubOne or SubTwo you'd say:

if(ref instanceof SubOne){
}
else if(ref instanceof SubTwo){
}

Note that: (ref instanceof Base) will always return true though.

Solution 2

On some occasions simply this line in the parent class solves this problem. It returns the name of the "child" class (not the parent):

this.getClass().getName() //String like "com.mycompany.myclassname"
this.getClass().getSimpleName() //String like "myclassname"

See here for further discussion: http://www.coderanch.com/t/324715/java/java/Getting-child-class-name-parent

Solution 3

Instead of using an if statement you should create an abstract method for your conditional logic. Then have the child class run the code that is correct for it.

This also will keep you from having to modify the base class every time you create a new child class.

Solution 4

I think you want to use the instanceof operator, for example:

if(this instanceof SomeCustomActivity) {
    // do stuff
} else if (this instanceof AnotherCustomActivity) {
    // do other stuff
}

And that is all there is to it.

Solution 5

You can also use the .getClass() method of the parent and then check like this

if(parent.getClass().equals(childObj.class)){
//to do ..
}

This basically works because it returns the runtime object class. This will especially work in case of abstract classes - Tried and tested recently by me in my project.

Share:
60,382

Related videos on Youtube

Rich
Author by

Rich

I work here Tinder I used to work here Flipagram Trello Urbanspoon / Zomato Raizlabs Infusion TradeStation

Updated on February 15, 2021

Comments

  • Rich
    Rich over 3 years

    I have a base class for my all of activities (ActivityBase) that itself derives from android.app.Activity. In onCreate I want to execute some conditional logic based on the currently executing child class. If SomeCustomActivity and AnotherCustomActivity both extend ActivityBase, how can I determine in the parent class (ActivityBase) which of the two is the currently executing one?

    • Stephen C
      Stephen C over 13 years
      The question and the title do not match. You do not want the child classes name at all. Rather you want to test if this is an instance of a particular child class.
    • Rich
      Rich over 13 years
      Is the super class not also called the "parent"? I've heard relationships defined as A derives from B - B is the parent, A is the child.
    • gailbear
      gailbear over 13 years
      Yes, the super class is called the parent, but you don't specifically need to know the name of the child class, you only need to know what class the child is an instance of.
    • Rich
      Rich over 13 years
      I appreciate that I got the semantics wrong, but personally I actually want the name. I want to have my activities behave one of two ways, so I wanted to declare a list of Activities in BaseActivity and say "if the currently executing Activity is one of those in this list, execute this code block". I was thinking that keeping a list of the name would be the cleanest solution. I don't need all the activities to implement it because it's the same thing (would be repeated code). I guess I could make a List<Class> or something like that instead of comparing the names.
  • Rich
    Rich over 13 years
    I don't need to re-implement anything over and over again though. I just want to find out if the Activity is one that should execute a certain piece of code, otherwise skip it. Let's say 15 do and 15 don't, for example. All 15 should execute the same code block...should be done in the base class and just conditionally execute it.
  • Evvo
    Evvo about 7 years
    This is not an object oriented solution. You're better off with using this.getClass().getSimpleName(). That way you won't have to modify your parent code each time you introduce a new child class. Or give the parent an abstract method called getName() so each child will return its own name.