C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class inherits all of its base classes. Let's see a simple example in which a class animal is inherited further by Dog class and this is also inherited by another class babydog. At last babydog not only acquire all the properties of its base class dog but of animal also.

using System;  
   public class Animal  
    {  
       public void eat() { Console.WriteLine("Eating..."); }  
   }  
   public class Dog: Animal  
   {  
       public void bark() { Console.WriteLine("Barking..."); }  
   }  
   public class BabyDog : Dog  
   {  
       public void weep() { Console.WriteLine("Weeping..."); }  
   }  
   public class TestInheritance2{  
       public static void Main(string[] args)  
        {  
            BabyDog d1 = new BabyDog();  
            d1.eat();  
            d1.bark();  
            d1.weep();  
        }  
    }  
Output
Eating...
Barking...
Weeping...
C# Multilevel Inheritance: some more meaningful Example
using System;
public class RBI
{
            public float rate_rbi=6.5f;
            public float calculateBalRBI(int amount,int years)                      
            {
                        return amount+((amount*rate_rbi*years)/100);
            }
}
public class SBI:RBI
{
            public float rate_sbi=7f;
            public float calculateBalSBI(int amount,int years)
            {
                        return amount+((amount*rate_sbi*years)/100);
            }
}
public class Canera:SBI
{
            public float rate_canera=7.2f;
            public float calculateBalCanera(int amount,int years)
            {
                       return amount+((amount*rate_canera*years)/100);
            }
}
public class InheritanceExample
{
            public static void Main (string[] args)
            {
            Canera c=new Canera();
            Console.WriteLine("Enter the amount you want to invest??");
            int amount=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("for how many time you want to invest (in years)?");
            int years=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Your amount according to canera bank policies after "+years+" years will be "+c.calculateBalCanera(amount,years)); 
            Console.WriteLine("Your amount according to State bank of India policies after "+years+" years will be "+c.calculateBalSBI(amount,years));
            Console.WriteLine("Your amount according to Reserve bank of India policies after "+years+" years will be "+c.calculateBalRBI(amount,years));
            }
}
Output
Enter the amount you want to invest??
1000
for how many time you want to invest (in years)?
10
Your amount according to canera bank policies after 10 years will be 1720
Your amount according to State bank of India policies after 10 years will be 1700
Your amount according to Reserve bank of India policies after 10 years will be 1650



ADVERTISEMENT
ADVERTISEMENT