0% found this document useful (0 votes)
37 views44 pages

Lecture3-CS6004 Application Development

The document discusses object oriented programming concepts including classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It provides examples and explanations of each concept.

Uploaded by

Dilru Dayananda
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)
37 views44 pages

Lecture3-CS6004 Application Development

The document discusses object oriented programming concepts including classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It provides examples and explanations of each concept.

Uploaded by

Dilru Dayananda
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/ 44

CS6004

Application
Development

Lecture 3

Chamila Karunatilake
[email protected]

1
Outline

• Introduction to Object Oriented Programming


• Objects and Classes
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
2
What is Object Oriented Programming?
• Procedural programming is about writing procedures or methods that
perform operations on the data. (C programming Language)
• Object-oriented programming(OOP) is about working with objects that
contain both data and methods.
Object-oriented programming has several advantages over procedural
programming:
– OOP is faster and easier to execute
– OOP provides a clear structure for the programs
– OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug
– OOP makes it possible to create full reusable applications with less code
and shorter development time
3
Objects
• Object is an entity that has data and some actions on that data.
• Let’s take Person objects.

Clark Lois Ann Jack John


28 25 20 37 40
Engineer Editor Model Pirate Manager

4
Classes
• Class is a definition of a set of objects that can be categorized into one
type.
Person
Name
Age Properties
Occupation

Self Introduction Methods

A class is a template for objects, and an object is an


instance of a class. 5
Classes
• When we practically write the programs, first we
define the class.
• Once a class is defined, we can create objects of that
class. Person
Name
Age
Occupation
Self Introduction

6
Classes

7
Objects

8
Encapsulation

• Encapsulation is sometimes referred to as data hiding.


• A class can specify how accessible each of its members is to code
outside of the class.
• Properties and methods that aren't intended to be used from outside of
the class or assembly can be hidden to limit the potential for coding
errors or malicious exploits.
• You specify how accessible your classes and their members are to other
codes by using the access modifiers.

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

• A constructor is a special method that is used to


initialize objects.
• The advantage of a constructor, is that it is called when
an object of a class is created.
• It can be used to set initial values for fields.
• Every class has a default constructor with no
parameters, and you don’t have to define it explicitly.
• If you define additional constructors, then default
constructor should also be defined explicitly.
14
Constructors

this keyword refers to the current


instance of the class.
15
Constructors

16
Inheritance

• In C#, it is possible to inherit fields and methods from one class to


another.
• We group the "inheritance concept" into two categories:
– Derived Class (child) - the class that inherits from another class
– Base Class (parent) - the class being inherited from
• To inherit from a class, use the : symbol.

17
Inheritance

18
Inheritance

19
Inheritance

20
Inheritance

21
Inheritance

22
Polymorphism

• Polymorphism is a Greek word meaning "one name many forms." "Poly"


means many, and "morph" means forms.
• In other words, one object has many forms or has one name with
multiple functionalities.
• Polymorphism allows a class to have multiple implementations with the
same name.
• It is one of the core principles of Object-Oriented Programming after
encapsulation and inheritance.

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.

Dynamic Polymorphism (Runtime Polymorphism)


• Dynamic/runtime polymorphism is also known as late binding.
• Here, the method name and the method signature (the number of
parameters and parameter type must be the same and may have a
different implementation).
• Method overriding is an example of dynamic polymorphism. 25
Static / Compile-time Polymorphism
• A same class can have multiple methods with the same name, but
different method signature.

public int Add(int a,int b)

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

Result is the same as previous

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

• In the main method, different objects are invoked.


• There is a reference variable of Shape type, which is s2.
• Then, s2 invokes the method according to the specific class.
• At compile time, the compiler will only refer the super class
calculateArea method.
• When actual execution begins, it will lead to the execution of different
calculateArea methods.

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

There are some important points about Abstract Base Class.


• An Abstract Base class cannot be instantiated; it means the object of
that class cannot be created.
• The class having the abstract keyword with some of its methods (not all)
is known as an Abstract Base Class.
• The class having the Abstract keyword with all its methods is a pure
Abstract Base Class.
• It can be defined as an abstract void method ();
• An abstract class holds the methods, but the actual implementation is
made in the derived class.
40
Abstraction

41
Interfaces

• Another way to achieve abstraction in C#, is with


interfaces.
• An interface is a completely "abstract class", which can
only contain abstract methods and properties (with
empty bodies).
• By using interfaces, you can, for example, include
behavior from multiple sources in a class.
• That capability is important in C# because the language
doesn't support multiple inheritance of classes.
42
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

You might also like