Purpose of Abstract classes? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Purpose of Abstract classes?

I get that an abstract class is basically an outline for the other subclasses need to have, such as methods, instance variables, and so on. However, can't we just remove the abstract class and declare the methods directly in the subclasses. It just seems like extra work to me to have an abstract class that does nothing, and when you are setting up the methods and variables in the subclasses anyway. Thanks in advance for the help!

11th Feb 2017, 1:58 AM
Sathvik Kuthuru
Sathvik Kuthuru - avatar
5 Réponses
+ 3
consider 50 classes with same methods, you would have to write every method over and over again. Isin't it just better to write it once and then extend base class? Additionally it helps in Polymorphism (read about it it's really important concept of OOP).
11th Feb 2017, 2:16 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 2
@Sathvik No, you don't. If it was Interface you would have to, but not in Abstract Class :) Edit: Didn't see "abstract method" part. Abstract method indeed have to be overriden in sub classes, but most of the time you won't need them since you've Interfaces where every method is abstract by default ^^
11th Feb 2017, 1:08 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
Oh thanks. But correct me if i'm wrong, if you create an abstract method in an abstract superclass, don't you have to redefine that method anyway in the sub class?
11th Feb 2017, 12:35 PM
Sathvik Kuthuru
Sathvik Kuthuru - avatar
0
Ok, now it makes sense, thanks @Jakub.
11th Feb 2017, 1:15 PM
Sathvik Kuthuru
Sathvik Kuthuru - avatar
0
If I understand it well, I can't create any instance from the abstract class only other classes can inherit the methods and variables. Practically speaking this is an outline.
11th Feb 2017, 3:23 PM
Zolt√°n Bolod√°r
Zolt√°n Bolod√°r - avatar