Abstract class

  • Note

    What is abstract class

    1. Abstract class is a partially implemented class. Some functions are implemented and some are empty body ( only signature)

    2. We cann't create directly object from a abstract class

    3. A abstract class must be extended

    4. A abstract class is created using the keyword "Abstract"

    5. A abstract class must have atleast one abstract method (only signature)

    6. Child class must provide their own logic

    What do you mean abstract

    Abstract means that hide the internal logic and show relavent details (signature of a function)

    Why/when should Use Abstract Classes?

    1. enforce a comman structure to all related classes: abstract class acts like a blueprint/template to keep comman structure to the related classes

    2. code reusability : child classes are extended from abstract class. So we can share parent class functions and properties

    3. Code maintainability : we can modify the functions in a child class without affecting the other child classes

    Use case or example

    1. Use of different PaymentGateways (stripe, razorpay, paypal etc..) in a project

    2. Use of notifications ( EmailNotification, SMSNotification ) in a project

    3. Use of Different databases (mysql, mssql, oracle etc ) in a project

    1. A abstract class has shared properties and shared logics (functions). but in interface, no properties and no shared logics

    2. A abstract class allows single inheritance. But interface allows multiple inheritance