0% found this document useful (0 votes)
65 views

Visual Programming-II (With C#)

The document discusses different types of inheritance in object-oriented programming with C#, including single inheritance, hierarchical inheritance, multilevel inheritance, multiple inheritance using interfaces, and hybrid inheritance using interfaces. It provides examples and definitions of each type of inheritance. Key points covered include that C# only supports single inheritance of classes but allows multiple inheritance through interfaces, and that subclasses inherit fields and methods but not private members or constructors from their parent classes.

Uploaded by

Mahammad azad
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)
65 views

Visual Programming-II (With C#)

The document discusses different types of inheritance in object-oriented programming with C#, including single inheritance, hierarchical inheritance, multilevel inheritance, multiple inheritance using interfaces, and hybrid inheritance using interfaces. It provides examples and definitions of each type of inheritance. Key points covered include that C# only supports single inheritance of classes but allows multiple inheritance through interfaces, and that subclasses inherit fields and methods but not private members or constructors from their parent classes.

Uploaded by

Mahammad azad
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/ 13

Visual Programming-II (with C#)

Lebanese French University


College of Engineering and Computer Science
Information Technology Department
Asst. Prof. Ashish Sharma
Lecture # 5-6

1 4/21/2021
Outline
 Introduction to Inheritance

 Types of Inheritance

➢ Single Inheritance

➢ Hierarchical Inheritance

➢ Multilevel Inheritance

➢ Multiple Inheritance (using Interfaces)

➢ Hybrid Inheritance (using Interfaces)

2 4/21/2021
Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming).

It is the mechanism by which one class is allowed to inherit the features


(fields and methods) of another class.

Inheritance is a process in which one object acquires all the properties


and behaviors of its parent object automatically.

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.

• Reusability: Inheritance supports the concept of “reusability”, i.e. when


programmer wants to create a new class and there is already a class that
includes some of the code that we want, we can derive our new class
from the existing class. By doing this, we are reusing the fields and
methods of the existing class.

The symbol used for inheritance is colon (:)

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();

Console.WriteLine("Salary: " + p1.salary);


Console.WriteLine("Bonus: " + p1.bonus);
}
5 } 4/21/2021
6 4/21/2021
Single inheritance
It is the type of inheritance in which there is one base class and one derived
class. For example:
public class Accountcreditinfo //base class
{
public string Credit()
{
return "balance is credited";
}
}
public class debitinfo : Accountcreditinfo //derived class
{
public string debit()
{
Credit(); //derived class method
return "balance is debited";
}
}
In this program Accountcreditinfo is the base class and debitinfo is the derived class.
7 4/21/2021
Hierarchical inheritance
This is the type of inheritance in which there are multiple classes derived
from one base class. This type of inheritance is used when there is a
requirement of one class feature that is needed in multiple classes.
For example:
class A //base class
{
public string msg()
{
return "this is A class Method";
}
}
class C : A
class B : A
{
{
public string getinfo()
public string info()
{
{
msg();
msg();
return "this is C class Method";
return "this is B class Method";
}
}
}
}
In this program one base class is derived in many classes, hence it is a
8 4/21/2021
called a Hierarchical Inheritance.
Multilevel inheritance
When one class is derived from another derived class, then this type of
inheritance is called multilevel inheritance. For example:
public class Person A
{
public string persondet()
B
{
return "this is the person class";
} C
}
Multilevel Inheritance
public class Bird : Person public class Animal : Bird
{ {
public string birddet() public string animaldet()
{ {
persondet(); persondet();
return "this is the birddet Class"; birddet();
} return "this is the Animal Class";
} }
}

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:

public interface IA //interface 1


{
string setImgs(string a);
}
public interface IB //Interface 2
{
int getAmount(int Amt); In this program the ICar class inherits the
} features of the two interfaces hence this
public class ICar : IA, IB //implementation type of inheritance is called Multiple
{ Inheritance.
public int getAmount(int Amt) The following are some key points about
{ inheritance:
return 100; 1. C# does not support multiple inheritance of
} classes, the same thing can be done using
public string setImgs(string a) interfaces.
{ 2. Private members are not accessed in a derived
return "this is the car"; class when one class is derived from another.
}
10 } 4/21/2021
Hybrid Inheritance (Through Interfaces)
It is a mix of two or more of the above types of inheritance. Since C#
doesn’t support multiple inheritance with classes, the hybrid inheritance is
also not possible with classes. In C#, we can achieve hybrid inheritance
only through Interfaces.

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.

• Superclass can only be one: A superclass can have any number of


subclasses. But a subclass can have only one superclass. This is because
C# does not support multiple inheritance with classes. Although with
interfaces, multiple inheritance is supported by C#.

• Inheriting Constructors: A subclass inherits all the members (fields,


methods) from its superclass. Constructors are not members, so they are
not inherited by subclasses, but the constructor of the superclass can be
invoked from the subclass.

• Private member inheritance: A subclass does not inherit the private


members of its parent class. However, if the superclass has properties (get
and set methods) for accessing its private fields, then a subclass can
12 inherit. 4/21/2021
13 4/21/2021

You might also like