Saturday 18 August 2018

C#: Inheritance

Inheritance is the concept of re usability. Object of one class can get the properties and methods of object of another class by using inheritance. One class can use the methods and properties of other class.

A class that is derived from another class is called a subclass. The class from which the subclass is derived is called a super class. Sub class inherits the properties and methods of super class using ‘:’.

class SuperClass{

}

class SubClass : SuperClass{

}

Example

Rectangle
area = width x height
perimeter = 2 * (width + height)

Square
area = s * s
perimeter = 4s

Where s is the length of side

Program.cs
using System;

class Rectangle
{
    protected double width, height;

    public Rectangle()
    {
        width = height = 0;
    }

    public Rectangle(double width, double height)
    {
        this.width = width;
        this.height = height;
    }

    public Rectangle(double width)
    {
        this.width = this.height = width;
    }

    public double getArea()
    {
        return (width * height);
    }

    public double getPerimeter()
    {
        return (2 * (width + height));
    }

}

class Square : Rectangle
{
    public Square(double s){
        width = height = s;
    }

    public double getSquarePerimeter(){
        return (4 * width);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Square s1 = new Square(10);
        Console.WriteLine("Area : {0}", s1.getArea());
        Console.WriteLine("Perimeter : {0}",s1.getSquarePerimeter());
    }
}


Output
Area : 100
Perimeter : 40

Notify above code snippet, even though Square class don't have fields like width, height, still it can access the width and height. In the same way Square object calls the method getArea() of Rectangle class.

There are six types of inheritance in Object Oriented Programming.

Those are:
    1. Single Level Inheritance
    2. Multi-Level Inheritance
    3. Hierarchical Inheritance
    4. Multiple Inheritance
    5. Hybrid Inheritance
    6. Multi path Inheritance

Single Level Inheritance
One class derived from other class. It is also called as simple Inheritance.



Class B derived from class A. I.e, B is the sub class and A is the super class.

   Class A{

   }

   class B : A{

   }

Whenever sub class constructor is called, the super class default constructor called internally by default.
Program.cs
using System;

class A
{
    public A()
    {
        Console.WriteLine("I am class A's default constructor");
    }

    public A(int a)
    {
        Console.WriteLine("I am class A's parameterized constructor");
    }
}

class B : A
{
    public B(){
        Console.WriteLine("I am class B's default constructor");
    }

    public B(int a){
        Console.WriteLine("I am class B's parameterized constructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        B obj1 = new B();
        B obj2 = new B(10);
    }
}


Output
I am class A's default constructor
I am class B's default constructor
I am class A's default constructor
I am class B's parameterized constructor

As you observe the output
1. Super class constructor called first before executing the subclass constructor code.

2. Super class default constructor is called in the sub class constructors by default.

That's fine, but I want to call the super class parameterized constructor. How can I call that ?
You can call super class parameterized constructor by using the keyword ‘base’.

public B(int a) : base(a)
Above statement calls parameterized constructor of super class.

Following is the complete working application.


Program.cs
using System;

class A
{
    public A()
    {
        Console.WriteLine("I am class A's default constructor");
    }

    public A(int a)
    {
        Console.WriteLine("I am class A's parameterized constructor");
    }
}

class B : A
{
    public B(){
        Console.WriteLine("I am class B's default constructor");
    }

    public B(int a) : base(a) {
        Console.WriteLine("I am class B's parameterized constructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        B obj1 = new B();
        B obj2 = new B(10);
    }
}

Output
I am class A's default constructor
I am class B's default constructor
I am class A's parameterized constructor
I am class B's parameterized constructor

Multi Level Inheritance

The method of deriving a class from another derived class is called as multilevel inheritance. C derived from B. and B derived from A.


Program.cs
using System;

class A
{
    public A()
    {
        Console.WriteLine("I am class A's default constructor");
    }

    public A(int a)
    {
        Console.WriteLine("I am class A's parameterized constructor");
    }
}

class B : A
{
    public B(){
        Console.WriteLine("I am class B's default constructor");
    }

    public B(int a) : base(a) {
        Console.WriteLine("I am class B's parameterized constructor");
    }
}

class C : B
{
    public C(){
        Console.WriteLine("I am class C's default constructor");
    }

    public C(int a){
        Console.WriteLine("I am class C's parameterized constructor");
    }
}

class Program
{
    static void Main(string[] args)
    {
        C obj1 = new C();
        C obj2 = new C(10);
    }
}


Output
I am class A's default constructor
I am class B's default constructor
I am class C's default constructor
I am class A's default constructor
I am class B's default constructor
I am class C's parameterized constructor

Hierarchical Inheritance

Multiple child classes derived from one base(super class) class is called Hierarchical inheritance.


Program.cs
using System;

class A
{
    public A()
    {
        Console.WriteLine("I am class A's default constructor");
    }

    public A(int a)
    {
        Console.WriteLine("I am class A's parameterized constructor");
    }
}

class B : A
{
    public B(){
        Console.WriteLine("I am class B's default constructor");
    }

    public B(int a) : base(a) {
        Console.WriteLine("I am class B's parameterized constructor");
    }
}

class C : A
{
    public C(){
        Console.WriteLine("I am class C's default constructor");
    }

    public C(int a){
        Console.WriteLine("I am class C's parameterized constructor");
    }
}

class D : A
{
    public D()
    {
        Console.WriteLine("I am class C's default constructor");
    }

    public D(int a)
    {
        Console.WriteLine("I am class C's parameterized constructor");
    }
}

Multiple Inheritance

In multiple inheritance one class derived from more than one super class.


C# doesn't support multiple inheritance.

Why C# not supporting multiple inheritance?
Let’s say there is a method called print(), in the super classes A and B

Program.cs
using System;

class A
{
    public void print()
    {
        Console.WriteLine("I am in A");
    }
}

class B
{
    public void print()
    {
        Console.WriteLine("I am in B");
    }
}

class Program : A, B
{
    static void Main(string[] args)
    {
        print();
    }
}


Class "C" is calling the method print(), then C# run time has two options to call print method, one is in the class A and other is in class B, here is an ambiguity. Of course there are languages like C++ provides a way to solve this problem, but to make the C# language simple, C# doesn't support multiple class inheritance directly.

C# supports multiple inheritance using interfaces.

Hybrid Inheritance
It combines two or more forms of inheritance.


Multipath Inheritance
When a class is derived from two or more classes which are derived from the same base class then such type of inheritance is called multipath inheritance.


Here class 'D' derived from classes 'B' and 'C', which are derived from same base class 'A'.






Previous                                                 Next                                                 Home

No comments:

Post a Comment