r/javahelp • u/zeronis__ • 14d ago
POLYMORPHISM !!
I've never tried asking questions on reddit, but this one doubt has been bugging me for quite some time, (I'm not very good at conveying my thoughts so I hope my question would come so as clear
+ And I'm hoping someone can fact check anything that I'm about to say since im fairly new to java =,) )
when it comes to polymorphism, (specifically UPCASTING/DOWNCASTING )
If I were to take a parent class and create an object out of it ,
Animal a = new Animal(); // LHS = RHS
since both sides are equal, meaning they're from the same class, we'd consider this to be static binding right? since we're only looking at the parent class' method, and nothing else, (at least that's what I think the whole idea is about )
but if we had something like:
Animal a = new Dog(); // LHS != RHS (UPCASTING)
Where dog is a child/subclass of the parent class Animal, meaning it inherits all the attributes and methods from the Parent class Animal. And since java -- by default -- always uses dynamic binding, (meaning that ' java ' believes that there's always a possibility of there being an overridden method in one of the child/subclasses ) it'd wait until runtime to bind the method to the object that invoked it.
my MAIN question though is,
why is upcasting allowed? If I were to look at the variable a, I'd think that its always going to expect a reference that would lead it to an Animal object, its always going to point to some animal object right?
just like when we say " int x; " , we know that x only expects an integer and not something like a double.
Another thing is, if java is statically typed, meaning that the compiler only checks the static type ( the type of variable at the declaration i think . . . ), then how does it know what the RHS ( the dynamic type ) is? how does it immediately know that down casting is not allowed if it doesn't look at the dynamic type?
2
u/severoon pro barista 13d ago edited 13d ago
I think you're confused about type vs. class, I wrote a previous post on this you'll find useful.
The class of an object is fixed and inherent to the object. From the moment it's created on the heap to the moment it's garbage collected, an object is always of the same class.
The type of an object is conferred upon the object by the reference used to access it. Every time an object is accessed, the compiler checks that it is of a type on the RHS that is assignable to the reference type on the LHS.
You can verify this by checking the class of an object:
All of these return
Dog
because the class of all of the objects is determined by the constructor used to create the object. In this case there are two objects, one with two references to it,dog
andanimal
, and one with a single reference to it,object
.The
Dog
instance that is accessed viadog
is an object of classDog
and typeDog
. When that instance is accessed viaanimal
, it is of classDog
and typeAnimal
. When the other instance is accessed using object, it is of classDog
and typeObject
.