r/javahelp • u/Nobody37373 • Oct 08 '25
Unsolved Why learn Upcasting/Downcasting?
After days of getting stuck in this concept, i finally feel like giving up and never looking at it back again. After countless hours of Googling, asking assistance from AI, watching YouTube videos, I am now falling into a guilt of why I am even wasting time over a single concept. I feel I should move on at this point. Before this one topic, one google search used to clear all my doubts so effortlessly guys.
But this one seems like a tough nut to crack. Can anyone help me out on this?
I know the 'how' and 'what', but I am not reaching anywhere near to the 'why' of this one concept.
View all comments
Show parent comment
1
u/Nebu Writes Java Compilers Oct 09 '25 edited Oct 09 '25
Scenario 1:
So let's say I'm prototyping a new app, so I write a line like:
Then, I want to retrieve some data from the database. So on the next line, I type the following in my IDE:
and my cursor is after the period. The IDE will pop up all the methods that are defined on the type of the variable. The type of the variable is
MySQL, and so it shows all theMySQLmethods (in addition to the genericDatabase) methods. I pick one of them, and now my code looks like this:A week later, I find out that MySQL is not good for my project. So I edit the code where I initialize the variable:
Now my code is littered with compile errors because I've been using MySQL methods all over my code base. It's basically gonna be a full rewrite of the entire app.
Scenario 2:
So let's say I'm prototyping a new app, so I write a line like:
Then, I want to retrieve some data from the database. So on the next line, I type the following in my IDE:
and my cursor is after the period. The IDE will pop up all the methods that are defined on the type of the variable. The type of the variable is
Database, and so it only shows me the methods that exist on all implementations ofDatabase, and it does not show me anything specific to MySQL, nor to CouchDB, nor do DynamoDB, etc. I pick one of them, and now my code looks like this:A week later, I find out that MySQL is not good for my project. So I edit the code where I initialize the variable:
Everything still works flawlessly, because I never used any functionality that was specific to MySQL.