PRG Chapter 3
PRG Chapter 3
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
• 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
• 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