Lecture 10 - Inheritance
Lecture 10 - Inheritance
https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content (1)
Introduction to Microsoft Visual C# and Visual Studio
Working with variables, Data types, Operators, and Expressions
Visual C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods
Using Decision Statements
3
C# Inheritance
• One of the most important concepts in object-oriented
programming is inheritance.
• Inheritance allows us to define a class in terms of another
class, which makes it easier to create and maintain an
application.
derived Class: The class that inherits the other class is known
as subclass(or a sub class, extended class, or child class). The
subclass can add its own fields and methods in addition to the
• Single Inheritance:
• Multilevel Inheritance:
8
Single C# Inheritance
• In single inheritance, subclasses inherit the features of one
superclass. In image below, the class A serves as a base class
for the derived class B.
17
Hybrid C# Inheritance
• 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.
21
UnBoxing
• It is the process of converting back the reference type into
the value type.
• This process verifies that the receiving data type is
equivalent to the boxed type as;
22
•
Generic
In c#, generic is a type that is used to define
a class, structure, interface or method with placeholders
(type parameters) to indicate that they can store or use one
or more of the types.
• A generic type is a generic class or interface that is
parameterized over types.
• Essentially, generic types allow you to write a general,
24
Generic Class
The following table describes the core class types of this
namespace.
26
Generic Class (Example)
using System;
using System.Collections.Generic; class Program
{
namespace GenericApp static void Main(string[] args)
{ {
public class TestClass<T> //instantiate generic with Integer
{ TestClass<int> intObj = new TestClass<int>();
// define an Array of Generic type with length 5
T[] obj = new T[5]; //adding integer values into collection
int count = 0; intObj.Add(1);
intObj.Add(2);