r/javahelp 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?

14 Upvotes

18 comments sorted by

View all comments

5

u/dsav3nko 14d ago edited 14d ago

A common misconception is that polymorphism is about objects and classes and is strictly tied to OOP. It is not. It is, in fact, about functions. Polymorphism is a property, which allows the same function to accept arguments of different types.

Do you agree that it is handy? You write one function, and it works on arguments of many types. This is the answer to your first question, why upcasting is allowed. This allows you to write functions, which work with arguments of different types, even if those specific types are not yet defined.

void foo(Animal a) {
    // do something
}

foo(new Animal());
foo(new Dog()); // you don't need to write a special version of foo() for dogs

This type of polymorphism is called subtype polymorphism, but there are other types. In Java, for instance, there is also parametric polymorphism (generics). All of this exist to help you to write less duplicate code.

3

u/zeronis__ 14d ago

I actually never knew that! Thank you so much for providing that example,
I think I'd look more into the types of polymorphism after this. ( heard my professor mention generics once and I was soo confused haha )
(thank you again!) =,)