Friday, June 18, 2021

C# Inheritance

What is Inheritance?
It is a mechanism to consuming the members of one class into another by establishing parent-child relationship between them.
Just as child inherits the properties of parent, in OOP child class derives the properties of its parent class. The parent class is called base class and child class is called derived class.

Super = Base = Parent
Sub = Derived = Child

The inheritance provides the merit of reusability.

class A
{
//members of class A
Let class B inherits class A. Colon symbol is used to denote inheritance.
class B : A
{
//members of class A is consumed by class B except private members of parent A
//members of class B

NOTE: Dot operator is used to consume the member of a class.

Parent class constructor must be accessible to Child class, otherwise child class cannot consume its parent class members.
Example
class A
{
private A(){} //private constructor
 

class B : A
{
//Now, members of class A cannot be consumed by B 

Execution of parent class constructor precedes the child class constructor. 
Reason: Parent class must be initialized before the child class so that child can consume the members of its parent class. 
Child class has right on parent class members but not vice versa. If you create an object of parent class, the IntelliSense will never show the members of child class when dot operator is used with parent class object.

As child class has access rights on the members of its parent class, we can create a reference variable of child class and can use dot operator on it to access parent class members.

A reference variable points to an object of class object. A child class reference variable can point to parent class objects as well as its own objects.

A a; // here a is a reference variable of class A and it is uninitialized
B b= new B(); //b is a reference variable of class B and it is initialized
a=b; // now a is referencing to what b is referencing to.
But is the above assignment of one reference variable to another is okay?

Now consider the opposite  case.

A a;
B b= new B();
b=a;

Rule1. Parent class constructor must be accessible to Child class, otherwise child class cannot consume its parent class members.
Rule2. Child class has right on parent class members but not vice versa. 
Rule3. We can instantiate a parent class variable using the instance of its child class.
Rule4. Object class is the parent class of all the classes. So, any class can access the members of Object class. Object class has Equals(), GetHashCode(), GetType(), ToString() methods, so all classes can use these four methods. Object is the root class in C#.

Object is C# type and object is alias type.
String is C# type and string is alias type.
C# types are in Proper case.

Types of Inheritance in C#
  1. Single
  2. Multi-level
  3. Hierarchical

Rule5. Hybrid inheritance is not supported in C#.

Suppose that parent class constructor is parameterized and the child class is derived like below.

class A
{  private int a;
    A(int x){ a=x;}
}
class B: A
{
}

class Program
{
public static void Main(string[] args)
{
    B objB = new B();//What will happen?
}
}
Remark In the above case, the child class B is instantiated. So, class B constructor must be invoked but before that the parent class constructor will be called. Now the parent class constructor is parameterized, it expects a parameter from the invoker. The class B does not pass any argument to class A constructor, exception occurs. To resolve this issue, class B must provide explicit value. We have to modify the above code as shown below.

 class Program
{
public static void Main(string[] args)
{
    B objB = new B():base(2)
}
}
Rule6. Child class calls the parent class constructor implicitly in case the parent class constructor is parameter-less, but if parent class constructor is parameterized, the child class must explicitly pass the value using base(). It is the programmers responsibility to pass the correct number of parameters and correct datatype arguments in the base().
Remember that whatever are the members of child class are not accessible by parent class. This principal idea can help us to differentiate the above rule.

No comments:

Post a Comment

Hot Topics