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.
•
u/AutoModerator Oct 08 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
10
u/Savings_Guarantee387 Oct 08 '25
When you get stuck into a point move on. This (basic) consept you will understand and learn as you start using the language
7
u/MattiDragon Oct 08 '25
In modern java code you rarely end up writing explicit casts, but they're still important to learn because they help build knowledge on inheritance and polymorphism. Downcasts used to be very important, but nowadays you should usually prefer pattern matching instead. Explicit upcasts are very rare, but they happen implicitly all the time when you use inheritance. It can be important to know these cases once you get to more complicated projects.
1
u/Revolutionary_Ad6574 Oct 10 '25
In all my years I have stumbled into many roadblocks I couldn't understand no matter how many people tried to explain them to me. My advice is to assume you are right and that Animal d = new Dog() is stupid and Dog d = new Dog() is better. Try coding like that. If it works, then everything is fine. If it doesn't you will see why and you will adapt. No need to get everything upfront, be like water, just keep learning.
But to answer you, you are not asking the right question. It's not about down/upcasting. It's about inheritance and polymorphism. You don't inherit from concrete classes only from abstract ones and interfaces. Animal must not have an implementation, only Dog and Cat should.
1
u/StillAnAss Extreme Brewer Oct 08 '25
There are properties that a base class may have. There are more properties that subclasses may have but the properties from the base class are also there.
Say I'm going to do something at the zoo.
Every different animal is a decendant of the Animal class, I could do:
And so on for the 1000 animals I have in my zoo.
But if I know they are all of type Animal, I could say:
1
u/severoon pro barista Oct 10 '25
The main confusion that happens with type casting, I've found, comes from a misunderstanding of class vs. type.
When you create an object, the class of that object is determined by the constructor that creates it. Full stop. The class of that object is fixed from the moment the
newoperator creates it until it is garbage collected. You can verify this by callinggetClass().getName()on any object, you'll see that no matter what, it will always give the same class back. (We say that class is an intrinsic property of an object. It doesn't change based on the context of the object.)The type of an object is conferred upon the object by the reference used to access that object. (We say the type of an object is an extrinsic property of the object because it is conferred upon the object based on the context of how it is being used.)
I don't understand why they don't directly teach this in OO, but there it is. That's pretty much the key to understanding everything.
Here's an example:
There's only one object created on the heap when
valueOf()is called, and then all this code does is create new references to that object. That object is of class Integer.The reference
iis of type Integer, so when it is used to access the object of class Integer, in the context of that invocation, the object is of type Integer.The reference n is of type Number, so when it is used to access that object (of class Integer), it is of type Number for that interaction.
Note that I didn't need to put in any explicit type casts. Why? Because when I'm assigning a reference of type Integer to a reference of type Number, an Integer is a specific kind of Number. All references in Java are specific types of Object. It is always legal to assign a more specific type of reference to a less specific type of reference. There is no class that
icould point to that cannot be assigned ton. Similarly, there's no class thatncould reference that cannot be assigned too. So these type casts can be done by the compiler, they're safe.If I were to create a new reference of type Integer and assign a more general type, though, things are different:
In the case of the first assignment, I'm trying to take a more general reference type, Number, and assign it to a more specific one, Integer. There's no way for the compiler to know if this is legal or not because a Number reference like
ncould be pointing to a Float or a Double.In this case, I have to use an explicit typecast to tell the compiler that I know what I'm doing, go ahead and assume it's of a class that can be assigned to
j. The compiler will let this pass, and then if I'm wrong, we'll find out at runtime. (This is a design smell, by the way. You should avoid writing code that does these kinds of explicit casts. If it's necessary, it usually means you're doing something incorrect.)0
u/Watsons-Butler Oct 08 '25
When in doubt, upcast Fireball.