Dec 9, 2019

INHERITANCE IN C# (sibling class)


c# inheritance, inheritance in c sharp, sibling class
inheritance in c#
    “The mechanism of creating relation between classes by declaring parent and child relationship to consume or to inherited the member of parent class in child class.”
            Usually we build inheritance when the need arise to call class member of any other into our defining class. Then our class will be child and the other will parent. Parent class is also known as super class or base class, and child class also known as sub class.
When we instantiate the child class then we will able to access the parent class due to create of inheritance link between these classes. Once we created inheritance between other class with our define class we are able to access the member of other class in our program.
SYNTAX
            Public class child : parent
Or
            Public class parent
            {
              Members;
            }
            Public class child : parent
            {
              Members;
            }

POINTS TO BUILD INHERITANCE

            1.    For performing inheritance the parent class constructor must be public or default means the parent class constructor should accessible to the child class otherwise inheritance will not possible. If we define the parent class constructor as private then error will show. Because the child class implicitly call the parent class constructor when its instantiated. But the child class constructor needs not to be public at all if not needed.
           2.    Parent class can never access child class member only child class can access its parent class.
           3.    By creating child class instance we can initialize parent class variable.by making child class as reference. When the reference built the parent class consumes the memory of child class.
          4.    If we have only one class in our program and we does not create any inheritance then our class will have a parent class by default that is object class which define in our system namespace.
          5.    In C –Sharp (C#) it don’t support multiple inheritance through classes. Only perform single and multi-level inheritance.
          6.    Referring point 1. If the parent class constructor is parametric then we have to call it explicitly, that means it is the responsibility of the programmer to pass the value by using keyword ‘base’ from the constructor of the child class. Otherwise the child class can’t able to call parent class constructor. Like this
   Public class child (int a) : (base (a))

TYPE OF INHERITANCE

            Actually on the base of C++ we have five type of inheritance which are follow as
                    a.    Single Inheritance
                    b.    Multi-level Inheritance
                    c.    Hierarchical Inheritance
                    d.    Hybrid Inheritance
                    e.    Multiple Inheritance
But we only discussed about single, multi-level and multiply inheritance in C#.

SINGLE INHERITANCE

            The mechanism of creating  only one immediate parent class and it has only one parent and one child that perform inheritance mechanism is called single inheritance.
c# inheritance, inheritance in c sharp, inheritance in c#
c# single inheritance

SYNTAX
Public class parent
            {
              Members;
            }
            Public class child : parent
            {
              Members;
            }

MULTI-LEVEL INHERITANCE

            The second type of inheritance in which we create one immediate parent class but it may consist of more than one parent and one child class.
c# inheritance, multilevel inheritance, inheritance
c# multilevel inheritance
SYNTAX
Public class parent
            {
              Members;
            }
            Public class child 1 : parent
            {
              Members;
            }
Public class child 2 : child 1
            {
              Members;
            }

MULTIPLE INHERITANCES

            The type of inheritance that performing by creating one child having multiple parents means more than one immediate parent class to child.
Normally it is not allowed in c# but we can do it by adding additional keywords by using interfaces. Referring point 6.
inheritance in c sharp, multiple inheritance, c# class inheritance,
c# multiple inheritance


SYNTAX
Public class parent 1
            {
              Members;
            }
            Public class parent 2
            {
              Members;
            }
Public class child  : parent1 : parent 2
            {        
              Members;
            }

SIBLING CLASS

“If two or more than two classes have same parent class then the classes will be sibling class”. Like brother sister in real that have same parents.
sibling class, c# inheritance, c# derived class
C# sibling class
SYNTAX
Public class parent
            {
              Members;
            }
            Public class child 1 : parent
            {
              Members;
            }
Public class child 2 : parent
            {
              Members;
            }

see examples:  GO

0 comments:

Post a Comment

Please do not enter any spam link in the comment box