In Java is it possible to check at runtime on which subclass a method was called? -
interface y { void search(string name); } class implements y { void search(string name) { //is possible say: "if called class b search("b"); } } class b extends { } public class main { public static void main(string[] args) { b b = new b(); b.search(); } }
given above code possible reason in superclass subclass used calling method?
the reason want because code in search similar subclasses, thing changes classname, thought there no need override in each subclass. have updated code reflect this. please let me know if there better way of doing it/
calling this.getclass()
inside search
method give concrete class of current instance.
for example:
class example { static class { public void search() { system.out.println(getclass()); } } static class b extends {} public static void main (string[] args) throws java.lang.exception { new a().search(); new b().search(); } }
outputs
class example$a class example$b
Comments
Post a Comment