java basics help
For this example:
I dont understand why we do this:
Animal dog = new Dog();
Animal cat = new Cat();
Why not just do something like, Dog dog = new Dog();, Cat cat = new Cat();
Also why CAN we do this and why doesnt it throw an error, because ive seen it happen that the left hand side of the initialization was sometimes an interface?
One thing that I found online was thit helps with abstraction, like for the method below. But why does it even make a difference at all, since Dog dog = new Dog(); is still an Animal so the method should it accept it too right?
public void handleSound (Animal animal) {
animal.makeSound();
}
13 Replies
⌛
This post has been reserved for your question.
Hey @mjstx! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
you can do both but most of the time its best to just do
and just like in the example at the bottom
that you added
passing an animal instead of a specific class like dog and cat means you can use this method for all animals
Ahh ok, so doing Animal dog = new Dog(); doesnt have any technical benefits, but just makes it clearer for the programmer/reader that dog implements/extends the Animal class. Is that right?
basically yes
however if you do
animal dog
then you only get the methods the animal has
but for example if you got an extra method in the dog class
then you wont be able to call it
because you got an animal as reference
so unless you know your method will receive different types of animal
its best to use that specific type
so dog cat etc
search the strategy design pattern it can explain a lot
Actually it is best to use the type that does the least, so that your program doesn't make any use of members that aren't needed, and stays free to replace one choice of class with another, without any change.
I'm not sure what you think is the strategy pattern
Strategy in Java / Design Patterns
Strategy pattern in Java. Full code example in Java with detailed comments and explanation. Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object.
this here
shows how casting to a different class can give you more methods
if needed
or way different ones
but yes if its a generic method like changing animal name then if your animal class has a name field then theres no need to know what kind of a specific class it is
like dog or cat
.... That example shows no such thing.
Design patterns usually do not cast anytjing in their typical examples.
well if you need a specific method for like a dog or a cat
you need to cast to that
from the animal
in the strategy
but if you have a method for all animals then you dont need casting
....... The point of strategy pattern is you don't need to do that. There would be no pattern if you did that, just ad hoc code.
you do depending on the case
want me to write an example
hi
💤
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.