getting child class from static method of parent class in Java

WeinTaube

Suppose I have these classes:

public class ChildClass extends ParentClass
{
    // some class definition here
}


public abstract class ParentClass
{
    public static void printClass()
    {
        // get the class that extends this one (and for example, print it)
    }

    // some class definition here
}

Lets say when calling ParentClass.printClass() I want to print the name of the class (like doing System.out.println(ParentClass.class)). When then extending ParentClass (for example like in ChildClass) and calling ChildClass.printClass(), I want it to print the name of the extending class (like doing System.out.println(ChildClass.class)). Is this somehow possible?

I've found a way to get the class from inside a static method by using MethodHandles.lookup().lookupClass(), but when using it inside of ParentClass.printClass and extending ParentClass, then calling printClass on the extending Class, I always get the class of ParentClass.

rzwitserloot

static methods are best thought of as living entirely outside of the class itself. The reason they do show up in classes is because of the design of java (the language) itself: Types aren't just types with a hierarchy, they also serve as the primary vehicle for java's namespacing system.

Types live in packages, packages are the top level namespace concept for types. So how do you refer to a method? There's only one way: Via the type system. Hence, static methods do have to be placed inside a type. But that's about where it ends.

They do not inherit, at all. When you write:

ChildClass.lookupClass()

The compiler just figures out: Right, well, you are clearly referring to the lookupClass() method in ParentClass so that is what I will compile. You can see this in action yourself by running javap -c -p MyExample. The same principle applies to non-static methods, even.

For instance methods, the runtime undoes this maneuvre: Whenever you invoke a method on any object, the runtime system will always perform dynamic dispatch; you can't opt out of this. You may write:

AbstractList<String> list = new ArrayList<String>();
list.sort(someComparator);

and you can use javap to verify that this will end up writing into the class file that the method AbstractList::sort is invoked. But, at runtime the JVM will always check what list is actually pointing at - it's an instance of ArrayList, not AbstractList (that's obvious: AbstractList is abstract; no object can ever be directly instantiated as `new AbstractList). If ArrayList has its own take on the sort method, then that will be called.

The key takeaway of all that is: Static methods do not inherit, therefore, this dynamic dispatch system is not available to them, therefore, what you want cannot be done in that fashion.

So what to do?

It feels like what you're doing is attempting to associate a hierarchy to properties that apply to the class itself. In other words, that you want there to be a hierarchical relationship between the notion of 'ParentClass's lookupClass method and ChildClass's lookupClass method - lookupClass is not a thing you ask an instance of ChildClass or ParentClass - you ask it at the notion of the these types themselves.

If you think about it for a moment, constructors are the same way. You don't 'ask' an instance of ArrayList for a new arraylist. You ask ArrayList, the concept. Both 'do not really do' inheritance and cannot be abstracted into a type hierarchy.

This is where factory classes come in.

Factory classes as a concept are just 'hierarchicalizing' staticness, by removing static from it: Create a sibling type to your class hierarchy (ParentClassFactory for example):

abstract class ParentClassFactory {
  abstract ParentClass create();
  abstract void printClass();
}

and then, in tandem with writing ChildClass, you also write ChildClassFactory. Generally factories have just one instance - you may want to employ the singleton pattern for this. Now you can do it just fine:

class ChildClassFactory extends ParentClassFactory {
  private static final ChildClassFactory INSTANCE = new ChildClassFactory();
  public static ChildClassFactory instance() { return INSTANCE; }
  public ParentClass create() { return new ChildClass(); }
  public void printClass() { System.out.println(ChildClass.class); }
}

// elsewhere:

// actually gets the ChildClassFactory singleton:
ParentClassFactory factory = ....;
factory.printClass(); // will print ChildClass!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Getting the class name from a static method in Java

From Java

Java : call child class method from parent class using interface

From Dev

Java - parent class is calling a method from a child class?

From Dev

Is it possible to invoke Parent class method from Child class object in Java?

From Dev

Python get child class inside parent class static/class method

From Dev

Executing a method from a parent class in a child class

From Dev

How to access parent class instance method from child class static method in python?

From Dev

es6 access static method of child class from static method of parent?

From Dev

Calling constructor of parent class in static method of child class

From Dev

Access to property defined in child class from static method in the parent class - javascript

From Java

Getting the class name from a generic static method in Java

From Dev

Typescript: Override static factory method of parent Class in Child method

From Dev

How Call child method from parent class

From Dev

Calling child method from obj of parent class

From Dev

Child Method not recognised from Parent class TypeScript

From Dev

Call parent class method from child in ReactJS

From Dev

Java: Use Static methods of Parent Class in Child Class

From Dev

Call Child class method from Parent class method

From Dev

How to create a child class in TypeScript using parent static method?

From Dev

What child-class caused static parent method? Or mission impossible?

From Dev

How to call parent static method using child class in kotlin?

From Dev

Instantiating child class from a static method in base class, using TypeScript

From Dev

Return a new instance of Child class from base class static method

From Java

Throws Nullpointer exception when calls parent class method from child class In JAVA

From Java

is there any way to call parent class method from child class object in java without modifying methods

From Java

Call a child class method from a parent class object

From Java

How to call a Parent Class's method from Child Class in Python?

From Dev

How to access a child class method from a parent class?

From Dev

Get variable from parent class for use in method of child class

Related Related

  1. 1

    Getting the class name from a static method in Java

  2. 2

    Java : call child class method from parent class using interface

  3. 3

    Java - parent class is calling a method from a child class?

  4. 4

    Is it possible to invoke Parent class method from Child class object in Java?

  5. 5

    Python get child class inside parent class static/class method

  6. 6

    Executing a method from a parent class in a child class

  7. 7

    How to access parent class instance method from child class static method in python?

  8. 8

    es6 access static method of child class from static method of parent?

  9. 9

    Calling constructor of parent class in static method of child class

  10. 10

    Access to property defined in child class from static method in the parent class - javascript

  11. 11

    Getting the class name from a generic static method in Java

  12. 12

    Typescript: Override static factory method of parent Class in Child method

  13. 13

    How Call child method from parent class

  14. 14

    Calling child method from obj of parent class

  15. 15

    Child Method not recognised from Parent class TypeScript

  16. 16

    Call parent class method from child in ReactJS

  17. 17

    Java: Use Static methods of Parent Class in Child Class

  18. 18

    Call Child class method from Parent class method

  19. 19

    How to create a child class in TypeScript using parent static method?

  20. 20

    What child-class caused static parent method? Or mission impossible?

  21. 21

    How to call parent static method using child class in kotlin?

  22. 22

    Instantiating child class from a static method in base class, using TypeScript

  23. 23

    Return a new instance of Child class from base class static method

  24. 24

    Throws Nullpointer exception when calls parent class method from child class In JAVA

  25. 25

    is there any way to call parent class method from child class object in java without modifying methods

  26. 26

    Call a child class method from a parent class object

  27. 27

    How to call a Parent Class's method from Child Class in Python?

  28. 28

    How to access a child class method from a parent class?

  29. 29

    Get variable from parent class for use in method of child class

HotTag

Archive