0% found this document useful (0 votes)
8 views16 pages

Ex 3

The document provides an overview of inheritance in C#, explaining its significance in Object-Oriented Programming. It details various types of inheritance, such as single, multilevel, hierarchical, and hybrid inheritance, along with examples of method overriding and the use of the base keyword. Additionally, it emphasizes the importance of inheritance for code reusability through practical examples involving regular polygons.

Uploaded by

gangursahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Ex 3

The document provides an overview of inheritance in C#, explaining its significance in Object-Oriented Programming. It details various types of inheritance, such as single, multilevel, hierarchical, and hybrid inheritance, along with examples of method overriding and the use of the base keyword. Additionally, it emphasizes the importance of inheritance for code reusability through practical examples involving regular polygons.

Uploaded by

gangursahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com

C# Inheritance
In C#, inheritance allows us to create a new class from an
existing class. It is a key feature of Object-Oriented
Programming (OOP).

The class from which a new class is created is known as


the base class (parent or superclass). And, the new class is
called derived class (child or subclass)

The derived class inherits the fields and methods of the


base class. This helps with the code reusability in C#.

How to perform inheritance in C#?


In C#, we use the : symbol to perform inheritance. For
example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
class Animal {
// fields and methods
Try hands-on coding with
36% } (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
// Dog inherits from Animal
Programiz (https://programiz.pro?utm_source=nav-
class DogSearch
: Animal {
PRO
tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
// fields and methods of Animal
// fields and methods of Dog
www.domain-name.com
}

Here, we are inheriting the derived class Dog from the


base class Animal . The Dog class can now access the
fields and methods of Animal class.

C# Inheritance
Example:
Thank you for printing our contentC# Inheritance Please check back soon for new contents.
at www.domain-name.com.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
usingNow
Claim Discount System;

Programiz (https://programiz.pro?utm_source=nav-
PRO namespace
Search tutorials
Inheritance { & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
// base class
www.domain-name.com
class Animal {

public string name;

public void display() {


Console.WriteLine("I am an animal");
}

// derived class of Animal


class Dog : Animal {

public void getName() {


Console.WriteLine("My name is " + name);
}
}

class Program {

static void Main(string[] args) {

// object of derived class

Output

I am an animal
My name is Rohu

In the above example, we have derived a subclass Dog

from the superclass Animal . Notice the statements,

labrador.name = "Rohu";

labrador.getName();

Here, we are using labrador (object of Dog) to access the


name and display() of the Animal class. This is possible
because the derived class inherits all fields and methods
of the
Thank you for printing ourbase class.
content at www.domain-name.com. Please check back soon for new contents.

Try hands-on
Also,coding with accessed the name field inside the method
we have
36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
of the Now
Claim Discount Dog class.

Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com

is-a relationship
In C#, inheritance is an is-a relationship. We use
inheritance only if there is an is-a relationship between
two classes. For example,

Dog is an Animal

Apple is a Fruit

Car is a Vehicle

We can derive Dog from Animal class. Similarly, Apple


from Fruit class and Car from Vehicle class.

protected Members in C#
Inheritance
When we declare a field or method as protected , it can
only be accessed from the same class and its derived
classes.
Example:
Thank you for printing our contentprotected Members
at www.domain-name.com. in check back soon for new contents.
Please

36%
Inheritance
Try hands-on coding with
(https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
using System;
Search tutorials & examples
PRO floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com
namespace Inheritance {

// base class
class Animal {
protected void eat() {
Console.WriteLine("I can eat");
}
}

// derived class of Animal


class Dog : Animal {
static void Main(string[] args) {

Dog labrador = new Dog();

// access protected method from base class


labrador.eat();

Console.ReadLine();
}
}
}

Output

I can eat

In the above example, we have created a class named


Animal . The class includes a protected method eat() .

We have derived the Dog class from the Animal class.


Notice the statement,

labrador.eat();
Since
Thank you for printing the
our content method can be accessed
at www.domain-name.com.
protected from
Please check back soon for new contents.
derived classes, we are able to access the eat() method
Try hands-on coding with
36% (https://programiz.pro?utm_source=sticky-
off
from
Programiz PRO!the Dog class.
banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com
Types of inheritance
There are the following types of inheritance:

1. Single Inheritance
In single inheritance, a single derived class inherits from a
single base class.

C# Single Inheritance

2. Multilevel Inheritance
In multilevel inheritance, a derived class inherits from a
base and then the same derived class acts as a base
class for another class.
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com

C# Multilevel Inheritance

3. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes
inherit from a single base class.

C# Hierarchical Inheritance

4. Multiple Inheritance
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
In multiple inheritance, a single derived class inherits from
www.domain-name.com
multiple base classes. C# doesn't support multiple
inheritance. However, we can achieve multiple
inheritance through interfaces.

Multiple Inheritance

5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types
of inheritance. The combination of multilevel and
hierarchical inheritance is an example of Hybrid
inheritance.
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com

C# Hybrid Inheritance

Method Overriding in C#
Inheritance
If the same method is present in both the base class and
the derived class, the method in the derived class
overrides the method in the base class. This is called
method overriding in C#. For example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
using System;
Try hands-on coding with
36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off namespace Inheritance {
banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
(https://programiz.pro?utm_source=nav-
Programiz// baseSearch
class tutorials & examples
PRO floating&utm_campaign=programiz&utm_medium=referral)
(/)
class Animal {
www.domain-name.com
public virtual void eat() {

Console.WriteLine("I eat food");


}
}

// derived class of Animal


class Dog : Animal {

// overriding method from Animal


public override void eat() {

Console.WriteLine("I eat Dog food");


}
}
class Program {

static void Main(string[] args) {


// object of derived class
Dog labrador = new Dog();

Output

I eat Dog food

In the above example, the eat() method is present in


both the base class and derived class.

When we call eat() using the Dog object labrador ,

labrador.eat();

the method inside Dog is called. This is because the


method inside Dog overrides the same method inside
Animal .
Notice,
Thank you for printing we have
our content used virtual and override
at www.domain-name.com. withback soon for new contents.
Please check
methods of the base class and derived class respectively.
Try hands-on coding with
36% (https://programiz.pro?utm_source=sticky-
off
Here,
Programiz PRO!
banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programizvirtual - allows the method to be overridden by the
(https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
derived class
www.domain-name.com
override - indicates the method is overriding the
method from the base class

base Keyword in C# Inheritance


In the previous example, we saw that the method in the
derived class overrides the method in the base class.

However, what if we want to call the method of the base


class as well?

In that case, we use the base keyword to call the method


of the base class from the derived class.
Example:
Thank you for printing our contentbase keyword in C#
at www.domain-name.com. inheritance
Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
usingNow
Claim Discount System;

Programiz (https://programiz.pro?utm_source=nav-
PRO namespace
Search tutorials
Inheritance { & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
// base class
www.domain-name.com
class Animal {
public virtual void eat() {

Console.WriteLine("Animals eat food.");


}
}

// derived class of Animal


class Dog : Animal {

// overriding method from Animal


public override void eat() {

// call method from Animal class


base.eat();

Console.WriteLine("Dogs eat Dog food.");


}
}
class Program {

static void Main(string[] args) {

Output

Animals eat food.


Dogs eat Dog food.

In the above example, the eat() method is present in


both the base class Animal and the derived class Dog .
Notice the statement,

base.eat();

Here, we have used the base keyword to access the


method of Animal class from the Dog class.
Importance of Inheritance in C#
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% ToPRO! (https://programiz.pro?utm_source=sticky-
understand the importance of Inheritance, let's
Programiz
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount
considerNowa situation.
Programiz (https://programiz.pro?utm_source=nav-
PRO Suppose
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/) we are working with regular polygons such as
www.domain-name.com
squares, rectangles, and so on. And, we have to find the
perimeter of these polygons based on the input.

1. Since the formula to calculate perimeter is common for


all regular polygons, we can create a RegularPolygon

class and a method calculatePerimeter() to calculate


perimeter.

class RegularPolygon {

calculatePerimeter() {
// code to compute perimeter
}
}

2. And inherit Square and Rectangle classes from the


RegularPolygon class. Each of these classes will have
properties to store the length and number of sides
because they are different for all polygons.

class Square : RegularPolygon {

int length = 0;
int sides = 0;
}

We pass the value of the length and sides to


calculateperimeter() to compute the perimeter.

This is how inheritance makes our code reusable and


more intuitive.
Example:
Thank you for printing our contentImportance of Inheritance
at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
usingNow
Claim Discount System;

Programiz (https://programiz.pro?utm_source=nav-
PRO namespace
Search tutorials
Inheritance { & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com
class RegularPolygon {

public void calculatePerimeter(int length, int si

int result = length * sides;


Console.WriteLine("Perimeter: " + result);
}
}

class Square : RegularPolygon {

public int length = 200;


public int sides = 4;
public void calculateArea() {

int area = length * length;


Console.WriteLine("Area of Square: " + area);
}
}

class Rectangle : RegularPolygon {

public int length = 100;

Output

Area of Square: 40000


Perimeter: 800
Area of Rectangle: 20000
Perimeter: 400

In the above example, we have created a RegularPolygon

class that has a method to calculate the perimeter of the


regular polygon.

Here, the Square and Rectangle inherit from


RegularPolygon .
The formula
Thank you for printing toatcalculate
our content the perimeter
www.domain-name.com. is common
Please for all,
check back soon for new contents.
so we have reused the calculatePerimeter() method of
Try hands-on coding with
36% (https://programiz.pro?utm_source=sticky-
off
thePRO!
Programiz base class.
banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
And since
Programiz the formula to calculate the area is different for
(https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
different shapes, we have created a separate method
www.domain-name.com
inside the derived class to calculate the area.

Next Tutorial:
(/csharp-
C# abstract class and programming/abstract-
method class)

Previous Tutorial:
C# static (/csharp-programming/static-
keyword)
Keyword

Share on:

(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/int
u=https://www.programiz.com/csharp- text=Check%20this%2
programming/inheritance) programming/inheritan

Did you find this article helpful?


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on coding with


36% (https://programiz.pro?utm_source=sticky-
Programiz PRO!
off banner&utm_campaign=programiz&utm_medium=referral)
Claim Discount Now
Programiz (https://programiz.pro?utm_source=nav-
PRO
Search tutorials & examples
floating&utm_campaign=programiz&utm_medium=referral)
(/)
www.domain-name.com
Related Tutorials

C# Tutorial

C# abstract class and method

(/csharp-programming/abstract-class)

C# Tutorial

C# sealed class and method

(/csharp-programming/sealed-class)

C# Tutorial

C# Polymorphism

(/csharp-programming/polymorphism)

C# Tutorial

C# interface

(/csharp-programming/interface)

You might also like