As already commented, casting does not change the object - it passes a "message" to the compiler that in that context he may consider that object to be of that class.
But at runtime, the method called is that of the object itself - in the above case, the C.imprimir
. If the so-called method did not exist in the superclass (that is, if imprimir
only if defined for classes B and C, the compiler would error - since it would not be able to generate code to call the method imprimir
of an object of Class A.
Now, as for your concern of "not understanding any concept of object orientation": rest assured - you understood O.O. - the "casting" which is something that the Java language borrows from C which is not something very "object-oriented", and, yes, more like a mechanism to allow the compiler to find itself in some cases.
If it were in Python for example, another object-oriented language - in Python there is no "casting" - but you can always call methods in the class itself by explicitly passing the instance - in this case, which you are trying to do would work:
class A:
def i(self):
return "A"
class B(A):
def i(self):
return "B"
b = B()
print(b.i())
print(A.i(b))
Print output B and A - that is, if the method is called in the instance, as is required in Java syntax, the parameter self
, equivalent to this
, is filled in by the language itself - but you have the option to call the method from the class - A.i()
- and in that case have to pass what would be the self
explicitly - then it is possible to call superclass methods in instances of subclasses.
The super
of Java cannot give you a direct reference to the methods of the superclass if used outside the subclasses. It exists within a method, without arguments, so it assumes points directly to the object in the super-class, but if you tried to use super
from within Executa
, he would look for the method imprimir
in some super-class of Executa
, and not of C
. (Again, the super()
de Python lets you explicitly pass the class and instance from which you want to access the attributes of the superclass. In the example above, the call below prints A
)
print(super(B, b).i())
The ideal would be a way to convert your objects to the desired class, and the only clean way to do this is to have an explicit method for conversion, which is called in the subclass and returns an object of the desired class.
I found in Soen a similar question, where the answers, including using Reflection, indicate that it is really not possible to call a super-class method from an instance of a subclass: https://stackoverflow.com/questions/5411434/how-to-call-a-superclass-method-using-java-reflection
The fact of casting does not make the object type
C
becomeA
norB
. He remainsC
. Remember that cast is not conversion, you are not converting types, you are just using polyformism, sinceC
is also a kind ofA
and alsoB
.– user28595
Thanks @Articuno already managed to clarify the ideas here! but to call the superclass method A, should use super ? Thanks in advance
– Luiz Augusto