Inheritance for code Reusability
C# implements the concepts of Object Oriented Programming well. There are four major pillars of Object Oriented Programming which are known as Abstraction, Polymorphism, Inheritance and Encapsulation.
In this article we will understand the Inheritance.
What is Inheritance? Inheritance literally means to inherit ancestral property. In computer programming, inheritance refers to the adoption of members of another class by one class.
In OOP language, when the code of one class is to be used by another class, for this the code of the first class can be copied to the other class but by doing so the size of the code will increase, which is an important concept of object-oriented programming. That is, there should be reusability of the code and not copying the code everywhere. In other words, the concept of inheritance has been developed under Object Oriented Programming to increase the reusability of the code.
When a class inherits another class, then the class that inherits another class is called Child class and the class which is inherited is called Parent class.
Synonyms: Parent class is also called as Base class or Super class. Similarly, child class is also expressed as Sub class or Derived class. The words base class and derived class are mainly used in C# whereas the words super class and child class are used in Java.
The colon symbol is used in C# to express the parent child relationship, for example B : A means that the B class inherits the A class, that is, the B class has given the members of A class its own. incorporated within. So B class can consume members of class A. When B class inherits A class then it can use all the members of A class except private member of A class.
class A
{
// Members of A
}
class B : A
{
// Members of A and B except private members of A
}
It is a very similar context or metaphor or metaphor from the common life as the son inherits all the property of his father except the private property of the father like his qualifications, job etc. Therefore, under Object Oriented Programming Language, the concept of inheritance is expressed in parent-child relationship.
Just as the life cycle of the son is based on the life cycle of the father, without the life of the father the life of the son cannot arise, in the same way the life cycle of the child class is based on the life cycle of its base or parent class. Think of it as creating an object of a child class, first calling the base class's constructor and then calling its child's constructor. This thing is also quite logical because child class inherits members of base class so first creation of base class object is necessary, in other words initialization of base class members is necessary so that child class can inherit base class members .
Conditions of Inheritance
Now we will look at the conditions under which a class may or may not inherit another class. For a class to inherit another class, it is necessary that the class which is being inherited must be class inheritable i.e. accessible to other classes. Now the question arises what are the conditions for this.
When a class inherits another class, the most important element for it is the constructor of the parent class, which determines whether that class can be inherited or not and if it can be inherited then what will be the conditions for it. This is necessary because whenever an object of child class is created its constructor call takes place but before that the constructor call of base class takes place. In other words, the constructor of the child class calls the constructor of the base class. Constructor of parent class is possible of many types like Implicit constructor, Explicit constructor. There are also different types of Explicit constructor such as parameterized constructor and parameterless constructor.
Implicit constructor is always public. If the parent class's constructor is an implicit constructor, then any other class can inherit it because then the child class's constructor can call the base class's constructor. For example look at the following code.
Rule 1) A class can inherit another class only if the first class's constructor can call the second class's constructor.
Rule 2) Under Inheritance, it is to be remembered that the child class can consume the members of the base class but the base class cannot use the members of the child class i.e. this relation is one-way.
Rule 3) Under Inheritance it is to be remembered that member fields of base class can be initialized with the help of child class but vice versa is not true. p=c In other words, the reference variable of the parent class can be passed to the reference of the child class.
Rule 4) Under Inheritance, it is to be remembered that if a class is not directly inheriting any class, then in such a case that class inherits the Object class. In other words, the Object class is the topmost super class of each class. So the method members of Object class like Equals, GetHashCode, GetType, ToString methods are inherited by all classes.
Rule 5) There are two types of inheritance under Object Oriented Programming Language-
I) Single Inheritance
II)Multiple Inheritance
It should be noted that the C# language does not support Multiple Inheritance. The C# language supports Single Inheritance. There are different forms of Single Inheritance which are as follows
- Single level Inheritance
- Multi level Inheritance
- Hierarchical Inheritance
In Inheritance, one class inherits from another class and in the same way another class can inherit from third class. Then two or more classes can inherit the third class. Thus there can be a long chain of inheritance between different classes.
Single Inheritance When a class has one and only one parent class or base class, then this type of inheritance or relationship between classes is called Single Inheritance.
Multiple Inheritance When a class has more than one parent class or base class, then this type of inheritance or relationship between classes is called Multiple Inheritance.
Single level Single Inheritance When a class has one and only one parent or base class, and the parent class has one and only one child class and this relationship is of only one level, then this type of inheritance or relationship between classes is called This is called univariate single inheritance.
Multilevel Single Inheritance When a class has one and only one parent or base class, and the parent class has one and only one child class, and the relationship is of more than one level only, then this type of inheritance or classes is The relation is called multi level single inheritance.
Hierarchical Single Inheritance When a class has one and only one parent or base class, and the parent class has more than one child class, this type of inheritance or relationship between classes is called single hierarchical inheritance.
If the explicit constructor of a class is a parameter less constructor and is not private, then that class can be inherited because then the child class's constructor can call the base class's constructor.
Rule 6) Explicit constructor in a class can be inherited if it has a parameterized constructor and is not private. But in such a situation the constructor of the child class calls the constructor of the base class, then it has to pass parameters to the constructor of the base class as well. It is the programmer's responsibility to pass the value of such a parameter. When the parent class's constructor is parameterized then the child class's constructor cannot call it implicitly indirectly and then it is the programmer's responsibility to pass the parameter's value.
Practical example of inheritance: Under the hospital system, doctors, patients and nurses will understand inheritance with the help of entities. Under the database technology, what is called an entity is called a class under the object-oriented programming language. The attribute of an entity is called a field within a class. Just as there is a relationship between entities, in the same way there can be a relationship between classes, which is known as inheritance in Object Oriented Programming Language.
Edited on 5th July 2023
No comments:
Post a Comment