In this blog I am intended to explore the concept of Inheritance in OOPs and C#.
Inheritance is the basic concept of OOPs (object oriented programming). This assists the programmer to code reusability of existing code.
Inheritance implement by inheriting the base (super/parent)class member to its derived (sub/child)class, which in turn enables the base class to access the code of its base class. That means we extend our derived class functionality by inheriting the base class.
Every programming languages that supports OOP must contains a provision for implementing Inheritance. And each language has its own syntax to create and use it.
Basically there are two types of inheritance in OOPs. But both types of inheritance is rarely supported by language(c++) because of problem in second type of inheritance (multiple inheritance)
1. Single Inheritance
2. Multiple inheritance( c# does not supports )
Now we discussed about single inheritance, because many popular OOPs supporting languages (java, c# etc.) does not support the Multiple inheritance. So now we let’s focus on c#(c-Sharp) inheritance implementation issues:
When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.
C# supports single inheritance and implemented in following manner
Syntax:
<Derived class> : <base class>// the only one base class is place here, because of single inheritance supports.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace inheritanceExample
{
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10, 15);//create object of rectangle class rect.Area();//call method area() of its own class
Square sqr = new Square(8);//create object of square class sqr.Area();//call method area() of its own class
Console.ReadKey();
}
}
// base class shape
class Shape
{
public int length;
public int breadth;
public Shape(int length)
{
this.length = length;
}
public Shape(int length, int breadth)
{
this.length = length;
this.breadth = breadth;
}
public void Area()// base class method area()
{
Console.WriteLine("Area method of base class");
}
}
//derived class square inherite base class shape
class Square : Shape
{
public Square(int length)//calling base class constructor
: base(length)
{
}
// creating/overriding new definition of area method of base class
public new void Area()
{
base.Area();//calling area() method of base class
Console.WriteLine("Area of Square(L x L) = {0} x {0} = {1}",
length,
length * length);
}
}
//derived class rectangle inherite base class shape
class Rectangle : Shape
{
public Rectangle(int length, int breadth)//calling base class constructor : base(length, breadth)
{
}
// creating/overriding new definition of area method of base class
public new void Area()
{
base.Area();//calling area() method of base class
Console.WriteLine("Area of Rectangle(L x B) = {0} x {1} = {2}",
length,
breadth,
length * breadth);
}
}
}
As you have been noticed, the derived class never contains
public int length;
public int breadth;
Then how its methods are able to access the length and breadth in its
method calls like
public new void Area()
{
base.Area();//calling area() method of base class
Console.WriteLine("Area of Rectangle(L x B) = {0} x {1} = {2}",
length,
breadth,
length * breadth);
}
How surprise!
No, this is possible due to the inheritance used in this program, which causes the derived classes Rectangle and Triangle inherits all the member of base class Shape , which already contains (length and breadth). So after inheriting Shape class the derived classes can also have (length and breadth). Thus no need to define it again in derived class separately. Only needs to use it, instead of redefining it.
Leave Comment