Why Can’t We Instantiate Abstract Classes
ฝัง
- เผยแพร่เมื่อ 22 ม.ค. 2025
- Abstract classes in Python are meant to enforce specific methods into subclasses or concrete classes. In this video, we'll discuss why can't we instantiate an abstract class if it is a class.
Join👇👇
Discord: / discord
#oop #objectorientedprogramming
dang thats actually one of the best explanations ı have ever heard ngl
Thanks
For viewers:
It is possible to Remove the “ABC” base class from the Animal class. This will allow you to instantiate.
By inheriting from the ABC base, you are essentially telling the class that you do not want it to be instantiated as it’s own class & that you’re going to create subclasses from the “blueprint” as said in the video.
Another key point I’d like to add is that it is possible to have the @abstractmethod decorator on any class method (whether or not you inherit from ABC)
This has a similar advantage of subclasses being required to implement those methods. Without instance issues!
this is bad, this way it creates too much confusion, an abstract class should enforce it's methods like other oop languages and not let any class use this decorator
So they are like interfaces in java or protocols in swift?
Great
Thanks for your support
If we have a second abstract class, for example Mammals, is it possible for Mammals to inherit from ABC and Animals in python ?
Yes, Python supports Multiple Inheritance
If abstract classes is for design enforcement then why would anyone create an object of it...
instantiated an object when?
he should create an abstract class named Creature that is implemented by Animal
You wouldn’t - that’s the entire point.
His last example changes the abstract class into a concrete class where it’s no longer just an abstract blueprint, but a proper, fully implemented class.
Ohhh okay... Got it
Great