You are on page 1of 18

C#- OOP presentation

C# - WHAT IS OOP?

 OOP stands for Object-Oriented Programming.

 Object-oriented programming is about creating objects that


contain both data and methods.

 Object-oriented programming has several advantages over


procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP makes it possible to create full reusable applications
with less code and shorter development time

 The language must meet the following criteria:


 Abstraction
 Encapsulation
 Polymorphism
 Inheritance
C# - WHAT ARE CLASSES AND OBJECTS?

 Classes and objects are the two main aspects of object-oriented


programming.

 a class is a template for objects, and an object is an instance of a class.

 When the individual objects are created, they inherit all the variables
and methods from the class.

 Everything in C# is associated with classes and objects, along with its


attributes and methods.
For example: in real life, a car is an object. The car has attributes, such
as weight and color, and methods, such as drive and brake.

 A Class is like an object constructor, or a "blueprint" for creating objects.


Create a Class
 To create a class, use the class keyword:

 Create a class named "Car" with a variable color:


class Car
{
string color = "red";
}

Create an Object
 An object is created from a class.
 To create an object specify the class name, followed by the object
name, and use the keyword new

 you can create multiple objects of one class


Class Members
 Fie lds a n d m e th ods in s ide c la s se s a re oft e n r e fe rre d to a s " Cla s s M e m be rs "
 va r iable s in s ide a cl as s a re c a lle d fie l ds , a n d th a t you c an a c c e s s th e m by
cre at in g a n obj ect of t h e c la s s, a n d by u s in g th e dot s yn ta x ( . ).
 Y ou c a n a ls o le a ve th e fi e lds bla nk , a nd m odify the m wh e n c re ati n g th e o bje c t

Object Methods
 M e th ods n or m a lly be lo ng s to a c la ss , an d the y de fin e ho w a n obje ct of a c la s s
be h a ve s .
 Ju s t lik e with fiel ds , you ca n a c c e s s m e th ods wit h th e do t sy nta x. Ho we ve r ,
n ote t ha t th e m e tho d m u s t be pu blic . A n d r e m e m be r t h at we us e th e n a m e of
th e m e t h od fo llowed by two pa ra n th e s e s () a nd a se m ic ol on ; to c a ll th e
m e th od
ACCESS MODIFIERS

Public : The code is accessible for all classes

Private : The code is only accessible within the same class

Protected : The code is accessible within the same class, or


in a class that is inherited from that class.
Internal : The code is only accessible within its own
assembly, but not from another assembly.
C# PROPERTIES (GET AND SET)

 private variables can only be accessed within the same class.


However, sometimes we need to access them - and it can be
done with properties.

 A property is like a combination of a variable and a method,


and it has two methods: a get and a set method

 The get method returns the value of the variable name.

 The set method assigns a value to the variable. The value


keyword represents the value we assign to the property.
ENCAPSULATION

 The meaning of Encapsulation, is to make sure that


"sensitive" data is hidden from users. To achieve this, you
must:

declare fields/variables as private.

 provide public get and set methods, through properties, to


access and update the value of a private field
WHY ENCAPSULATION?

 Better control of class members

 Fields can be made read-only (if you only use the get method),
or write-only (if you only use the set method)

 Flexible: the programmer can change one part of the code


without affecting other parts

 Increased security of data


C# INHERITANCE

 In C#, it is possible to inherit fields and methods from one


class to another. We group the "inheritance concept" into two
categories:

 Derived Class (child) - the class that inherits from another


class
 Base Class (parent) - the class being inherited from

 To inherit from a class, use the : symbol.

 It is useful for code reusability: reuse fields and methods of


an existing class when you create a new class.
C# POLYMORPHISM

 Polymorphism means "many forms", and it occurs when we


have many classes that are related to each other by
inheritance.

 With method overloading, multiple methods can have the same


name with different parameters . Instead of defining two
methods that should do the same thing.
 C# provides an option to override the base class method, by
adding the virtual keyword to the method inside the base class,
and by using the override keyword for each derived class
methods
 Polymorphism uses those methods to perform different tasks.
This allows us to perform a single action in different ways.

 It is useful for code reusability: reuse fields and methods of


an existing class when you create a new class.
C# ABSTRACTION

 Data abstraction is the process of hiding certain details and


showing only essential information to the user.

 Abstraction can be achieved with either abstract classes or


interfaces

 The abstract keyword is used for classes and methods:

 Abstract class: is a restricted class that cannot be used to


create objects
 Abstract method can only be used in an abstract class, and it
does not have a body. The body is provided by the derived class
.

 An abstract class can have both abstract and regular methods

 To access the abstract class, it must be inherited from another


class.

 To achieve security - hide certain details and only show the


important details of an object.

 Abstraction can also be achieved with Interfaces


C# INTERFACE


Another way to achieve abstraction in C#, is with interfaces.


An interface is a completely "abstract class", which can only contain
abstract methods and properties


To access the interface methods, the interface must be "implemented"
by another class. To implement an interface, use the : symbol . The
body of the interface method is provided by the "implement" class.

Note that you do not have to use the override keyword when
implementing an interface:


Like abstract classes, interfaces cannot be used to create objects.
 Interface methods do not have a body - the body is provided
by the "implement" class

 On implementation of an interface, you must override all of


its methods
 Interfaces can contain properties and methods, but not
fields/variables
 An interface cannot contain a constructor.

 To achieve security - hide certain details and only show the


important details of an object (interface).

 C# does not support "multiple inheritance" . However, it can


be achieved with interfaces.
THANK YOU

You might also like