×

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

Related Topics

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.