0% found this document useful (0 votes)
27 views30 pages

Lecture 10 - Inheritance

The document discusses visual programming using Microsoft Visual C# and Visual Studio. It provides an overview of the course content which includes introductions to Visual C#, programming language constructs, methods, decision and iteration statements, classes, collections, interfaces, inheritance, databases and LINQ queries, user interfaces with XAML, multitasking, and Windows Presentation Foundation applications. It also discusses key concepts like inheritance in C# including syntax, types (single, multilevel, hierarchical), and examples.
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)
27 views30 pages

Lecture 10 - Inheritance

The document discusses visual programming using Microsoft Visual C# and Visual Studio. It provides an overview of the course content which includes introductions to Visual C#, programming language constructs, methods, decision and iteration statements, classes, collections, interfaces, inheritance, databases and LINQ queries, user interfaces with XAML, multitasking, and Windows Presentation Foundation applications. It also discusses key concepts like inheritance in C# including syntax, types (single, multilevel, hierarchical), and examples.
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/ 30

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

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

Department of CSE, QUEST


Dr. Irfana Memon
Using Compound Assignment and Iteration Statements
Managing Errors and Exceptions
Implementing Type-safe Collections: Creating Classes, Organizing Data into
Collections, Handling Events, Defining and Implementing Interfaces
Developing the Code for a Graphical Application: Implementing Structs and Enums
Creating a Class Hierarchy by Using Inheritance, Extending .NET Framework
Classes, Creating Generic Types
Using Arrays and Collections and Understanding Parameter Arrays
2
Using Garbage Collection and Resource Management
Course Content (2)
Accessing a Database: Creating and Using Entity Data Models, Querying and Updating
Data by Using LINQ
Designing the User Interface for a Graphical Application: Using XAML, Binding
Controls to Data, Styling a User Interface
Improving Application Performance and Responsiveness: Implementing Multitasking
by using Tasks and Lambda Expressions

Department of CSE, QUEST


Dr. Irfana Memon
Performing Operations Asynchronously, Synchronizing Concurrent Access to Data
Building Windows Presentation Foundation Applications : Introducing Windows
Presentation Foundation, Gathering User Input, Performing Validation.
Managing Data: Querying Information in a Database, Displaying and Editing Data by
Using the Entity Framework and Data Binding,
Building Professional Solutions with Visual Studio 2010: Introducing the Task Parallel
Library, Performing Parallel Data Access, Creating and Using a Web Service

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.

Department of CSE, QUEST


Dr. Irfana Memon
• This also provides an opportunity to reuse the code
functionality and speeds up implementation time.
• When creating a class, instead of writing completely new
data members and member functions, the programmer can
designate that the new class should inherit the members of
an existing class.
• This existing class is called the base class, and the new
class is referred to as the derived class. 4
C# Inheritance (Terminology)
Base Class: The class whose features are inherited is known as
base class(or a super class or a parent class).

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

Department of CSE, QUEST


Dr. Irfana Memon
superclass fields and methods.

Reusability: Inheritance supports the concept of “reusability”,


i.e. when we want 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. 5
C# Inheritance Syntax
• Following is the syntax of implementing an inheritance to
define a derived class that inherits the properties of base
class in c# programming language.

<access_modifier> class <base_class_name>


{

Department of CSE, QUEST


Dr. Irfana Memon
// Base class Implementation
}

<access_modifier> class <derived_class_name> : <base_class_name>


{
// Derived class implementation
}

If you observe the above syntax, we are inheriting the 6


properties of base class into child class to improve code
reusability.
C# Inheritance (Example)
public class X
{
public void GetDetails()
{ If you observe the example, we defined a
// Method implementation class “X” with the method called
} “GetDetails” and the class “Y” is inheriting
} from class “X”. After that, we are calling a
public class Y : X

Department of CSE, QUEST


Dr. Irfana Memon
“GetDetails” method by using an instance of
{ derived class “Y”.
// your class implementation
}
class Program
{
static void Main(string[] args)
{
Y y = new Y();
y.GetDetails();
7
}
}
Types of C# Inheritance
• Below are the different types of inheritance which is
supported by C# in different combinations.

• Single Inheritance:
• Multilevel Inheritance:

Department of CSE, QUEST


Dr. Irfana Memon
• Hierarchical 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.

Department of CSE, QUEST


Dr. Irfana Memon
9
Single C# Inheritance (Example)
using System; . class Father
using System.Collections.Generic; {
using System.Linq; public void Display() {
using System.Text; Console.WriteLine("Display");
}
namespace MyAplication }
{
class Demo

Department of CSE, QUEST


Dr. Irfana Memon
{ class Son : Father
static void Main(string[] args) {
{ public void DisplayOne() {
// Father class Console.WriteLine("DisplayOne");
Father f = new Father(); }
f.Display(); }
// Son class }
Son s = new Son(); }
Output
s.Display();
s.DisplayOne(); Display 10
Console.ReadKey(); Display
} DisplayOne
Multilevel C# Inheritance
• In Multilevel Inheritance, a derived class will be inheriting a
base class and as well as the derived class also act as the
base class to other class.
• In below image, class A serves as a base class for the derived
class B, which in turn serves as a base class for the derived
class C.

Department of CSE, QUEST


Dr. Irfana Memon
11
Multilevel C# Inheritance
(Example)
using System;
namespace Studytonight
{
public class Grandparent
{
public Grandparent()

Department of CSE, QUEST


Dr. Irfana Memon
{
Console.WriteLine("Constructor called at run-time");
}
public void DisplayGrandParentsAB()
{
Console.WriteLine("A and B are my grandparents");
}
}
12
Multilevel C# Inheritance
public class Parents: Grandparent
{
(Example)
public void DisplayParentsCD()
{
Console.WriteLine("C and D are my parents");
}
}
public class Child: Parent
{
public void DisplayChildZ()

Department of CSE, QUEST


Dr. Irfana Memon
{
Console.WriteLine("I am the child Z");
}
}
public class Program
{
public static void Main(string[] args)
{
child cd = new Child();
cd.DisplayChildZ(); 13
cd.DisplayParentsCD();
cd.DisplayGrandParentsAB();
}}}
Hierarchical C# Inheritance
• In Hierarchical Inheritance, one class serves as a superclass
(base class) for more than one subclass. In below image,
class A serves as a base class for the derived class B, C, and
D.

Department of CSE, QUEST


Dr. Irfana Memon
14
Hierarchical C# Inheritance
(Example)
public class ChildD: Parent
{
using System;
public void DisplayChildD()
namespace Studytonight
{
{
Console.WriteLine("I am the child D");
public class Parent
}
{
}
public void DisplayParentsAB()

Department of CSE, QUEST


Dr. Irfana Memon
public class Program
{
{
Console.WriteLine("A and B are my
public static void Main(string[] args)
parents");
{
}
ChildC cc = new ChildC();
}
ChildD cd = new ChildD();
public class ChildC: Parent
cc.DisplayChildC();
{
cc.DisplayParentsAB(); // accessing parent
public void DisplayChildC()
class
{
cd.DisplayChildD(); 15
Console.WriteLine("I am the child C");
cd.DisplayParentsAB(); //accessing parent
}
class
}
}}}
hierarchical C# Inheritance
(Example)
public class ChildD: Parent
{
using System;
public void DisplayChildD()
namespace Studytonight
{
{
Console.WriteLine("I am the child D");
public class Parent
}
{
}
public void DisplayParentsAB()

Department of CSE, QUEST


Dr. Irfana Memon
public class Program
{
Output: {
Console.WriteLine("A and B are my
public static void Main(string[] args)
parents"); I am the child C {
}
} A and B are my ChildC
parentscc = new ChildC();
am the child DChildD cd = new ChildD();
public class ChildC: IParent
cc.DisplayChildC();
{ A and B are my parents
cc.DisplayParentsAB(); // accessing parent
public void DisplayChildC()
class
{
cd.DisplayChildD(); 16
Console.WriteLine("I am the child C");
cd.DisplayParentsAB(); //accessing parent
}
class
}
}}}
Multipl C# Inheritance (through
Interface)
• In Multiple inheritance, one class can have more than one
superclass and inherit features from all parent classes.
Please note that C# does not support multiple
inheritance with classes. In C#, we can achieve multiple
inheritance only through Interfaces. In the image below,

Department of CSE, QUEST


Dr. Irfana Memon
Class C is derived from interface A and 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.

Department of CSE, QUEST


Dr. Irfana Memon
18
Important fact about C#
Inheritance
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.

Department of CSE, QUEST


Dr. Irfana Memon
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#..
19
Important fact about C#
Inheritance
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

Department of CSE, QUEST


Dr. Irfana Memon
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
inherit.
20
Boxing
• .Net defines two major categories of data type termed
value type and reference type to represent a variable.
• This is where boxing and unboxing are needed.
• Boxing is a mechanism to explicitly convert a value type to
a reference type by storing the variable into System.Object;

Department of CSE, QUEST


Dr. Irfana Memon
when you box the value the CLR allocates a new object into
the heap and copies the value type's value into that
instance.
• For example you have created a variable of int type as:
int a = 20;
object b = a; //boxing

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;

Department of CSE, QUEST


Dr. Irfana Memon
int c = (int)b; // unboxing

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,

Department of CSE, QUEST


Dr. Irfana Memon
generic class (or method) that works with different types,
allowing for code re-use.
• Rather than specifying obj to be of an int type, or
a String type, or any other type, you define the Box class to
accept a type parameter <;T>. Then, you can use T to
represent that generic type in any part within your class.
• The detailed specification for each collection is found under 23
the System.Collection.Generic namespace.
Generic Class
• The Generic class can be defined by putting the <T> sign
after the class name.
• It isn't mandatory to put the "T" word in the Generic type
definition.
• You can use any word in the TestClass<> class declaration.

Department of CSE, QUEST


Dr. Irfana Memon
public class TestClass<T> { }

• The System.Collection.Generic namespace also defines a


number of classes that implement many of these key
interfaces.

24
Generic Class
The following table describes the core class types of this
namespace.

Generic class Description


The basis for a generic collection
Collection<T> Comparer compares two generic

Department of CSE, QUEST


Dr. Irfana Memon
objects for equality
A generic collection of name/value
Dictionary<TKey, TValue>
pairs
List<T> A dynamically resizable list of Items
A generic implementation of a first-
Queue<T>
in, first-out (FIFO) list
A generic implementation of a last-
Stack<T>
in, first-out (LIFO) list
25
Generic Class (Example)
• The example program (next slide) shows a simple Generic
type manipulation.
• The TestClass<T> defines an array of generic type with
length 5.
• The Add() method is responsible for adding any type of
objects into the collection and the Indexer property is an

Department of CSE, QUEST


Dr. Irfana Memon
implementation of foreach statement iteration.
• Finally in the main class we instantiated the TestClass<T>
class with an Integer type reference and adds some integer
type elements into the collection using the Add() method.

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

Department of CSE, QUEST


Dr. Irfana Memon
// adding items mechanism into generic type intObj.Add(3); //No boxing
public void Add(T item) intObj.Add(4);
{ intObj.Add(5);
//checking length
if (count + 1 < 6) //displaying values
{ for (int i = 0; i < 5; i++)
obj[count] = item; {
Console.WriteLine(intObj[i]); //No unboxing
} }
count++; Console.ReadKey();
} }
//indexer for foreach statement iteration }
public T this[int index] }
{
get { return obj[index]; } 27
set { obj[index] = value; }
}
}
Characteristics of Generic Class
• Generics is a technique that improves your programs in
many ways such as:
 It helps you in code reuse, performance and type safety.
 You can create your own generic classes, methods,
interfaces and delegates.

Department of CSE, QUEST


Dr. Irfana Memon
 You can create generic collection classes. The .NET
framework class library contains many new generic
collection classes in System.Collections.Generic
namespace.
 You can get information on the types used in generic data
type at run-time.
28
Advantages of Generic Class
Reusability: You can use a single generic type definition for
multiple purposes in the same code without any alterations. For
example, you can create a generic method to add two numbers.
This method can be used to add two integers as well as two
floats without any modification in the code.
Type Safety: Generic data types provide better type safety,

Department of CSE, QUEST


Dr. Irfana Memon
especially in the case of collections. When using generics you
need to define the type of objects to be passed to a collection.
This helps the compiler to ensure that only those object types
that are defined in the definition can be passed to the
collection.
Performance: Generic types provide better performance as
compared to normal system types because they reduce the 29
need for boxing, unboxing, and typecasting of variables or
objects.
Wish You Good Luck

Dr. Irfana Memon


30

Department of CSE, QUEST

You might also like