×

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 while the one which is inherited is called parent object. The chilled object can reuse, redefine all the properties and behavior of its parent object. The class which inherits another class to access its members is called derived class while the class which is inherited is called base class.
 
Advantages of inheritance:
  1. Code reusability: the code which is written in the base class can be reused in the derived class
  1. Code Extensibility: inheritance provides flexibility to extend the code according to the business logic in the derived class.
  1. Removes Code Redundancy: Inheritance removes code redundancy by inheriting all the logic which was once written. We don't need to write the logic again and again.
Types of inheritance in C#:
  1. Single level inheritance
  2. Multilevel inheritance
Note: C# doesn't support multiple inheritance by class.
 
C# Single Level Inheritance Example: inheriting Fields
using System;
public class Bank
{
                        public float R_O_I=6;
}
public class SBI:Bank
{
                        public float share_price=1300;
}
public class Inheritance_Example1
{
                        public static void Main(string[] args)
                        {
                                    SBI bank=new SBI();
                        Console.WriteLine("Rate of interest of SBI is "+bank.R_O_I+" and The share price      is "+bank.share_price);
                        }
}
Output Rate of interest of SBI is 6 and The share price is 1300
 
C# Single Level Inheritance Example: Inheriting Methods
using System;
public class Animal 
{
                        public void eat()
                        {
                        Console.WriteLine("Eating.....");
                        }
}
public class Hourse:Animal
{
                        public void run()
                        {
                        Console.WriteLine("Running......");
                        }
}
public class InheritanceExample2
{
                        public static void Main (string[] args)
                        {
                                    Hourse h=new Hourse();
                                    h.eat();
                                    h.run();
                        }
}
Output Eating..... Running......

Related Topics

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# 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# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 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# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

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.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

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

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# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

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

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes 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# 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.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.