×

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# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 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.

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# 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# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 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.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

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...

2 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

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.

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# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.