Algorithm
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (subclass or derived class) to inherit properties and behaviors from another class (base class or parent class). In C#, you can achieve inheritance using the : (colon)
symbol.
Code Examples
#1 C# Progrramming Inheritance
Code -
C# Programming
using System;
namespace Inheritance {
// base class
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
Dog labrador = new Dog();
// access field and method of base class
labrador.name = "Rohu";
labrador.display();
// access method from own class
labrador.getName();
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
My name is Rohu
#2 protected Members in Inheritance in C# Programming
Code -
C# Programming
using System;
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();
}
}
}
Copy The Code &
Try With Live Editor
Output
#3 Method Overriding in C# Inheritance
Code -
C# Programming
using System;
namespace Inheritance {
// base class
class Animal {
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();
// accesses overridden method
labrador.eat();
}
}
}
Copy The Code &
Try With Live Editor
Output
#4 base keyword in C# inheritance
Code -
C# Programming
using System;
namespace Inheritance {
// base class
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) {
Dog labrador = new Dog();
labrador.eat();
}
}
}
Copy The Code &
Try With Live Editor
Output
Dogs eat Dog food.
#5 Importance of Inheritance in C#
Code -
C# Programming
using System;
namespace Inheritance {
class RegularPolygon {
public void calculatePerimeter(int length, int sides) {
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;
public int breadth = 200;
public int sides = 4;
public void calculateArea() {
int area = length * breadth;
Console.WriteLine("Area of Rectangle: " + area);
}
}
class Program {
static void Main(string[] args) {
Square s1 = new Square();
s1.calculateArea();
s1.calculatePerimeter(s1.length, s1.sides);
Rectangle t1 = new Rectangle();
t1.calculateArea();
t1.calculatePerimeter(t1.length, t1.sides);
}
}
}
Copy The Code &
Try With Live Editor
Output
Perimeter: 800
Area of Rectangle: 20000
Perimeter: 400
Demonstration
C# Programming Inheritance