Why should we use the abstract method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why should we use the abstract method?

We can just use simple methods in each class, we are rewriting methods in the Dog and cat anyways. So why should we use the abstract method?

9th Jul 2020, 5:04 PM
BELHANI Mohamed-Afif
BELHANI Mohamed-Afif - avatar
1 Answer
+ 4
It forces the class that extends or implements the class to override and implement it's own version of the method. Often you'll find that there will be methods that must be uniquely implemented in a child class. Say you had a abstract Database class that outlined the common methods that a class used to work with Databases would need. This class has a connect() method that is abstract, because not all Databases are connected to the same and the use of a method implemented from the parent class would cause a catastrophic crash. So, here it is a good thing that the method is abstract as it forces the inheriting class to implement the connect method as needed for that type of Database and avoids the ability to overlook its implementation and using a connect method incorrectly implemented from the parent class.
9th Jul 2020, 5:36 PM
ChaoticDawg
ChaoticDawg - avatar