Ex 3
Ex 3
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).
C# Inheritance
Example:
Thank you for printing our contentC# Inheritance Please check back soon for new contents.
at www.domain-name.com.
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 {
class Program {
Output
I am an animal
My name is Rohu
labrador.name = "Rohu";
labrador.getName();
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
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");
}
}
Console.ReadLine();
}
}
}
Output
I can eat
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.
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.
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.
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() {
Output
labrador.eat();
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() {
Output
base.eat();
class RegularPolygon {
calculatePerimeter() {
// code to compute perimeter
}
}
int length = 0;
int sides = 0;
}
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 {
Output
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
C# Tutorial
(/csharp-programming/abstract-class)
C# Tutorial
(/csharp-programming/sealed-class)
C# Tutorial
C# Polymorphism
(/csharp-programming/polymorphism)
C# Tutorial
C# interface
(/csharp-programming/interface)