Lecture3-CS6004 Application Development
Lecture3-CS6004 Application Development
Application
Development
Lecture 3
Chamila Karunatilake
[email protected]
1
Outline
4
Classes
• Class is a definition of a set of objects that can be categorized into one
type.
Person
Name
Age Properties
Occupation
6
Classes
7
Objects
8
Encapsulation
9
Access modifiers
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class that is inherited
from that class.
internal The code is only accessible within its own assembly, but not from another
assembly.
protected A protected internal member is accessible from the current assembly or
internal from types that are derived from the containing class.
private A private protected member is accessible by types derived from the
protected containing class, but only within its containing assembly.
10
Encapsulation
11
Encapsulation
12
Encapsulation
Full Properties
Expression
Auto Properties Bodied
Properties
13
Constructors
16
Inheritance
17
Inheritance
18
Inheritance
19
Inheritance
20
Inheritance
21
Inheritance
22
Polymorphism
23
Polymorphism
There are two types of polymorphism in C#:
– Static Polymorphism (Compile Time Polymorphism)
– Dynamic Polymorphism (Runtime Polymorphism)
24
Polymorphism
Static Polymorphism (Compile-Time Polymorphism)
• Method overloading is an example of Static polymorphism.
• Overloading is the concept in which method names are the same with
different parameters.
• The method/function has the same name but different signatures in
overloading. It is also known as Early binding.
Method Signature
26
Dynamic / Runtime Polymorphism
27
Dynamic / Runtime Polymorphism
28
29
Dynamic / Runtime Polymorphism
• When you build the project, you see that the addition of the
CalculateArea method in Circle causes a warning.
• The warning says that the CalculateArea method in Circle hides the
Method2 method in Shape.
• You are advised to use the new keyword in the CalculateArea definition
if you intend to cause that result.
• In either case, with or without new keyword, the derived class method
hides the base class method. (Method Hiding)
30
Dynamic / Runtime Polymorphism
31
Dynamic / Runtime Polymorphism
• If you want to override the implementation of the base class, you have
to do two things.
– Use virtual modifier in the base class method.
– Use override modifier in the child class method.
32
Dynamic / Runtime Polymorphism
33
Dynamic / Runtime Polymorphism
34
Dynamic / Runtime Polymorphism
• If you are not desired overriding a method, you can simply remove the
virtual modifier.
• If you have a method which is overridden, you can use sealed modifier
to prevent it from overriding further.
35
Dynamic / Runtime Polymorphism
36
Dynamic / Runtime Polymorphism
37
Abstraction
• The word abstract means a concept, or an idea not associated with any
specific instance.
• In C# programming, we apply the same meaning of abstraction by
making classes not associated with any specific instance.
• Abstraction is needed when we only inherit from a certain class but do
not need to instantiate objects of that class.
• In such a case, the base class can be regarded as "Incomplete".
• Such classes are known as an Abstract Class.
38
Abstraction
39
Abstraction
41
Interfaces
43
Interfaces
• Like abstract classes, interfaces cannot be used to create objects (in the
example above, it is not possible to create an "IPetable" object in the
Program class)
• Interface methods do not have a body - the body is provided by the
"implement" class
• On implementation of an interface, you must override all of its methods
• Interfaces can contain properties and methods, but not fields/variables
• Interface members are by default abstract and public
• An interface cannot contain a constructor (as it cannot be used to create
objects)
44