0% found this document useful (0 votes)
15 views

PRG Chapter 3

The document discusses object oriented programming concepts including encapsulation, inheritance and polymorphism. It defines each concept and provides examples to illustrate how they are implemented in C# code. The document also includes code challenges for readers to practice applying the concepts.

Uploaded by

Redsadow 238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

PRG Chapter 3

The document discusses object oriented programming concepts including encapsulation, inheritance and polymorphism. It defines each concept and provides examples to illustrate how they are implemented in C# code. The document also includes code challenges for readers to practice applying the concepts.

Uploaded by

Redsadow 238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 3

Getting Started with OOP


Module Objectives

1.Introduction to Object Oriented Programming

2.OOP in a PIE

3.Encapsulation

4.Inheritance

5.Polymorphism
Introduction to Object Oriented Programming
• Object oriented programming (OOP) is a software design
technique that helps to organize data and methods in a single
4.Inheritance
object. It helps objects to talk to each other by defining
relationships among them.
5.Polymorphism

• Steve Jobs: “Objects are like people . They’re living, breathing


things that have knowledge inside them about how to do
things and have memory inside them so they can remember
things. And rather than interacting with them at a very low level,
you interact with them at a very high level of abstraction , like
we’re doing right here.”
OOP in a PIE

• In a nutshell OOP has three fundamental pillars:


Polymorphism, Inheritance and Encapsulation (PIE)
• Encapsulation refers to an object-oriented design principle
called Data Hiding, which restricts the accessibility of data
(attribute) and method (behavior) of an entity that are not
necessary to its user.

• Encapsulation is implemented through two ways:


1.Access Specifiers
2.Data Protection
Access Specifiers
• In C#, we have five kinds of access specifiers.
1.Public: Members defined with public are accessible within the
class as well as outside the class.
2.Private: Members defined with private are only accessible
within the class and they cannot be accessed from outside the
class.
3.Protected: Members defined with protected are accessible
within the class and also within its child classes. They cannot be
accessible from outside the class.
4.Internal: members defined with internal are accessible within
the class as well as outside the class. But they are not accessible
to any class which is defined outside the project’s assembly.
5.Protected Internal: members defined with internal protected
are accessible within as well as outside the class and also to its
child classes.
Data Protection
• In C#, data is stored in a single variable or in an array. To
protect this data from accidental damage, we have:
1. Properties
2. Indexers
• Properties are used to encapsulate the value of a private field.
• Properties are of two kinds:
1. Full Property
2. Auto Property
• In full property, we declare private fields and encapsulate
them inside a property’s definition.
Full Property
Auto Property

• Auto property is a lot like full property. The only key difference
is, it doesn’t require any field or extra logic in its get and set to
manipulate values
Indexers
• Indexers are used to encapsulate the value of an array. It
behaves and works like property. It also uses access specifiers,
which give better control to read, write, or manipulate an array’s
value. It creates a sandbox over an array, which protects it from:
1. saving false data in an array;
2. using the wrong index value in an array;
3. changing the reference of an array from the outer world.
Indexers Example
Inheritance
• Inheritance is one of the three fundamental pillars of object-
oriented programming. It allows new classes to reuse or inherit
properties and methods from an existing class.
• The class whose members are inherited is called base class and
the class which inherited those members is called derived class
• In C#, a class cannot inherit from multiple classes, but it can be
inherited at multiple levels.
• A class can implement multiple interfaces at the same time.
Abstract Class
• Abstract classes cannot be instantiated. It is used as base
class, where it provides common members to all its derived
classes. It is either overridden partially or not at all. It is also
used to declare abstract methods (method without definition)
that when it inherits, it must be overridden by its derived
classes.
Interface

• Interface cannot be instantiated. Its members have no


implementation detail. All implementation detail is defined
by classes which implement (inherit) interfaces. Interface
provides the highest level of abstraction.

• Key Points
1. Do not use access specifiers with interface’s members.
2. Do not define definition of interface members.
3. Auto-property, indexer, method, and event can be used as
a member of an interface.
4. Class must implement full definition of interface’s
members. Otherwise error may occur at compile/run time.
5. Class can implement more than one interface.
Polymorphism
• Polymorphism is all about changing behaviors; in other
words, it is different forms of one object.
• In C#, polymorphism is of two types:
1. Static Polymorphism
2. Dynamic Polymorphism
• In C#, static polymorphism can be implemented with two
ways:
1. Method Overloading
2. Operator Overloading
In C#, dynamic polymorphism means changing behavior of
an object at runtime by overriding a method and can be
implemented with two ways:
3. Virtual method
4. Abstract method
Polymorphism
1. Method Overloading

2. Operator Overloading
Static Type
Code Challenges
• Challenge 1: Develop a Transformer
Code Challenges
• Challenge 2: Develop Steve Jobs Laundry System
PRG521
Main | CH3
Heading | Subheading

The End

2 0 2 32 0C2T0U CTTr U
a i nTirnagi nSi nogl u St ioolnust i o|n sA l |l RAi gl l hRt si gRhet s eRr ev es edr v |e dc t u| t rcat iuntirnagi n. ai nc g
. z. a c . z a

You might also like