[go: up one dir, main page]

0% found this document useful (0 votes)
18 views47 pages

OOP Concepts

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around 'objects', which are instances of classes, promoting modularity and reusability. The four pillars of OOP include encapsulation, inheritance, polymorphism, and abstraction, each serving to enhance code organization and functionality. Encapsulation bundles data and methods within classes, inheritance allows classes to inherit properties and behaviors, polymorphism enables entities to perform differently in various scenarios, and abstraction focuses on exposing essential details while hiding implementation specifics.

Uploaded by

abdoalsenaweabdo
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)
18 views47 pages

OOP Concepts

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around 'objects', which are instances of classes, promoting modularity and reusability. The four pillars of OOP include encapsulation, inheritance, polymorphism, and abstraction, each serving to enhance code organization and functionality. Encapsulation bundles data and methods within classes, inheritance allows classes to inherit properties and behaviors, polymorphism enables entities to perform differently in various scenarios, and abstraction focuses on exposing essential details while hiding implementation specifics.

Uploaded by

abdoalsenaweabdo
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/ 47

OOP

Object Oriented Programming


by Arwa Alaa
What Is OOP ?

Object-oriented programming (OOP) is a programming


paradigm that organizes software design based on the
concept of "objects", which are instances of classes to
create modular, reusable, and maintainable code.

by Arwa Alaa
OOP PILLARS

OOP have 4 Pillars


1.Encapsulation
2.Inheritance
3.Polymorphism
4. Abstraction
by Arwa Alaa
Encapsulation
Separate Data Definition [Attributes] From Its Use [methods]

by Arwa Alaa
Encapsulation

by Arwa Alaa
Encapsulation Definition

Encapsulation is one of the fundamental principles of object-oriented


programming (OOP). It involves bundling the data (attributes or properties) and
the methods (functions or behaviors) that operate on the data into a single unit,
called a class.

by Arwa Alaa
Problems Before Encapsulation

1. Maintenance of data on struct & class will effect out in use


• Solved this through getter and setter methods to access private attribute if maintain any data in
class ,class only effected

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)

A property in C# is a member of a class that provides a way to


get and set the value of a private field. Properties are defined
using the get and set accessors.

Named Rules
• Property Start with Capital Character And Have Sane Name Of Attribute

• Attribute Start with Small Character

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

▪ A full property provides encapsulation of a field with additional logic, such as


validation or calculation, in the getter and setter methods.

▪ 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)

• Automatic properties simplify property declarations by allowing The compiler


generates a private, anonymous backing field(Hidden Private Attribute) that can
only be accessed through the property's get and set accessors.

by Arwa Alaa
3.Indexer

An indexer is special property


• Named always with keyword this.

• Can take parameter.

• Can overloaded (depend on number & Type & Order parameters)

• Allows an object to be indexed like an array. Used when abject contain arrays

• It is useful for classes that encapsulate collections or other array-like structures.

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

propfull + 2tab :code snippet for full


property

prop + 2tab : code snippet for automatic


property
by Arwa Alaa
Constructor
Special Method

by Arwa Alaa
What Is Constructor ?

• A constructor is a special method in a class that is called


when an instance (object) of that class is created.

• The primary purpose of a constructor is to initialize the


newly created object, often by setting initial values for its
attributes.

by Arwa Alaa
Why Constructor is Special Function ?

1. Named always with the same name of its struct or class


2. Has No Return Type
3. Responsible for initialization of attribute inside struct or class
4. Take parameter or not
Always is public, In one case is private when implement
singleton design pattern
by Arwa Alaa
Constructor Chaining

Constructor chaining is useful for code reuse and maintaining clean,


DRY (Don't Repeat Yourself) code. (base constructor initialized first)

Use this to chain constructors within the same class.

Use base to chain constructors to the base class in inheritance


scenarios.

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?

Inheritance is a fundamental concept in Object-Oriented Programming (OOP)


that allows a new class (called the derived or child class) to inherit properties
and behaviors (methods) from an existing class (called the base or parent
class). This mechanism promotes code reusability, modularity, and the
organization of code in a hierarchical manner.

by Arwa Alaa
Key Concepts of Inheritance:

1.Base Class (Parent Class):


1. The class whose properties and methods are inherited by another class.
2. It is sometimes referred to as the superclass.
2.Derived Class (Child Class):
1. The class that inherits from the base class.
2. It can access the public and protected members of the base class and can also add its own
members or override the inherited ones.
3.Types of Inheritance:
1. Single Inheritance: A child class inherits from one parent class.
2. Multilevel Inheritance: A class inherits from a derived class, forming a chain of inheritance.
3. Hierarchical Inheritance: Multiple classes inherit from the same parent class.
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

• Inheritance relation represent as (Is a)

• By Default child constructor chain on Empty parameter-less parent constructor

• Inheritance is required in overriding with two keywords New ,Override

• 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

Overloading is a compile-time polymorphism feature in which an entity has


multiple implementations with the same name. Overloading enhances the
flexibility and readability of code by allowing the same method name to be used
for different tasks depending on the input parameters.

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

Method Overriding allows a derived class to provide a specific


implementation of a method that is already defined in its base class. using two
keyword New and Override

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.

An override method provides a new implementation of the method inherited from a


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

2. The new modifier hides an accessible base class 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

1. The override modifier is required to extend or modify the abstract or virtual


implementation of an inherited method, property, indexer, or event.
2. The overridden base method at first appear must be public virtual, abstract, or
override to be chaining. You cannot override a non-virtual or static method.
3. An override declaration cannot change the accessibility of the virtual method.
Both the override method and the virtual method must have the same access level
modifier.
4. Override modifier can be added before or after public.
5. If stop chaining with new keyword the last override will execute

by Arwa Alaa
Abstraction

by Arwa Alaa
Abstraction

Abstraction: It means providing only the essential


details to the outside world and hiding the internal
details, i.e., hiding the background details or
implementation. This is definition as a guidline

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

Concrete Class : Is Fully


Implemented Class ,can
create instance from it

Abstract Class : is Not Fully


implemented class , can’t
create instance from it

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

You might also like