Machine Translated by Google
Event driving Programming
Object-Oriented in C#
7/19/202 3
Event driving Programming by(C#) by M.Ou 1
Machine Translated by Google
Inheritance in C#
ÿ Outline
ÿ Why inheritance
ÿ Advantages of inheritance
ÿ Syntax of inheritance
ÿ Inheritance concepts.
7/19/202
Event driving Programming by(C#) by M.Ou 2
3
Machine Translated by Google
Inheritance in C#
7/19/202
Event driving Programming by(C#) by M.Ou 3
3
Machine Translated by Google
Inheritance…
7/19/202
Event driving Programming by(C#) by M.Ou 4
3
Machine Translated by Google
Inheritance…
ÿPillars of object oriented programming
ÿ Inheritance
ÿ Encapsulation
ÿ Abstraction
ÿ Polymorphism
ÿ Why Inheritance ?
1. Inheritance is one of the pillars of object
oriented
programming 2. .
It allows code
reuse 3. Code reuse can reduce time and errors Note: You
will specify the common fields, properties, methods
7/19/202
Event driving Programming by(C#) by M.Ou
in the base class, which allows reusability. In the
5
3
Inheritance in C#...
Machine Translated by Google
In this example
1.DerivedClass inherits from Parent Class
2. C# supports only single inheritance
3. C# supports multiple interface inheritance
4. Child class is specialization of Base class
5. Base class are automatically instantiated
before derived classes
6.Parent class Constructor executes before child class
Event driving Programming by(C#) by M.Ou
7/19/202
3
6
constructor
Machine Translated by Google
Method Hiding in C#
ÿ Method Hiding in C#
ÿ Method hiding and Invoking hidden base class
members Inheritance concepts
ÿ Use the new keyword to hide a base class member.
You will get a compiler warning, if you miss the
new keyword. Different ways to invoke a hidden
base class member from derived class.
1. Use base keyword
2. Cast child type to parent type and invoke the hidden
member
3. ParentClass pc =new ParentClass()
pc.HiddenMethod()
7/19/202
Event driving Programming by(C#) by M.Ou 7
3
Machine Translated by Google
Method hiding Program
7/19/202
Event driving Programming by(C#) by M.Ou 8
3
Machine Translated by Google
Polymorphism in C#
ÿ Overriding virtual methods and Polymorphism
ÿ Polymorphism: ÿ
is one the primary pillars of object oriented programming.
ÿ Allows to invoke derived class methods through
a base class reference during runtime
ÿ In the base class the method is declared virtual and in the
derived class we override same method
ÿ The virtual keyword indicates, the method can be
overridden in any derived class.
7/19/202
Event driving Programming by(C#) by M.Ou 9
3
Machine Translated by Google
Polymorphism in C#...
7/19/202
Event driving Programming by(C#) by M.Ou 10
3
Machine Translated by Google
Polymorphism in C#...
7/19/202 3
11
Event driving Programming by(C#) by M.Ou
Machine Translated by Google
Polymorphism in C#...
7/19/202
Event driving Programming by(C#) by M.Ou 12
3
Machine Translated by Google
Method Hiding Vs.
Method Hiding Vs. Overriding Overriding
ÿ In method overriding a base class reference variable
pointing to a child class object, will invoke the
overridden method in the child class.
ÿ In method hiding a base class reference variable
pointing to a child class object, will invoke the
hidden method in the base class.
From the Examples below
bc.Print(): invokes the overridden print() method in the
child class
bc.Print(): invokes the hidden print() method in the
base class
Event driving Programming by(C#) by M.Ou
7/19/202
3
13
Machine Translated by Google
Method Hiding Vs. Overriding…
7/19/202
Event driving Programming by(C#) by M.Ou 3
14
Machine Translated by Google
Method Hiding Vs. Overriding…
7/19/202
Event driving Programming by(C#) by M.Ou 3
15
Machine Translated by Google
Method Overloading…
ÿ Function overloading and Method overloading terms are
used interchangeably.
ÿ Method overloading allows a class to have multiple
methods with same name, but with a different signature.
So in C# functions can be overloaded based on
the number, type (int, float etc) and kind (value, Ref
or out) of
parameters. ÿ The signature of the method consists of
the name of the method and the type, kind
(value ,reference or output) and the number of its
formal parameters. The signature of a method
does not include the return type and the
paramsmodifier. So, it is not possible to overload a function,
modifier.
Event driving Programming by(C#) by M.Ou
7/19/202
3
16
ÿ Look an example from the following slides.
Machine Translated by Google
Method Overloading…
7/19/202
Event driving Programming by(C#) by M.Ou 3
17
Machine Translated by Google
Method Overloading…
7/19/202
Event driving Programming by(C#) by M.Ou 18
3
Machine Translated by Google
Properties in C#
ÿ Why Properties
ÿ The need of properties
ÿ Marking the class fields as public and exposing
to the external world is bad, as you will not
have control over what gets assigned and returned.
7/19/202
Event driving Programming by(C#) by M.Ou 19
3
Machine Translated by Google
Getter and setter in C#
ÿ Getter and Setter Methods Programming languages that
does not support properties use getter and methods to
encapsulate and protect fields.
7/19/202
Event driving Programming by(C#) by M.Ou 20
3
Machine Translated by Google
Properties in C#...
ÿ Properties in C#
ÿ In this session we will learn
ÿ Read/Write properties ÿ
Read only properties ÿ
Write only properties ÿ
Auto implemented properties
7/19/20
Event driving Programming by(C#) by M.Ou 21
23
Machine Translated by Google
Properties in C#...
ÿ In C# to encapsulate and protect fields we use
properties
1. We use get and set accessors to implements properties
2. A property with both set and get accessor is a
Read/Write property
3. A property with only get accessor is a Read only
property
4. A property with only set accessor is a Write only
property
ÿ Note: the advantage of properties over traditional
Get() and Set() methods is that, you can access them as
if they were public fields
7/19/202
Event driving Programming by(C#) by M.Ou 22
3
Machine Translated by Google
Properties in C#
7/19/202
Event driving Programming by(C#) by M.Ou 3
23
Machine Translated by Google
Properties in C#...
7/19/202 3
Event driving Programming by(C#) by M.Ou 24
Machine Translated by Google
Structures in C#
ÿ In this session we will learn about structs Just like classes structs can
have
1. Private fields
2. Public properties
3. Constructors
4. Methods
Object initializer syntax, introduced in C# 3.0 can be used initialize either
a class or a struct.
7/19/202
Event driving Programming by(C#) by M.Ou 25
3
Machine Translated by Google
Structures in C#...
7/19/202 3
Event driving Programming by(C#) by M.Ou 26
Machine Translated by Google
Structure in C#...
7/19/202
Event driving Programming by(C#) by M.Ou 3
27
Machine Translated by Google
Struct vs Class…
ÿ We understood that structs are very similar to classes. We use struct keyword
to create a struct.
ÿ Just like classes structs can have fields, properties, constructors and methods.
ÿ How ever there are several differences classes and structs, which we will
explore on this session. A struct is a value type where as a class is a reference
type.
ÿ All the differences that are applicable to value types and reference types are
also applicable to classes and structs.
ÿ Structs are stored on stack, where as classes stored on the heap.
ÿ Value types hold their value in memory where they are declared, but reference
types hold a reference to an object in memory
7/19/202
Event driving Programming by(C#) by M.Ou 28
3
ÿ Value types destroyed immediately after the scope is lost, where as for
Machine Translated by Google
Struct…
1) Structs can’t have destructors, where as classes can have
destroyers
2) Structs can’t have explicit parameter less constructor where as
class can.
3) Struct can’t inherit from another class where as a class can,
both structs and classes can inherit from interface.
4) Examples of structs in .NET framework- int, double Note 1: A
class or a struct cannot inherit from another struct.
5) Struct are sealed types. Note 2:To prevent a class from being
inherited use the sealed modifier. 7/19/202
Event driving Programming by(C#) by M.Ou 29
3
Machine Translated by Google
Interface…
ÿ Interface basics We create interfaces using interfacekeyword.
Just like classes interfaces also contains properties, methods,
delegates or events, but only declarations and no
implementations.
ÿ It is a compile time error to provide implementations for any
interface member.
ÿ Interface members are public by default, and they don’t allow
explicit access modifiers.
ÿ Interfaces cannot contain fields.
ÿ If a class or a struct inherits from an interface, it must
Event driving Programming by(C#) by M.Ou
7/19/202
30
3
provide implementation for all interface members.
Machine Translated by Google
Abstract Class in C#…
ÿ Abstract Classes
ÿ The abstract keyword is used to create abstract
classes.
ÿ An abstract class is incomplete and cannot be
instantiated.
ÿ An abstract class can only be used as base class.
ÿ An abstract class cannot be sealed (closed).
ÿ An abstract class may contain abstract 7/19/202
Event driving Programming by(C#) by M.Ou 31
3
members(methods, properties, indexers, and
Machine Translated by Google
Abstract Class in C#…
ÿ If a class inherits an abstract class, there are 2
options available for that class Option 1: Provide
Implementation for all the abstract members
inherited from the base abstract class. Option 2: If
the class does not wish to provide implementation
for all the abstract members inherited from the
abstract class, then the class has to be marked as
abstract
7/19/202
Event driving Programming by(C#) by M.Ou 32
3
Machine Translated by Google
Abstract Class vs interface
ÿ Abstract classes can have implementations for some of its members
(methods), but the interface can’t have implementation for any of its
members.
ÿ Interfaces cannot have fields where as an abstract class can have fields.
ÿ An interface can inherit from another interface only and cannot inherit
from an abstract class, where as an abstract class can inherit from another
abstract class or another interface.
7/19/202
Event driving Programming by(C#) by M.Ou 33
3
Machine Translated by Google
Con’t…
ÿ A class can inherit from multiple interfaces at the
same time, where as a Abstract cannot inherit from
multiple classes at the same time.
ÿ Abstract class members can have access modifiers
where as interface members cannot have access
modifiers. A non-abstract class derived from an
abstract class must provide implementations for all
inherited abstract members.
7/19/202
Event driving Programming by(C#) by M.Ou 34
3
Machine Translated by Google
Abstract Class vs interface
ÿ Abstract classes can have implementations for some of its members
(methods), but the interface can’t have implementation for any of its
members.
ÿ Interfaces cannot have fields where as an abstract class can have
fields.
ÿ An interface can inherit from another interface only and cannot
inherit from an abstract class, where as an abstract class can inherit
from another abstract class or another interface.
7/19/202
Event driving Programming by(C#) by M.Ou 35
3
Machine Translated by Google
Con’t…..
ÿ A class can inherit from multiple interfaces at the
same time, where as a class cannot inherit from
multiple classes at the same time.
ÿ Abstract class members can have access modifiers
where as interface members cannot have access
modifiers. A non-abstract class derived from an
abstract class must provide implementations for all
inherited abstract members.
7/19/202
Event driving Programming by(C#) by M.Ou 36
3
Machine Translated by Google
Delegates
ÿ Delegates
ÿ A delegate is a type safe function pointer. That is, it holds
a reference (pointer) to a function.
ÿ The signature of the delegate must match the signature of
the function, the delegate points to, otherwise you get a
compiler error. This is the reason delegates are called as
type safe function pointers.
7/19/202
Event driving Programming by(C#) by M.Ou 37
3
Machine Translated by Google
ÿ A Delegate is similar to a class. You can create an
instance of it, and when you do so, you pass in the
function name as a parameter to the delegate
constructor, and it is to this function the delegate
will point to.
ÿ Tip to remember delegate syntax: Delegates syntax
look very much similar to a method with a delegate
keyword.
7/19/202
Event driving Programming by(C#) by M.Ou 38
3
Machine Translated by Google
Delegates
7/19/202
Event driving Programming by(C#) by M.Ou 39
3
Machine Translated by Google
THANK YOU !!!
7/19/202 3
Event driving Programming by(C#) by M.Ou 40
Machine Translated by Google
Thank you!!!
7/19/202
Event driving Programming by(C#) by M.Ou 3
41