2

So, I want to be able to get an instance of a subclass that is being run when it calls a method from the super class. For example, if I had this class:

public class A {
public void aMethod() {
    //Here is where I want to see if class B is calling the code
}
}

public class B extends A {
}

public class C {
B b = new B();
b.aMethod();
}

And, like the comment says, I want to check, in aMethod, if class B, the subclass of class A, is calling the code.

nh_99
  • 58
  • 7
  • The fact that `B` extends `A` here is basically irrelevant; you haven't created an instance of `B` here... – Oliver Charlesworth Jan 28 '14 at 22:20
  • The code doesn't even compile. – JB Nizet Jan 28 '14 at 22:21
  • 6
    If you want to "see if class B is calling the code", you can say `this instanceof B` (which is also `true` if it's a subclass of `B`), or `this.getClass().equals(B.class)` (which is `true` only for `B`, not its subclasses). But: **don't do this.** 99.9967% of the time you just need to rethink your design and use polymorphism properly. – ajb Jan 28 '14 at 22:25

4 Answers4

4

As has been pointed out to you, there is almost never a good reason to do this and I agree that you should be using polymorphism instead. However, if you "need" to do this or just want to know how to go about doing something like this, you can use instanceof on this inside of the method:

class A {
    public void aMethod() {
        if (this instanceof B) {
            System.out.println("I'm a B!");
        }
    }
}

public class B extends A {

    public static void main(String[] args) {
        B b = new B();
        b.aMethod();
    }
}
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • 2
    That's the correct way to answer the question, but I want to reemphasize what I said in my comment: **don't do it**. Polymorphism was invented so that programmers wouldn't have to write stuff like this. – ajb Jan 28 '14 at 22:30
  • I agree. Updated the answer to re-iterate that. – Martin Dinov Jan 28 '14 at 22:33
1
public class A {

    public void aMethod() {
        if(this.getClass() == B.class){
            System.out.println("huhuhuuuuuuuuuuuuuuuuu");
        }
    }
}

public class B extends A {


}

public class C {

    public static void main(String[] args) {
        B b = new B();
        b.aMethod();
    }
}
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
0

Note that this finds a call at any depth:

for(final StackTraceElement element : Thread.currentThread().getStackTrace()) {
  if (element.getClassName().equals(B.class.getName())) {
    System.out.println("BINGO");    
  }
}

If you want to check only a limited depth, don't iterate through all of the array.

This can be useful e.g. if some framework forces you to have a special method or a no-arg constructor to be present, but you don't want any developer to call this method directly. (Yes, it is a hack, but sometimes odd frameworks force you to do odd things). Then you can have an assertion in the unwanted method that just throws an exception if it is called by the wrong corner of your code.

Anyway you should try do avoid things like this if possible.

yankee
  • 38,872
  • 15
  • 103
  • 162
0

Check here: How to get the caller class in Java

The 2nd part of the answer from @dystroy is probably a start.

Community
  • 1
  • 1
bgse
  • 8,237
  • 2
  • 37
  • 39