Upcasting
Hello all, I have a java related question related to upcasting. What is the point of upcasting, I dont see how its useful when a subclass' objects already inherit that of the superclass.
35 Replies
⌛ This post has been reserved for your question.
Hey @v! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
When you say upcasting, I assume you specifically mean using the casting syntax i.e.
(SuperType)object?Animal= super class
Dog = subclass
Animal myAnimal = new Dog();
ah so in normal assignment
yeah 😭
there are multiple reasons, I'll start with a simple one
you can reassign the variable to another
Animal
Is that reason clear?Ok
Why would you do that though
Sometimes you want the variable to change at some point - or you want to reassign it in an if statement or in a loop
and using the common supertype can be useful here
Ok so if it's originally a cat object with an animal reference type, but then you make it a dog, won't the reference type still be animal, meaning you'd only be able to access animal methods anyways
and not dog specific methods
yes
that's the point
you can only use the common methods from the superclass but in turn you can use objects from any subclass
you would use it if you logically don't need anything from the subclass
and that also brings me to the second reason: It can make your code more maintainable/easy to change
How so?
How does it make it cleaner
If you use
and then do a lot of things with it, you cannot easily replace it by a cat in your code
but if you do
and you only use
Animal methods, you can easily change it to Cat by just changing the new Dog().
For example, Java provides different List implementations with different performance characteristics. Using List for the variables allows you to easily change which one you want to use if you ever change your mind.I can see from an organization standpoint for how that would be cleaner tbh
Because they're all animals at the end of the day
One question, with this code block here.
Can you explain what's happening here to the already made animal object:
animal = new Dog();
And if you e.g. want to store multiple cats and dogs, you would also use a
List<Animal> or Animal[] as that allows you to store both cats and dogs in the same data structureYeah but wouldn't that work without upcasting implicitly
As they are objects of the subclass belonging to a superclass
This works with
Animal myPet but not with Dog myPetI see
So myPet = new Cat() wouldn't work without upcasting it before the if statement
but for the reason that yes if you have a
Cat variable, you can't put a Dog inside
but for the reason about being able to change your mind in the future: This is typically not that much of a problem within a single method- but when it comes to fields, method parameters and return types, it's more important
If you write a method that accepts an animal, you want it to work with any animal, right?So when you change your mind and want to change the animal to a cat
would you do myPet = new Cat();
assuming it isn't supposed to be specific to cats or dogs
yeah you can do that whenyou like to
so if you do
you could use that method with cats and dogs - and you only need one method so you don't need to duplicate the code
But you can do that without upcasting which is the thing that confuses me
within methods, you typically don't have to use more general variables (unless you are in the situation where you want to use the same variable for objects of different types as explained before) - but using more general variables where it makes sense makes it easier to change your code in the future
and writing the code is generally the easy part - the important part is being able to maintain it in the future
Ok so what you're saying is that when there is a class that inherits from another class, and when you upcast it, it's better that those classes share the same methods
you can use the methods if and only if they are defined in the superclass
Yes ofc
But you're saying that's when upcasting is used
When you dont need to use any individual methods that aren't in the super class
yes - you use
Animal if your code is logically independent on the exact type of animalOk I see
I have finished an entire introductory course on Java, but this is the one thing that has had me stumped
idk why lol
Its starting to make more sense now, I appreciate it
you might also want to check https://stackoverflow.com/q/2279030/10871900
Stack Overflow
Type List vs type ArrayList in Java
(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
I understand that with (1), implementations of the List interface can be swapped. It...
Is it helpful to upcast when declaring objects in bulk?
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.what do you mean with that?
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.I figured this out, thank you for all the help!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@1265852855458332673>.