Visual Programming-II (With C#)
Visual Programming-II (With C#)
1 4/21/2021
Outline
Introduction to Inheritance
Types of Inheritance
➢ Single Inheritance
➢ Hierarchical Inheritance
➢ Multilevel Inheritance
2 4/21/2021
Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming).
In such way, programmer can reuse, extend or modify the attributes and
behaviors which is defined in other class.
Important terminology
• Super Class: The class whose features are inherited is known as super
class(or a base class or a parent class).
3 4/21/2021
• Sub Class: The class that inherits the other class is known as subclass(or
a derived class, extended class, or child class). The subclass can add its
own fields and methods in addition to the superclass fields and methods.
The derived class is the specialized class for the base class.
Syntax
class derived-class : base-class
{
// methods and fields
.
.
} 4/21/2021
4
//Example: Single level inheritance, inherits the fields only
using System;
public class Employee In this example, Employee is
{ the base class and Programmer is
public float salary = 40000; the derived class.
}
public class Programmer: Employee
{
public float bonus = 10000; Output
}
Salary: 40000
class TestInheritance{ Bonus: 10000
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
9
In this program, each class is derived from one class that is derived from another 4/21/2021
class. Hence, this type of inheritance is called Multilevel Inheritance.
Multiple inheritance using Interfaces
C# does not support multiple inheritances of classes. To overcome this
problem programmer can use interfaces. For example:
11 4/21/2021
Important facts about inheritance in C#
• Default Superclass: Except Object class, which has no superclass, every
class has one and only one direct superclass(single inheritance). In the
absence of any other explicit superclass, every class is implicitly a
subclass of Object class.