Dataflow In Inheritance Using C#

Introduction

In this article, we are going to discuss how actually Data flow works when we try to access the elements or resources of a particular Inherited class. has the highest priority to access the particular resource. This article will help a lot to make your base strong. It will clear your concepts about inheritance and you get the knowledge about, how Inheritance works. Before we go deep into this topic I want to give you a quick introduction to Inheritance.

What is Inheritance 

Inheritance is an important and basic concept of Object Oriented Programming Systems which is being used in every programming language which supports OOPS. By using this mechanism we can easily access the properties, data members, and member functions using a single class object. BasicallyItpes.

  1. Single Level Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

But in most languages, these all are not can be used directly. We have to take the help of Interface in such conditions when we need to Implement Multiple or hierarchical or hybrid Inheritance. It’s a big topic in itself currently we are skipping it. Now we are going to explain how Inheritance actually works. Let’s have an Example to understand it. Here I’m using multilevel Inheritance to explain the flow of Inheritance and C# as the programming language to implement the solution of the problem.

Example 

Suppose we have three classes class Red, class Yellow, and class Green. Now Class Red is inherited by class yellow, class Yellow is Inherited by class Green. This condition looks like looks


Figure-Multilevel Inheritance Example

Now if we code this situation in C# then the code will be,

class Test {
    public static void Main() {
        Green _color = new Green();
    }
}
public class Red {
    public Red() {
        Console.WriteLine("I'm Red");
    }
}
class Yellow: Red {
    public Yellow() {
        Console.WriteLine("I'm Yellow");
    }
}
class Green: Yellow {
    public Green() {
        Console.WriteLine("I'm Green");
    }
}

Explanation

In the above code, we have created three classes Red, Green, Yellow, And a driver class Test whose role is to start the flow of the Program. In each class, we have created a default constructor that has a console message that identifies the constructor belongs to which class. I have tried the easiest example to explain this concept, if we run this code the opt-put will be.


Figure-Output

The following output shows the flow of the program. We have created an object of the Green class with the name _color then when we assign the object using the new keyword Green _color = new Green();. We know that the constructor is automatically called when creates an object of that class and assigned it with a new keyword. Now we can see the flow of the program


Figure-Flow of Multilevel Inheritance

Now we can easily understand the flow of the memory in the inheritance. Here we can see that first of all it starts with the driver class in stage 1 and goes to the It parent class and checks for the static constructor if, there is then execute, if not then check for a parent if no parent then execute the current class but in our hierarchy there is a parent class named Green. The same thing with the class Yellow there is a parent class named Red. In the Red class, there is no parent class so it will execute the Red classes and we get the message -I'm Red, I'm Yellow, I'm Green.

This is a flow of normal classes with the non-static constructor. We can say that the flow of the execution starts from the top parent and comes towards child classes, if we are using static constructors then the flow will be just opposite from the current situation before going deep into static constructor flow first, let's have a quick introduction of static keyword and static constructor.

Static Keyword

The static keyword is used to declare a static variable, classes, methods, constructor, and other static data. We can simply use a static keyword before the declaration. We use static keyword when we need to create s single instance and want to share it with all instances. If create any static method we don't need to create an object to call that method we can invoke that method by using class name e and method name. We can also count the no of objects of a particular class by using the static keyword. Static variables are stored in heap. Here we are using a static constructor to demonstrate the concept of the memory flow in Inheritance when we are using static constructors, let's focus on the static constructor.

Static constructor 

static constructors are used to initializing the static variables and static data. Logically we only initialize those variables or properties that are needed to be initialized only once. to perform this initialization we use a static constructor. We can not use access specifier(public, private, protected ) when creating static constructors, if you use then get the following error


Figure-Access specifier error with a static constructor

So never use any access specifier when creating a static constructor. One class can have only one static constructor not more than one so we can not overload any static constructor.

Let's create the same program using a static constructor.

class Test {
    public static void Main() {
        Console.WriteLine("With Static constructor \n \n");
        GreenC __color = new GreenC();
    }
}
public class RedC {
    static RedC() {
        Console.WriteLine("I'm Red -- with static constructor");
    }
}
public class YellowC: RedC {
    static YellowC() {
        Console.WriteLine("I'm Yellow -- with static constructor");
    }
}
public class GreenC: YellowC {
    static GreenC() {
        Console.WriteLine("I'm Green -- with static constructor");
    }
}

Now when we execute the code we will get the following output 


Figure-Output with static constructor 

Now we can see clearly that the flow of the execution starts from button to top when there are static constructors in your class. Now if we compare both outputs we can see the difference

Dataflow In Inheritance Using C# 
Figure-Comparison Non-static  Vs Static constructor

Now we can see that the flow of Inheritance with static constructors is just the opposite of the flow of inheritance with non-static constructors. Static members' priority is high from the normal members that is why they are getting initialized first.

Conclusion

In this article, we have started with a problem and solved that problem with Multilevel Inheritance, and checked the flow of the memory with static and non-static constructors. Now you can easily understand how memory is being initiated when using inheritance. 


Similar Articles