*■ Inheritance*
• Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class.
• A class that inherits from another class is called a derived class (or subclass)
• The class which gets inherited by another class is called the base class (or superclass).
• Inheritance is possible only if there is is-a relationship between parent and child class.
• constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.
____________________________________________
*➤ Rules of Inheritance*
1) C# supports single inheritance, meaning a class can inherit from only one base class.
2) A parent class constructor must be accessible in child class otherwise
inheritance will be not possible.
3) every class, whether user-defined or predefined implicitly derives from the 'Object' class, which is defined in the 'System namespace'
4) constructors are not inherited.
If a base class has a parameterized constructor, the derived class must call this constructor explicitly.
5) C# supports multiple inheritance of interfaces, meaning a class can implement multiple interfaces.
6) A derived class can access members of its base class but base class member cannot access member of derived class.
7) Static members of the base class are not inherited by derived classes but can be accessed using the base class name.
____________________________________________
*➤ Access Modifiers in Inheritance*
1) *public* : Members are accessible from any code.
2) *protected* : Members are accessible within the same class and derived classes.
3) *private* : Members are accessible only within the same class.
4) *internal* : Members are accessible within the same assembly.
5) *protected internal* : Members are accessible within the same assembly and derived classes(whether same or different assembly).
____________________________________________
*➤ sealed class and methods*
➔ A class marked with the 'sealed' keyword cannot be inherited.
public sealed class ClassName { }
➔ A method marked with sealed in a derived class cannot be overridden further
public sealed override void MethodName() { }
____________________________________________
*➤ Types of Inheritance*
1) *Single Inheritance*
• A class (derived class) inherits from a single base class
2) *Multilevel Inheritance*
• A class is derived from another derived class, forming a chain of inheritance.
• Each class inherits from its immediate base class, creating a multi-level hierarchy.
3) *Hierarchical Inheritance*
• A single base class is inherited by multiple derived classes.
*C# does not support multiple inheritance & hybrid inheritance of classes, a class can implement multiple interfaces.*
____________________________________________
➤ Advantages
1. Code reusability
2. Less time
3. Reduce memory wastage
Comments
Post a Comment