Sir Syed University of Engineering & Technology, Karachi
Inheritance
Course Code : CS-127
Course Title : Object Oriented Programming
Semester : 2nd
1
Batch - 2020 Department of Computer Science / Information Technology
C# Inheritance
• In c#, Inheritance is one of the primary concept of object-
oriented programming (OOP) and it is used to inherit the
properties from one class (base) to another (child) class.
• The inheritance will enable us to create a new class by
inheriting the properties from other classes to reuse,
extend and modify the behavior of other class members
based on the requirements.
• In c# inheritance, the class whose members are inherited is
called a base (parent) class and the class that inherits the
members of base (parent) class is called a derived
(child) class.
Department of Computer Science /
9/28/2020 2
Information Technology
Advantage of C# Inheritance
“Code reusability: Now you can reuse the
members of your parent class. So, there is no
need to define the member again. So less code
is required in the class”
Department of Computer Science /
9/28/2020 3
Information Technology
C# Single Level Inheritance
Inheriting Fields and Methods
• When one class inherits another class, it is known as single level
inheritance. Let's see the example of single level inheritance which
inherits the fields and methods .
Department of Computer Science /
9/28/2020 4
Information Technology
Single Level Inheritance Example
using System;
using [Link];
using [Link];
namespace ConsoleApplication60
{
class Employee
{
public float salary = 4000;
}
class Programmer : Employee
{
public float bonus = 3433;
}
class Program
{
static void Main(string[] args)
{
Programmer pr = new Programmer();
[Link](" Salary : "+[Link]);
[Link](" Bonus : "+[Link]);
[Link]();
}
}
} Department of Computer Science /
9/28/2020 5
Information Technology
Access Modifiers in C#
Department of Computer Science /
9/28/2020 6
Information Technology
Order Of Execution Of The Constructors
• To demonstrate the order of execution of the constructors,
examine the following sample code. The first class is the
main Program class within a console application.
• It simply instantiates a MySubclass object. MyBaseClass is a
class with a single constructor that outputs a message to
the console. MySubclass inherits from MyBaseClass and
also outputs to the console when constructed.
• Note the order in which the messages appear.
Department of Computer Science /
9/28/2020 7
Information Technology
• class Program
• {
• static void Main()
• {
• MySubclass test = new MySubclass();
• }
• }
• class MyBaseClass
• {
• public MyBaseClass()
• {
• [Link]("MyBaseClass constructor called.");
• }
• }
• class MySubclass : MyBaseClass
• {
• public MySubclass()
• {
• [Link]("MySubclass constructor called.");
• }
• }
•
• /* OUTPUT
• MyBaseClass constructor called.
• MySubclass constructor called.
• */ Department of Computer Science /
9/28/2020 8
Information Technology
Order Of Execution Of The Constructors
class details : User
using System; {
using [Link]; public int salary;
using [Link]; public void displaysalary()
{
namespace ConsoleApplication60 [Link](" Salary : " + salary);
}
{
public details(int number)
class User {
{ [Link](" Child Class Constructor invoked..." +
public static string name; number);
public string location; }
public void displayuserinfo() }
class Program
{
{
[Link](" Name : "+name); static void Main(string[] args)
[Link](" Location : "+location); {
} details d = new details(387564);
public User() [Link] = "Ali Ahmed";
{ [Link] = "Karachi";
[Link]("Parent (Base) class [Link] = 2324;
constructor invoked..."); [Link]();
} [Link]();
[Link]();
}
}
}
}
Department of Computer Science /
9/28/2020 9
Information Technology
C# Multi-Level Inheritance
• When one class inherits another class which is further
inherited by another class, it is known as multi level
inheritance in C#.
• Inheritance is transitive so the last derived class acquires all
the members of all its base classes.
• Let's see the example of multi level inheritance in C#.
Department of Computer Science /
9/28/2020 10
Information Technology
• For example, suppose if class C is derived from class B, and class B is derived from
class A, then class C inherits the members declared in both class B and class A.
public class A
{
// Implementation
}
public class B : A
{
// Implementation
}
public class C : B
{
// Implementation
}
• class C is derived from class B, and class B is derived from class A, then class C
inherits the members declared in both class B and class A. This is how we can
implement multi-level inheritance in our applications.
Department of Computer Science /
9/28/2020 11
Information Technology
Multi-Level Inheritance Example
using System;
using [Link]; class D : C
using [Link]; {
public int age;
namespace ConsoleApplication60 public void displayage()
{ {
class A [Link](" Age : "+age);
{ }
public string name;
}
public void displayname()
class Program
{
{
[Link](" Name : "+name);
} static void Main(string[] args)
} {
class B : A D d = new D();
{ [Link] = "Ikrama";
public string location; [Link] = "Islamabad";
public void displaylocation() [Link] = 3533;
{ [Link] = 65;
[Link]("Location : "+location); [Link]();
}
[Link]();
}
[Link]();
class C : B
[Link]();
{
public int salary; [Link]();
public void displaysalary() }
{ }
[Link]("Salary : "+salary); }
}}
Department of Computer Science /
9/28/2020 12
Information Technology
The sealed Keyword
If we don't want other classes to inherit from a class, use the
sealed keyword:
Department of Computer Science /
9/28/2020 13
Information Technology