Different Types of inheritance examples in .net usng csharp (C#)


Inheritance using C#

In interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. 

 

Uses of Interface: We should use interface when we are not sure of any definition of members.

We are not sure about area and perimeter of shape, but we know there is a calculation of area and perimeter of circle and rectangle but not for shape. But this two are the part of shape. In this we will declare interface for Shape and then Circle and Rectangle as a class will inherit the shape interface. 

Example 1: Call methods of class using interface.

Declaring Interface

Interfaces are declared using the interface keyword. It is similar to class declaration. Interface statements are public by default. Following is an example of an interface declaration:

Below example are for calling of Add method with multiple parameter and call finally display method.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace InterfaceExample2

{

   interface Interface1

    {

       

        void Add(int x);

        void Add(int x, int y);

        void display();

 

    }

    

 

    class Addition : Interface1

    {

        public int Num1 { get; set; }

        public int Num2 { get; set; }

 

        public void Add(int x)

        {

 

            Num1 = x;

        }

 

        public void Add(int x, int y)

        {

 

            Num1 = x;

            Num2 = y;

        }

      

 

        public void display()

        {

            Console.WriteLine("Sum is = {0}",Num1 + Num2);

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            Interface1 intf;

            

            intf = new Addition();

            //Passing one parameter

            intf.Add(10);

            intf.display();

 

            //Passing two parameters

            intf.Add(20, 30);

            intf.display();

 

            Console.ReadLine();

            

        }

    }

 

}

 Output : 

Example 2: Multiple inheritance using interface and handling of ambiguity method.

The below example having two interface with the same method named display(). When you are going to inherit the interface1 and interface2 , you will get same display method from both interface in this case we have to use scope resolution.

 

We need to tell the compiler which class function we want to implement. For such cases, we need to use the name of the interface during the implementation of a function. Have a look at the following example:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace InterfaceExample2

{

    interface Interface1

    {

 

        void Add(int x);

        void Add(int x, int y);

        void display();

 

    }

    interface Interface2

    {

 

        void sub(int x);

        void sub(int x, int y);

        void display();

    }

 

 

    class Calculation : Interface1, Interface2

    {

        public int Num1 { get; set; }

        public int Num2 { get; set; }

 

        public void Add(int x)

        {

 

            Num1 = x;

        }

 

        public void Add(int x, int y)

        {

            Num1 = x;

            Num2 = y;

        }

 

        public void sub(int x)

        {

            Num1 = -x;

        }

 

        public void sub(int x, int y)

        {

            Num1 = x;

            Num2 = y;

        }

 

        void Interface1.display()

        {

            Console.WriteLine("Sum is = {0}", Num1 + Num2);

        }

 

        void Interface2.display()

        {

            Console.WriteLine("Subtraction is = {0}", Num1 - Num2);

        }

    }

}

 

Here from main method, we are trying to call Calculation class using interface.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace InterfaceExample2

{

    class Program

    {

        static void Main(string[] args)

        {

            Interface1 intf;

            Interface2 intf2;

          

            intf = new Calculation();

            intf.Add(30);

            intf.Add(30, 70);

            intf.display();// Calling display method of Interface 1

 

            intf2 = new Calculation();

            intf2.sub(30);

            intf2.sub(30, 70);

            intf2.display(); //Calling display method of Interface 2

 

            Console.ReadLine();

            

        }

    }

}

Output: 

Example 3 : Multilevel Inheritance with virtual method:

This C# Program describes Multilevel Inheritance with Virtual Methods. Here the system executes the first override-virtual method found from the hierarchy.

This program also demonstrates how base class constructors are inherited using multilevel inheritance.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultilevelInheritance

{

    public class Customer

    {

        public float fDis { get; set; }

        public Customer()

        {

            Console.WriteLine("I am a normal customer.");

        }

        public virtual void discount()

        {

            fDis = 0.3F;

            Console.WriteLine("Discount is :{0}", fDis);

        }

 

    }

    public class SilverCustomer : Customer

    {

        public SilverCustomer()

            : base()

        {

            Console.WriteLine("I am silver customer.");

        }

        public override void discount()

        {

            fDis = 0.4F;

            Console.WriteLine("Discount is :{0}", fDis);

        }

    }

    class GoldenCustomer : SilverCustomer

    {

        public GoldenCustomer()

        {

            Console.WriteLine("I am Golden customer.");

        }

        public override void discount()

        {

            fDis = 0.6F;

            Console.WriteLine("Discount is :{0}", fDis);

        }

 

    }

}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultilevelInheritance

{

    class Program

    {

        static void Main(string[] args)

        {

            Customer objCus = new Customer();

            objCus.discount();

 

            SilverCustomer objSil = new SilverCustomer();

            objSil.discount();

 

            GoldenCustomer objGold = new GoldenCustomer();

            objGold.discount();

 

 

            Console.ReadLine();

        }

    }

}

 

Output : 

Multilevel inheritance in c# with example

We discussed a bit about Multilevel inheritance in types of inheritance in C#. In this tutorial we will explain multilevel inheritance with the help of diagram and example program.

Diagram representation:

Multilevel-Inheritance-Diagram

It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grandparent class. If we take the example of above diagram, then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance.

Example 4 : Th multi level inheritance 

In below example there is a Class Car, Car is inheriting by Suzuki , Suzuki is inheriting by SwiftDzire. Now we are creating a object for SwiftDzire who is able to access the member of SwiftDzire, member of Suziki and  member of Car. 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultipleAndMultilevelInterface

{

    class Car

    {

        public Car()

        {

            Console.WriteLine("This is a car.");

        }

        public void vehicleType()

        {

            Console.WriteLine("Vehicle Type : Car");

        }

    }

    class Suzuki : Car

    {

        public Suzuki()

        {

            Console.WriteLine("Class Suzuki");

        }

        public void Brand()

        {

            Console.WriteLine("Brand : Suzuki");

        }

        public void Speed()

        {

            Console.WriteLine("Speed : 120KMPH");

        }

    }

    class SwiftDzire : Suzuki

    {

        public SwiftDzire()

        {

            Console.WriteLine("Suzuki Model : SwiftDzire");

        }

        public void Speed()

        {

            Console.WriteLine("Speed : 110KMPH");

        }

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultipleAndMultilevelInterface

{

    class Program

    {

        static void Main(string[] args)

        {

            //Calling SwiftDzire members using SwiftDzire object we are able to access the memebr of Suzuki and Car.

            SwiftDzire objDzire = new SwiftDzire();

            objDzire.Brand();

            objDzire.Speed();

            objDzire.vehicleType();

 

            Console.ReadLine();

        }

    }

}

Output :

Use of Multiple inheritance as well as Multi Level inheritance.

In below example, SwiftDzire is inheriting the class Suzuki and Implementing ICar interface and ICarSailing interface. In ICar interface, SwiftDzire is implementing wheelscount() for car and iCarSailing, SwiftDzire is setting the Boolean property to true or false, based on this property we can determine , car can sail in water or not.

Example 5: Multiple Inheritance and Multilevel Inheritance

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultipleAndMultilevelInterface

{

 

    //Here we will check wheelscount

    interface Icar

    {

        int WheelsCount();

    }

    //Here we are declaring a interface for ICarSailing , a car can sail in water.

    interface ICarSailing

    {

        bool bCanSailing

        {

            get;

        }

    }

 

    class Car

    {

        public Car()

        {

            Console.WriteLine("This is a car.");

        }

        public void vehicleType()

        {

            Console.WriteLine("Vehicle Type : Car");

        }

    }

    class Suzuki : Car

    {

        public Suzuki():base()

        {

            Console.WriteLine("Class Suzuki");

        }

        public void Brand()

        {

            Console.WriteLine("Brand : Suzuki");

        }

        public void Speed()

        {

            Console.WriteLine("Speed : 120KMPH");

        }

    }

    class SwiftDzire : Suzuki, Icar, ICarSailing

    {

        public SwiftDzire():base()

        {

            Console.WriteLine("Suzuki Model : SwiftDzire");

        }

        public void Speed()

        {

            Console.WriteLine("Speed : 110KMPH");

        }

 

        //Implementation of WheelCount for Interface ICar

        public int WheelsCount()

        {

            return 6;

        }

        //Implementation of property of bCanSailing for Interface ICarSailing

        public bool bCanSailing

        {

            get

            {

                return true;

            }

        }

    }

 

}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace MultipleAndMultilevelInterface

{

    class Program

    {

        static void Main(string[] args)

        {

            //Calling SwiftDzire members using SwiftDzire object we are able to access the memebr of Suzuki and Car.

            SwiftDzire objDzire = new SwiftDzire();

            objDzire.Brand();

            objDzire.Speed();

            objDzire.vehicleType();

            

            //Here we are accessing members of interface ICar, ICarSailing.

            Icar objCar = new SwiftDzire();

            objCar.WheelsCount();

            //Here we are accessing the property of ICarSailing

            ICarSailing objCarSailing = new SwiftDzire();

            Boolean bCanCarSailing = objCarSailing.bCanSailing;

 

            Console.WriteLine("Can this model sail in water ? : {0}",bCanCarSailing);

 

            Console.ReadLine();

        }

    }

}

 

Output :

 

Example 6: How to implement Hide Method functionality?

If a method with the same signature is declared in both base and derived classes but the methods are not declared as virtual and override, respectively, then the derived class version is said to hide the base class version.

Example:

using System;

 

namespace HideMethod

{

    class MyBaseClass

    {

        public void Display()

        {

            Console.WriteLine("Base Class ...");

        }

    }

    class MyDerivedClass : MyBaseClass

    {

        public  void Display()

        {

            Console.WriteLine("Derived Class ...");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            MyDerivedClass objDer = new MyDerivedClass();

            objDer.Display();

            Console.ReadLine();

        }

    }

}

Output : 

 

Example 7: How to call base version of function in inheritance using C#?

C# has a special syntax for calling base versions of a method from a derived class: base.<MethodName>().For example, if you want a method in a derived class to return 70 percent of the value returned by the base class method, you can use the following syntax:

Example:

 class CustomerAccount

    {

        public virtual decimal CalculatePrice()

        {

            // implementation

            return 0.0M;

        }

    }

    class GoldAccount : CustomerAccount

    {

        public override decimal CalculatePrice()

        {

            return base.CalculatePrice() * 0.9M;

        }

    }

 

 


Recommended Book for MVC : List of Some Important books for .NET Framework, C#, MVC, WCF



(Visited 250 times, 1 visits today)

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *