How to get the parent base class object super.getClass()
getClass().getSuperclass()
But don't use this. It is certainly a sign of bad design.
In Java super.getClass() prints Child not Parent - why is that?
A method call using super
just ignores any overrides in the current class. For example:
class Parent {
@Override public String toString() {
return "Parent";
}
}
class Child extends Parent {
@Override public String toString() {
return "Child";
}
public void callToString() {
System.out.println(toString()); // "Child"
System.out.println(super.toString()); // "Parent"
}
}
In the case of a call to getClass()
, that's a method which returns the class it's called on, and can't be overridden - so while I can see why you'd possibly expect it to return Parent.class
, it's still using the same implementation as normal, returning Child
. (If you actually want the parent class, you should look at the Class
API.)
This is often used as part of an override, in fact. For example:
@Override public void validate() {
// Allow the parent class to validate first...
super.validate();
// ... then perform child-specific validation
if (someChildField == 0) {
throw new SomeValidationException("...");
}
}
why super.getClass() in a subclass returns subclass name
getClass().getSuperclass()
should do.
Use keyword this/super in base class gives subclass name
The confusion is probably coming from the getClass()
method, which returns the runtime class of the object, which means you'll get back the class you instantiated, not a parent class, even if the method call takes place in a parent class.
I'd recommend hard-coding a string "Dolphin"
or "SeaCreature"
, and using those, which will probably help you visualize the calls better.
https://beginnersbook.com/2013/03/polymorphism-in-java/
Getting the name of a sub-class from within a super-class
Not possible. Static methods are not runtime polymorphic in any way. It's absolutely impossible to distinguish these cases:
System.out.println(Entity.getClass());
System.out.println(User.getClass());
They compile to the same byte code (assuming that the method is defined in Entity
).
Besides, how would you call this method in a way where it would make sense for it to be polymorphic?
How to get the parents of a Python class?
Use the following attribute:
cls.__bases__
From the docs:
The tuple of base classes of a class
object.
Example:
>>> str.__bases__
(<type 'basestring'>,)
Another example:
>>> class A(object):
... pass
...
>>> class B(object):
... pass
...
>>> class C(A, B):
... pass
...
>>> C.__bases__
(<class '__main__.A'>, <class '__main__.B'>)
Get parent class name?
From the documentation: https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy
Class objects have a __name__
attribute. It might cleaner to introspect the base class(es) through the __bases__
attr of the derived class (if the code is to live in the derived class for example).
>>> class Base(object):
... pass
...
>>> class Derived(Base):
... def print_base(self):
... for base in self.__class__.__bases__:
... print base.__name__
...
>>> foo = Derived()
>>> foo.print_base()
Base
Related Topics
Stream Filter of 1 List Based on Another List
Mockito: Mock Private Field Initialization
How to Calculate the Elapsed Time of an Event in Java
Java Native Method Source Code
JSON Parameter in Spring MVC Controller
Differencebetween Reader and Inputstream
Using Javafx.Beans Properties in Model Classes
How Can a Keylistener Detect Key Combinations (E.G., Alt + 1 + 1)
Using Spring Resttemplate in Generic Method with Generic Parameter
How to Ignore Pkix Path Building Failed: Sun.Security.Provider.Certpath.Suncertpathbuilderexception
How to Include Test Classes into Maven Jar and Execute Them
How to Load Files into My Java Application
Where Are Generic Types Stored in Java Class Files