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
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @v! Please use /close or the Close Post button 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st4mo ago
When you say upcasting, I assume you specifically mean using the casting syntax i.e. (SuperType)object?
valer
valerOP4mo ago
Animal= super class Dog = subclass Animal myAnimal = new Dog();
dan1st
dan1st4mo ago
ah so in normal assignment
valer
valerOP4mo ago
yeah 😭
dan1st
dan1st4mo ago
there are multiple reasons, I'll start with a simple one
Animal animal = new Cat();
//...
animal = new Dog();
Animal animal = new Cat();
//...
animal = new Dog();
you can reassign the variable to another Animal Is that reason clear?
valer
valerOP4mo ago
Ok Why would you do that though
dan1st
dan1st4mo ago
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
valer
valerOP4mo ago
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
dan1st
dan1st4mo ago
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
valer
valerOP4mo ago
How so? How does it make it cleaner
dan1st
dan1st4mo ago
If you use
Dog myPet = new Dog();
Dog myPet = new Dog();
and then do a lot of things with it, you cannot easily replace it by a cat in your code but if you do
Animal myPet = new Dog();
Animal myPet = new Dog();
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.
valer
valerOP4mo ago
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();
dan1st
dan1st4mo ago
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 structure
valer
valerOP4mo ago
Yeah but wouldn't that work without upcasting implicitly As they are objects of the subclass belonging to a superclass
dan1st
dan1st4mo ago
Animal myPet;//declare a variable that can be used to store animals
myPet = new Dog();//set it to a dog
if(catsAreCute){
myPet = new Cat();//reassign myPet to a new cat
// this changes the content of the variable to a cat object
}
Animal myPet;//declare a variable that can be used to store animals
myPet = new Dog();//set it to a dog
if(catsAreCute){
myPet = new Cat();//reassign myPet to a new cat
// this changes the content of the variable to a cat object
}
This works with Animal myPet but not with Dog myPet
valer
valerOP4mo ago
I see So myPet = new Cat() wouldn't work without upcasting it before the if statement
dan1st
dan1st4mo ago
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?
valer
valerOP4mo ago
So when you change your mind and want to change the animal to a cat would you do myPet = new Cat();
dan1st
dan1st4mo ago
assuming it isn't supposed to be specific to cats or dogs yeah you can do that whenyou like to so if you do
public void feed(Animal animal){
getFood(animal.getFavouriteFood());
putFoodInPlace();
showFoodTo(animal);
}
public void feed(Animal animal){
getFood(animal.getFavouriteFood());
putFoodInPlace();
showFoodTo(animal);
}
you could use that method with cats and dogs - and you only need one method so you don't need to duplicate the code
valer
valerOP4mo ago
But you can do that without upcasting which is the thing that confuses me
dan1st
dan1st4mo ago
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
valer
valerOP4mo ago
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
dan1st
dan1st4mo ago
you can use the methods if and only if they are defined in the superclass
valer
valerOP4mo ago
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
dan1st
dan1st4mo ago
yes - you use Animal if your code is logically independent on the exact type of animal
valer
valerOP4mo ago
Ok 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
dan1st
dan1st4mo ago
Stack Overflow
Type List vs type ArrayList in Java
(1) List&lt;?&gt; myList = new ArrayList&lt;?&gt;(); (2) ArrayList&lt;?&gt; myList = new ArrayList&lt;?&gt;(); I understand that with (1), implementations of the List interface can be swapped. It...
valer
valerOP4mo ago
Is it helpful to upcast when declaring objects in bulk?
JavaBot
JavaBot4mo ago
💤 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.
dan1st
dan1st4mo ago
what do you mean with that?
JavaBot
JavaBot4mo ago
💤 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.
valer
valerOP4mo ago
I figured this out, thank you for all the help!
JavaBot
JavaBot4mo ago
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.
JavaBot
JavaBot4mo ago
Post Closed
This post has been closed by <@1265852855458332673>.

Did you find this page helpful?