C# : Inheritance

In object-oriented programing (OOP) inheritance is a feature that represents the "is a" relationship between different classes. Inheritance allows a class to

have the same behavior as another class and extend or tailor that behavior to provide special action for specific needs.
Like in our day to day life we see various examples of inheritance, for example people inherit some properties from their ancestors like eye color, hair color, face gestures etc i.e they derive few properties from their base classes , same is the case with object oriented programming languages like c# or other. So, today in this post we learn basic concepts of inheritance in C#, how we implement inheritance in our C# program, what are base and derived classes, which data members we can inherit and so on. Now, let's get started!!!

What is Inheritance?

Inheritance means using the Pre-defined Code This is very Main Feature of OOP With the advantage of Inheritance we can use any code that is previously created. With the help of inheritance we uses the code that is previously defined but always Remember, We are only using that code but not changing that code.

With the Advent of inheritance we are able to use pre-defined code and also able to add new code. All the pre-defined code is reside into the form of classes if we want to use that code then we have to inherit or extend that class.

Base and Derived Classes

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base class or interface. The Class that is Pre-defined is called as Base or super Class and the class which uses the Existing Code is known as derived or sub class.

The syntax used in C# for creating derived classes is as follows:



Now, consider a base class Shape which is inherited by a derived class Rectangle. Here, you see in class Shape we created two integer data types namely "width" and "height" with protected access specifier, by default access specifier in private, you can also use public specifier for this task. Below, we created a simple program which calculates the area of rectangle using the concept of inheritance, we created derived class Rectangle which inherit from base class Shape.

Note : Colon ( : ) symbol is used for inheritance which separates base class and deived class.

In derived class Rectangle we created a method named getarea() which return area i.e width*height. Also, we created our main class is "Program" in which we created an object "rect" which will access properties of class Rectangle as well as class Shape as it is inherited by class Rectangle.

using System;
namespace Program
{
    class Shape
    {
        protected int width;
        protected int height;
        public void setwidth(int w)
        {
            width = w;
        }
        public void setheight(int h)
        {
            height = h;
        }
    }

    class Rectangle : Shape
    {
        public int getarea()
        {
            return (width*height);
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            Rectangle rect = new Rectangle();
            rect.setwidth(5);
            rect.setheight(50);
            Console.WriteLine("Total Area : "+ rect.getarea());
            Console.ReadKey();
        }
    }
}

Output

-----------------------------------------

Total Area : 250

-----------------------------------------

Now, its all for today I hope you all like this post and understand well about concept of Inheritance. Feel free to share this post among your friends and mention us in comments. Thank You!!
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment