×

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# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

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

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

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

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

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

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 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# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

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