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

5. Object Oriented Programming (OOP)

Chapter 05 discusses Object-Oriented Programming (OOP), highlighting its advantages such as improved code structure, reusability, and easier maintenance. Key concepts include classes, objects, encapsulation, inheritance, polymorphism, and exception handling, which together form the foundation of OOP in C#. The chapter also explains constructors, fields, properties, and the mechanisms of method overloading and overriding.

Uploaded by

mahdiware
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)
4 views

5. Object Oriented Programming (OOP)

Chapter 05 discusses Object-Oriented Programming (OOP), highlighting its advantages such as improved code structure, reusability, and easier maintenance. Key concepts include classes, objects, encapsulation, inheritance, polymorphism, and exception handling, which together form the foundation of OOP in C#. The chapter also explains constructors, fields, properties, and the mechanisms of method overloading and overriding.

Uploaded by

mahdiware
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/ 29

Chapter 05:

Object Oriented Programming


What is OOP?

 OOP stands for Object-Oriented Programming.


 Procedural programming is about writing
procedures or methods that perform operations
on the data, while object-oriented programming is
about creating objects that contain both data and
methods.
OOP has Several Advantage

 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.
Class and Object

Class and Object are the basic concepts of Object-Oriented


Programming which revolve around real-life entities.
A class is a user-defined blueprint or prototype from which objects are
created. Basically, a class combines the fields and methods(member
functions which define actions) into a single unit. In C#, classes
support polymorphism, and inheritance and also provide the concept of
derived classes and base classes.
Create a class.
To create it use class keyword

class Car
{
string color = "red";
}
Objects

 Object is a basic unit of Object-Oriented Programming


and represents real-life entities. It can also refer to the
instance of a class. An object of that class is created. An
object holds data and can access methods and their
properties. an object consists of :
 Identity: It gives a unique name to an object and
enables one object to interact with other objects.
 State: It is represented by attributes of an object, and
reflects the properties of an object.
 Behaviour: It is represented by the methods of an
object, and also reflects the response of an object with
other objects.
Fields and properties

 Fields are basic variables that hold data in a class, while


properties provide a safer and more flexible way to access and
modify that data. By using properties, you can keep your code clean
and maintain better control over how data is managed in your class.
 Properties expose fields. Fields should (almost always) be kept
private to a class and accessed via get and set properties.
Properties provide a level of abstraction allowing you to change the
fields while not affecting the external way they are accessed by the
things that use your class. class Person
{
private string name; // field
public string Name // property
{
get
{
return name;
} // get method
set
{
name = value; } // set method } }
Constructors

 A constructor is a special method that is used to initialize


objects. Constructor is a special method of the class which gets
automatically invoked whenever an instance of the class is
created. The advantage of a constructor, is that it is called when
an object of a class is created.

Example
class Geeks{
// constructor of Geeks class
public Geeks( ) { }
}

// Using of this constructor


Geeks obj = new Geeks();
Types of Constructor

1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
Default Constructor
A constructor with no parameters is called a Default
constructor.

Syntax
Parameterized
Constructor
A constructor having at least one parameter is called a
parameterized constructor. It can initialize each instance of the
class to different values.
it accepts one or more arguments or parameters, to initialize an
object with specific values, when created.

Syntax
Copy Constructor

Copy Constructor
A copy constructor is used to create a new object by copying the
values from an existing object of the same class.
The copy constructor is a constructor that creates an object by
initializing it with an object of the same class which has been
created previously.

Syntax
Polymorphism

Polymorphism is a fundamental concept in Object-Oriented


Programming (OOP) that allows objects of different types to be
accessed through the same interface. one with many forms.
There are two main types of polymorphism
Polymorphism: an object’s ability to take different forms
Essential ingredients of polymorphic behavior:
Ability to define a method in a superclass and override it in a
subclass
Subclass defines method with the same name
Ability to call the correct version of overridden method depending
on the type of object that called for it.
•Compile-time polymorphism (method overloading and operator
overloading).
•Run-time polymorphism (method overriding using virtual and
override keywords).
Compile Time
Polymorphism
Whenever an object is bound with its functionality at the compile
time, this is known as the compile-time polymorphism.
By knowing which method to call to check the method
signatures. So this is called compile-time polymorphism or static
or early binding.

Compile-time polymorphism is achieved through method


overloading Method Overloading says you can have more than one
function with the same name in one class having a different prototype.

Function overloading is one of the ways to achieve polymorphism but


it depends on technology and which type of polymorphism we adopt.
Run Time Polymorphism

 Whenever an object is bound with the functionality at run time, this is


known as runtime polymorphism. The runtime polymorphism can be
achieved by method overriding.
 determines the proper method to call at the runtime, not at the compile
time. It is also called dynamic or late binding.

 Method overriding says the child class has the same method as
declared in the parent class. It means if the child class provides the
specific implementation of the method that has been provided by one of
its parent classes then it is known as method overriding.
Encapsulation

is defined as the wrapping up of data and information


under a single unit.
It is the mechanism that binds together the data and
the functions that manipulate them.
encapsulation is a protective shield that prevents the
data from being accessed by the code outside this
shield.
Encapsulation
Abstraction

 Data abstraction is the process of hiding certain details


and showing only essential information to the user.
 Abstraction in C# is used to hide unwanted data and
shows only the required properties and methods to the
user.
 An abstract class in C# is a class that cannot be
instantiated on its own. It serves as a blueprint for other
classes, known as derived classes.
Abstract
Abstraction
Inheritance

 inheritance is a fundamental concept of object-


oriented programming that allows a new class to inherit
properties and behaviors (fields and methods) from an
existing class. This promotes code reusability and
establishes a natural hierarchy between classes.
 When creating a class, instead of writing completely new
data members and member functions, the programmer
can designate that the new class should inherit the
members of an existing class. This existing class is
called the base class, and the new class is referred to as
the derived class.
Inheritance
Class Inheritance
Types of Inheriance

 1. Single Inheritance:- Single inheritance is a basic type of


inheritance where a class inherits from another single class.
 2. Multi-Level Inheritance:- Multi-level inheritance is
implements when a class inherits from another derived class,
which creates a chain of inheritance.
 3. Hierarchical Inheritance:- Hierarchical inheritance occurs
when multiple classes inherit from a single base class.
 4. Multiple Inheritance:- Multiple inheritance states the
ability of a class to inherit from multiple base classes. But C#
does not support multiple inheritance. However, we can
implement multiple inheritance using the interfaces in c#.
Method Overriding in Inheritance

 In inheritance, method overriding allows you to rewrite a


method from the base class in a derived class using
the override keyword.

 if the same method is present in both the superclass and


the subclass. Then, the method in the subclass overrides
the same method in the superclass. This is called
method overriding.
Using 'base' Keyword in
Inheritance
The "base" keyword in inheritance allows a derived class to access the
members of its base class.
Using ‘This' Keyword in
Inheritance
The this keyword refers to the current instance of the class and is
also used as a modifier of the first parameter of an extension
method.
Preventing Inheritance with
sealed Keyword
Exception Handling

 What Is an Exception?
 An exception is a problem that arises during the
execution of a program. A C# exception is a
response to an exceptional circumstance that
arises while a program is running, such as an
attempt to divide by zero.
 C# exception handling is built upon four
keywords: try, catch, finally, and throw.
try, catch, finally, and throw

 try − A try block identifies a block of code for which particular


exceptions is activated. It is followed by one or more catch
blocks.
 catch − A program catches an exception with an exception
handler at the place in a program where you want to handle
the problem. The catch keyword indicates the catching of an
exception.
 finally − The finally block is used to execute a given set of
statements, whether an exception is thrown or not thrown. For
example, if you open a file, it must be closed whether an
exception is raised or not.
 throw − A program throws an exception when a problem
shows up. This is done using a throw keyword.
END

You might also like