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

ความคิดเห็น • 15

  • @compositeboson123
    @compositeboson123 16 วันที่ผ่านมา +9

    dang thats actually one of the best explanations ı have ever heard ngl

    • @2MinutesPy
      @2MinutesPy  16 วันที่ผ่านมา

      Thanks

  • @alexgraham108
    @alexgraham108 16 วันที่ผ่านมา +4

    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!

    • @rida_brahim
      @rida_brahim 16 วันที่ผ่านมา

      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

  • @byaruhaf
    @byaruhaf 15 วันที่ผ่านมา +1

    So they are like interfaces in java or protocols in swift?

  • @MaxSamoha
    @MaxSamoha 16 วันที่ผ่านมา +2

    Great

    • @2MinutesPy
      @2MinutesPy  16 วันที่ผ่านมา

      Thanks for your support

  • @Mindingsesssion
    @Mindingsesssion 13 วันที่ผ่านมา

    If we have a second abstract class, for example Mammals, is it possible for Mammals to inherit from ABC and Animals in python ?

    • @SaiponathGames
      @SaiponathGames 12 วันที่ผ่านมา

      Yes, Python supports Multiple Inheritance

  • @dweepverma3662
    @dweepverma3662 16 วันที่ผ่านมา +2

    If abstract classes is for design enforcement then why would anyone create an object of it...

    • @notpowder5831
      @notpowder5831 15 วันที่ผ่านมา

      instantiated an object when?

    • @notpowder5831
      @notpowder5831 15 วันที่ผ่านมา

      he should create an abstract class named Creature that is implemented by Animal

    • @Jbombjohnson
      @Jbombjohnson 15 วันที่ผ่านมา

      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.

  • @nottomention-h8w
    @nottomention-h8w 16 วันที่ผ่านมา +2

    Ohhh okay... Got it