Saturday, 6 July 2024

Abstract Class

 

Abstract Class: -

Abstract classes are closely related to interfaces they are classes that cannot be instantiated and are frequently are partially implemented or not implemented. One key difference between abstract classes and interfaces is that A class may implemented no of interfaces but May Inherit from only One abstract class.

v  A class that implement abstract class may still implement Interfaces.

v  Abstract class consist of CM(concrete methods)&AM(abstract methods).

v  Abstract class can only be used as base class.

v  Abstract classes declared with abstract keyword.

v  A non abstract class derived from abstract class must provide implementation for all inherited abstract members.

v  An abstract class cannot be sealed.

v  By default abstract members are virtual in nature

v  We cannot create object for abstract class but we can create reference for abstract class.

v  * Virtual methods may are may not override but abstract methods must override in derived class.

v  * Abstract class can have zero or more abstract methods.

v  *If the class does not wish to provide implementation for all the abstract members inherited from the abstract class then that class has to be marked as abstract.

 

 






When to use Abstract Class : When multiple classes required common functionality and code reusability and required to  enforce a contract, and provide a structure for derived classes then abstract is better.

Example : bankaccount is abstract class and having PrintAccount concreate method, Interest as abstract method ,Savings Account,and current Account as  child classes, both classes inherit BankAccount abstract class  and both can use PrintAccount method without any change, and implement Interest method in child class with different interest rates.

No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.