TEP , The Engineering Projects , Author Welcome! Syed Zain Nasir Mechatronics Engineer TEP , The ENgineering Projects , TEP Badge TEP , The Engineering Projects , Ratting Background 7.5 7.5/10
TEP , The Engineering Projects , Rating Percentage Background Reputation Score 55 / 100
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
TEP , The Engineering Projects , Boxes

Introduction to Inheritance in C#

TEP , THe Engineering Projects , Shares 2.5K Views
TEP , The ENgineering Projects , Reaction 40
TEP , The ENgineering Projects , Reaction 700
TEP , The ENgineering Projects , Reaction 60
TEP , The ENgineering Projects , Reaction 25
TEP , The ENgineering Projects , Reaction 60
TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest TEP , The Engineering Projects , Pintrest
Shares: 691
TEP , The Engineering Projects , PCBWAY
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
Hello friends, I hope you all are having fun. In today's tutorial, we will have a detailed Introduction to Inheritance in C#. It's our 15th tutorial in C# series and again an important concept in Object Oriented Programming, as it helps in code optimization. In our previous tutorial, we have discuss namespaces in C# and that was the last storage item in C#. Today, we are dealing with entirely different entity, which is inheritance. So, let's discuss it in detail:

Introduction to Inheritance in C#

  • Inheritance in C# is used to create a relation between two classes. In simple words, derived / child class is inherited from base / parent class and will automatically have all its members as its own. Inheritance is denoted by ( : ).
  • Mostly in projects, there are few functions which we have to use a lot throughout the project, so we can place such functions in any parent class and then which class needs those functions, we can simply inherit it from that parent class.
  • Here's the syntax for inheritance, where ChildClass is Inherited from ParentClass, so ChildClass can use all functions and fields of ParentClass:
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • When we create a new instance of any child class, then parent class constructor executes first and then child class constructor executes.
  • So, in below figure, I have created two classes, ChildClass is inherited from ParentClass and I have also created their Constructors.
  • Mow in the Main function, I have created a new instance of ChildClass and you can see in the Console that first Parent Constructor Called & then Child Constructor Called.
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • Suppose, you are designing a software for college class, where students have few same subjects but some different subjects.
  • Let's say, Engineering students study Physics & Chemistry while Medical students study Biology but they all have to study Mathematics & English.
  • So, I have created one Parent Class with name MainSubjects, which has the compulsory subjects in it.
  • Then I have created two Derived Class named MedicalSubjects & EnggSubjects and I have derived both of them from Main Subjects, so they can use both Maths & English fields.
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • You can see inheritance in above figure, in the definition of class we have MedicalSubjects : MainSubjects, so MedicalSubjects is inherited from MainSubjects.
  • In the Main code, I have created new instance of MedicalSubjects & then updated score for English, which is actually in MainSubjects, and it updated correctly.
  • So, I can use all members of parent class in derived class.
  • The benefit of Inheritance is that, it allows user to reuse old code without writing it again, so it saves time and reduce errors.
  • C# allows single class inheritance i.e. we can't derive class A from two different classes at the same time.
  • C# allows multiple Interface inheritance, which we will study later in detail.
Now I hope you have understood How to use Inheritance in C#, so now we are ready to discuss a similar concept called Method Hiding, it's related to Inheritance so let's have a look at it.

Method Hiding in C#

  • Method Hiding in C# is a simple technique, where we declare two methods with the same name, one in Child Class & other one in Parent Class and the Child method will hide the parent Method, if called from Child Class Reference Variable.
  • We need to use new Keyword with the child class Method, which will hide the old Parent class method.
  • In our previous part, we have seen How to use Inheritance and have created Parent & Child class.
  • Now, let's create two Methods with the same name, one in ParentClass & one in ChildClass, as shown in below figure:
Introduction to Inheritance in C#,Inheritance in C#, C# Inheritance, Inheritance C#
  • You can see in above figure, that I have created a Method PrintMsg() in both ParentClass & ChildClass.
  • In the Main Function, I have created 3 reference variables, which are:
    • PC is a ParentClass variable, pointing to ParentClass Object.
    • CC is a ChildClass variable, pointing to ChildClass Object.
    • PC2 is a ParentClass variable, pointing to ChildClass Object.
  • You must be wondering about the third variable, yeah we can do that as we can use all members of ParentClass in ChildClass but we can't do the opposite.
  • Moreover, when we called the PrintMsg() Function from 2nd variable, which is a ChildClass variable, then it has executed the Function in ChildClass, which has new Keyword in its definition.
  • So, the second variable has completely ignored (hide) the method in ParentClass and simply used the new method in ChildClass and it's called Method Hiding.
So, that was all about Inheritance in C#, which is quite simple concept but really effective in complex projects. It is considered as a pillar of Object Oriented Programming. In the next lecture, we will have a look at Polymorphism in C#. Till then, take care & have fun !!! :)
TEP , The Engineering Projects , Tags
Introduction to Inheritance in C#
Inheritance in C#
C# Inheritance
Inheritance C#
TEP , The Engineering Projects , Calender Tuesday, November 19, 2019
TEP , The Engineering Projects , Comments

Write a Comment

TEP , The Engineering Projects , WordPress TEP , The Engineering Projects , Google TEP , The Engineering Projects , Twitter TEP , The Engineering Projects , Facebook TEP , The Engineering Projects , User
TEP , The Engineering Projects , Robot
TEP , The Engineering Projects , Comments Comments on ‘’ Introduction to Inheritance in C# ‘’ (0)
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Top PCB Design Service

PCBA
TEP , The Engineering Projects , JLCPCB
PCB
TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Embedded Tools

ARDINO TEP , The Engineering Projects , JLCPCB
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Subscribe Now !!!

Learn Free Pro Tricks

TEP , The Engineering Projects , Mail Receive Quality Tutorials Straight in your Inbox by Submitting your Email ID Below
TEP , The Engineering Projects , Mail TEP , The Engineering Projects , Mail Shadow
TEP , The Engineering Projects , Mail Robot TEP , The Engineering Projects , Mail Robot Shadow TEP , The Engineering Projects , Mail Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Engineering Books

TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (3.0)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for arduino $20
TEP , The Engineering Projects , Rating Stars (4.7)
TEP , The Engineering Projects , Arduino Programming Book
SALE $20
Text Book for Raspberry Pi $20
TEP , The Engineering Projects , Rating Stars (5.0)
TEP , The Engineering Projects , TEP Robot TEP , The Engineering Projects , TEP Robot Shadow TEP , The Engineering Projects , TEP Robot Shadow
TEP , The Engineering Projects , About TEP , The Engineering Projects , About Shadow

Categories

TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K
TEP , The Engineering Projects , Arduino
TEP , The Engineering Projects , Notification 20K
TEP , The Engineering Projects , Tick 900
TEP , The Engineering Projects , Views 900
TEP , The Engineering Projects , Comments 20K