Inheritance
Acquiring the properties of an existing class into a new class by establishing a parent–child relationship between classes is known as Inheritance.
The big advantage of inheritance is reusability of code:
Syntax
[<Modifier>] Class <CC Name> : <PC Name>
Where
CC: child classs
PC: Parent class.
For example:
- Class class1
- {
- --members
- }
- Class class2:class1
- {
- Consume member of parent
- }
Note: private members of a parent class can never be consumed by child classes.
For example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace InheritanceDemo
- {
- class Parent
- {
- public void ParentMethod()
- {
- Console.WriteLine("this is parent method");
- }
- }
- class child :Parent
- {
- public void childmethod()
- {
- Console.WriteLine("this is child method");
- }
- static void Main(string[] args)
- {
- child obj = new child();
- obj.childmethod();
- obj.ParentMethod();
- Console.ReadKey();
- }
- }
- }
The following are the rules and regulations that must be adopted when working with inheritance.
Rule 1: if inherited, the execution of any child class starts by invoking its parent class's default constructor by default.
So the constructor of the parent class must be accessible by the child class or else inheritance will not be possible.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace InheritanceDemo
- {
- class Parent
- {
- public Parent()
- {
- Console.WriteLine("Parent Constructor");
- }
- public void ParentMethod()
- {
- Console.WriteLine("this is parent method");
- }
- }
- class child :Parent
- {
- public child()
- {
- Console.WriteLine("this is child construcotor");
- }
-
- public void childmethod()
- {
- Console.WriteLine("this is child method");
- }
- static void Main(string[] args)
- {
- child obj = new child();
- obj.childmethod();
- obj.ParentMethod();
- Console.ReadKey();
- }
- }
- }
Rule 2: In inheritance the child class can access its parent class's members but a parent class can never access its child class member.
To test this rewrite the code under the main metod of class2 as follows.
- static void Main(string[] args)
- {
- Parent p=new Parent ();
- p.ParentMethod ();
- p.childmethod();
- }
Rule 3: In the same way an object of a class can also be assigned to its parent class variable and make it as a reference, but in this scenario we are also using the parent reference. We cannot access only a member that is purely defind in the child class even if the reference is consuming the same object memory.
To test the rewrite of the code under the child class of the main method is as follows:
- static void Main(string[] args)
- {
- child obj = new child();
- Parent p=obj;
- p.ParentMethod();
- p.childmethod();
- }
Note: a reference of the parent class created using an object of the child class, if required, can be converted back into child class references again but there is no need to do an explicit conversion.
- static void Main(string[] args)
- {
-
- child obj = new child ();
- Parent p=obj;
- }
Converting parent references back to child references:
- child obj=(child )p;
- child obj=p as child ;
Finally the main block of the child looks like:
- static void Main(string[] args)
- {
- child obj = new child ();
- Parent p=obj;
- obj.childmethod ();
- obj.ParentMethod ();
- Console.ReadKey();
- }
Rule 4: Each and every class we define in .Net languages has an implicit parent class (in other words, the class object) defined in the system namespace.
So the members of this class as in the following:
- Equals
- GetHashCode
- GetType
- ToString
Can be accessed or consumed anywhere.