OOP Concepts
OOP Concepts
by Arwa Alaa
OOP PILLARS
by Arwa Alaa
Encapsulation
by Arwa Alaa
Encapsulation Definition
by Arwa Alaa
Problems Before Encapsulation
2. Can’t Make Code read only (private) (can’t use out class)
• solved by make public getter and private setter if you need use it in class else delete setter
1. Can’t Control Values (like validation) Solved by make validation on setter /property set
by Arwa Alaa
Getter & Setter
Old Approach
by Arwa Alaa
Getter & Setter (Old Approach )
In C#, getters and setters are methods used to access and modify the values of
private fields in a class. They are part of properties, which provide a way to
encapsulate data while controlling access to it.
by Arwa Alaa
Property
Modern Approach
by Arwa Alaa
Property (Modern Approach)
Named Rules
• Property Start with Capital Character And Have Sane Name Of Attribute
by Arwa Alaa
Property
Notes
• Property Not take Parameter
• Inside class or struct we deal with Attributes
• Outside class or struct we deal with Property
• Property by default must be public ,We can change property set or get as private
by Arwa Alaa
Types Of Property
Automatic
Full Property Indexer
Property
by Arwa Alaa
1.Full Property
▪ This allows you to control how the data is accessed and modified.
▪ Contain Keyword Reserved Named Value use to set value
by Arwa Alaa
2. Automatic Property (or auto-implemented property)
by Arwa Alaa
3.Indexer
• Allows an object to be indexed like an array. Used when abject contain arrays
by Arwa Alaa
Indexer Structure & Use
by Arwa Alaa
Property Notes
Inside Constructor
• If you used full property we initialize Attribute
• If you use automatic property we initialize Property
• If you make property for just set & get Automatic property is recommended
• If you need to control data (validation) Full Property Is recommended
by Arwa Alaa
Code Snippet For Property
by Arwa Alaa
What Is Constructor ?
by Arwa Alaa
Why Constructor is Special Function ?
by Arwa Alaa
Types of Constructor
1 2 3 4 5
Default Parameterized Static Copy Private
Constructor Constructor Constructor Constructor Constructor
by Arwa Alaa
Inheritance
by Arwa Alaa
What is Inheritance?
by Arwa Alaa
Key Concepts of Inheritance:
4. Overriding:
1. The derived class can modify or extend the functionality of methods inherited from
the base class by overriding them.
5.Constructors in Inheritance:
1. The constructor of the base class is called when an object of the derived class is
created.
2. The derived class can call the base class constructor explicitly using the base
keyword
by Arwa Alaa
Inheritance bref
• Important :Any function return type is parent type so this function can return Parent
type or child Type (like object (parent) ,classEmployee (child) (consider child as it
class inherit from object))
by Arwa Alaa
Polymorphism
by Arwa Alaa
What is
Polymorphism?
Polymorphism is composed of two
words - “poly” which means “many”,
and “morph”which means “forms”.
Therefore Polymorphism refers to
something that has many shapes.
This pillar makes the same entities
such as functions, and operators
perform differently in different
scenarios. You can perform the
same task in different ways.
by Arwa Alaa
Overloading
by Arwa Alaa
1.Overloading
by Arwa Alaa
Overloading
More than method /constructor / operator /indexer Have Same Name in class or
struct But different in :
1.Number Of parameters
2.Order Of Parameter
3.Type Of Parameter
by Arwa Alaa
Types of Overloading
1 2 3 4 5
Method Constructor Indexer Operator Casting
overloading overloading Overloading overloading operator
OverLoading
by Arwa Alaa
Overriding
by Arwa Alaa
2.Overriding
Notes
• Support in Class only as it work with inheritance
by Arwa Alaa
New vs. Override
In C#, a method in a derived class can have the same name as a method in the
base class.
You can specify how the methods interact by using the new and override keywords.
1. The override modifier extends the base class virtual method
by Arwa Alaa
1.Overriding Using New KeyWord
Method Hiding /Override uses the new keyword and creates a new method in the
derived class that hides the method with the same name in the base class.
1. New is used in method hiding /override( hide base class method )
2. New modifier can be added before or after public.
3. New Keyword Add New version for the function that inherited from base class
4. New Keyword stop (stop accessibility )that chain of inherited method (mean if
another class inherit from derived one that have method new the method can’t
override in inherited class but can use this function with new keyword in another
class inherit from derived one )
by Arwa Alaa
2.Overriding Using Override KeyWord
by Arwa Alaa
Abstraction
by Arwa Alaa
Abstraction
by Arwa Alaa
What are abstract classes in C#?
Abstract classes in C# are classes that cannot be instantiated, and they are
meant to be inherited by other classes. They are used to provide a common
definition of a base class that can be shared by multiple derived classes.
• We use the abstract keyword to create an abstract class.
• Abstract classes can have abstract and non-abstract methods and
properties.
• Derived classes must provide an implementation for these abstract
members.
by Arwa Alaa
Abstract Class
Abstract Class:
▪ is a partial implementation for another class will implement it
▪ can contain implemented and non implemented members
▪ You Can’t create object (instance) from abstract class because is Not Fully
Implemented class
When Need make class Is Abstract ?
1. If class not presented in business ,is just for container for common
2. If class contain not implemented members (methods , property)
by Arwa Alaa
Concrete Class Vs. Abstract Class
by Arwa Alaa
Abstract class
In class inherit from abstract class , Use override keyword to implement abstract
Method and Property
Implement abstract Property Implement abstract method
by Arwa Alaa
Stay Tuned Next Topic
by Arwa Alaa