Abstract classes and methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Abstract classes and methods

I understand the basis of abstract classes. I know how to use them and what they do. BUT e.g. I have an Animal, Dog, Cat class. Why can't I just write simply eat methods into the Dog and Cat Class (and Animal class wouldn't include it). Why to make Animal class abstract and inherit eat method from there and override it in subclasses?

4th Aug 2018, 8:48 AM
Adam
Adam - avatar
5 Answers
+ 1
This is because it allows for polymorphism. Let's say somewhere else in your program you implement the concept of pets, and create a Pets class. This class holds an arbitrary number of Animal objects. Now our program tells all the pets to start eating. Inside the Pets class, we can iterate over all the Animal objects, and call the eat method. This is because the eat method is defined in the Animal class. This would not be possible if you implement the eat methods individually in the Cat and Dog classes. The Pets class would have to typehint specifically for Cat and Dog, and if you later create another Animal, say Bird, the Pet class also has to typehint the Bird class. Your Animal class defines a common "interface" for all Animal objects. Classes that want to use Animal objects (like the Pets class) only want to know what an Animal can do (what kind of methods it has). It should not know all the individual Animal subclasses. Hope that makes sense.
4th Aug 2018, 9:32 AM
Jeroen van der Laan
Jeroen van der Laan - avatar
+ 1
Abstract classes use abstract methods primarily if they can not provide a default implementation. It forces its subclasses to implement this method. Not making the method abstract, and providing a default implementation, is also perfectly valid. This makes it optional for subclasses to implement (override) the method. So to answer your question; there is not much difference. In both cases, the eat method is part of the Animal "interface" (which is what we want, allowing polymorphism). Making the eat method abstract in Animal, forces Cat and Dog to provide their own implementation. Implementing eat method in Animal, makes Cat and Dog inherit this implementation, so they are not forced to implement it themselves.
4th Aug 2018, 10:15 AM
Jeroen van der Laan
Jeroen van der Laan - avatar
+ 1
That is entirely correct.
4th Aug 2018, 10:30 AM
Jeroen van der Laan
Jeroen van der Laan - avatar
0
okay so because of polymorphism but if eat method is in Animal and in Dog class(it inherits from Animal class) too but none of them is abstract then what's the difference between this and if eat method was abstract?
4th Aug 2018, 9:59 AM
Adam
Adam - avatar
0
So for example if I have loads of subclasses and I want to make sure that all of them has an eat method I make Animal class absttact and eat method as well and then unless I override eat method in all the subclasses it will cause an error. Am I right?
4th Aug 2018, 10:23 AM
Adam
Adam - avatar