java basics help

For this example:
public class Animal {
public void makeSound (){
System.out.println("animal sounds");
}
}

public class Cat extends Animal{
public void makeSound (){
System.out.println("meow");
}
}

public class Dog extends Animal{
public void makeSound (){
System.out.println("woof");
}
}

public class test {
Animal dog = new Dog();
Animal cat = new Cat();
}
public class Animal {
public void makeSound (){
System.out.println("animal sounds");
}
}

public class Cat extends Animal{
public void makeSound (){
System.out.println("meow");
}
}

public class Dog extends Animal{
public void makeSound (){
System.out.println("woof");
}
}

public class test {
Animal dog = new Dog();
Animal cat = new Cat();
}
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
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @mjstx! 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.
ayylmao123xdd
ayylmao123xdd2w ago
you can do both but most of the time its best to just do
Dog dog = new Dog()
Dog dog = new Dog()
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
mjstx
mjstxOP2w ago
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?
ayylmao123xdd
ayylmao123xdd2w ago
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
Kyo-chan
Kyo-chan2w ago
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
ayylmao123xdd
ayylmao123xdd2w ago
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.
ayylmao123xdd
ayylmao123xdd2w ago
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
Kyo-chan
Kyo-chan2w ago
.... That example shows no such thing. Design patterns usually do not cast anytjing in their typical examples.
ayylmao123xdd
ayylmao123xdd2w ago
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
Kyo-chan
Kyo-chan2w ago
....... 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.
ayylmao123xdd
ayylmao123xdd2w ago
you do depending on the case want me to write an example
Divyanshu
Divyanshu2w ago
hi
JavaBot
JavaBot2w 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.
Want results from more Discord servers?
Add your server