You are on page 1of 36

Workshop on

Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)

Computer Science Instructor of well known international Center


Also, Machine Learning and Deep learning Practitioner

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the
concept of "objects", which can contain data, in the form of fields (often known
as attributes or properties), and code, in the form of procedures (often known as
methods). It is the programming concept based on bottom-up approach!

The main aim of OOP is to bind together the data and the functions that operate
on them so that no other part of the code can access this data except that
function.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Object-oriented programming
Pillars of object-oriented programming

1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Function/Method
Before going into depth in OOP, let’s understand the 6 basic things!

1. What is class
2. What is object
3. What is namespaces
4. What is function/Method
5. What is Constructor and its types
6. What is Destructors

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Function/Method
Class:
It is a user-defined data type, which holds its own data members and member functions, which can be
accessed and used by creating an instance of that class. A class is like a blueprint for an object.

In simple words, it is a prototype or building block structure for starting work in the OOP approach!

Object:
An Object is an identifiable entity with some characteristics and behavior. An Object is an
instance of a Class. When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Function/Method
Function VS Method:
A method is a code block that contains a series of statements according to the requirement
of the program! In OOP = Method and in procedure = Function

Namespaces
Namespaces in C# are used to organize too many classes so that it can be easy to handle the
application. We use System.Console where System is the namespace and Console is the
class. To access the class of a namespace, we need to use namespacename.classname. We
can use using keyword so that we don't have to use complete name all the time.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types
A constructor is a special method of the class which gets automatically invoked whenever an instance of
the class is created. Like methods, a constructor also contains the collection of instructions that are
executed at the time of Object creation. It is used to assign initial values to the data members of the
same class.
 
Types of Constructor
• Default Constructor
• Parametrized Constructor (with overload concept)
• Copy Constructor
• Private Constructor
• Static Constructor

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types
Default Constructor
A constructor with no parameters is called a default
constructor. A default constructor has every instance of
the class to be initialized to the same values. The
default constructor initializes all numeric fields to zero
and all string and object fields to null inside a class.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types

Parameterized Constructor with overload


A constructor have at least one parameter is called
a parametrized constructor. It can initialize each
instance of the class to different values.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types

Copy Constructor
This constructor will creates an object by copying
variables from another object. Its main use is to
initialize a new instance to the values of an existing
instance.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types
Private Constructor
If a constructor is created with private specifier is known as Private
Constructor. It is not possible for other classes to derive from this
class and also it’s not possible to create an instance of this class.
Points To Remember :
use private constructor when we have only static members.
Using private constructor, prevents the creation of the instances of
that class.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor and its types
Static Constructor (no access modifier, no parameters)
Static Constructor has to be invoked only once in the class and it has been
invoked during the creation of the first reference to a static member in the class.
A static constructor is initialized static fields or data of the class and to be
executed only once.

Points To Remember :
It can’t be called directly.
When it is executing then the user has no control.
It does not take access modifiers or any parameters.
It is called automatically to initialize the class before the first instance created.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Constructor
Important points to Remember About Constructors

• Constructor of a class must have the same name as the class name in
which it resides.
• A constructor can not be abstract, final, and Synchronized.
• Within a class, you can create only one static constructor.
• A constructor doesn’t have any return type, not even void.
• A static constructor cannot be a parameterized constructor.
• A class can have any number of constructors.
• Access modifiers can be used in constructor declaration to control its
access i.e. which other class can call the constructor.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Destructors
Destructors in C# are methods inside the class used to destroy instances of that
class when they are no longer needed. The Destructor is called implicitly by
the .NET Framework's Garbage collector and therefore programmer has no
control as when to invoke the destructor

• In c#, destructors can be used only in classes and a class can contain only one destructor.
• The destructor in class can be represented by using tilde (~) operator
• The destructor in c# won’t accept any parameters and access modifiers.
• The destructor will invoke automatically, whenever an instance of class is no longer needed.
• The destructor automatically invoked by garbage collector whenever the class objects that
are no longer needed in application.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Till Now, We have Learn!
What is class
What is object
What is namespaces
What is function/Method
What is Constructor and its types
What is Destructor

Theory + Practical in C#,


Thank you Very Much, end of Workshop Part 1
Now, next we will discuss about the pillars from scratch!

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Workshop on
Workshop Part 2

Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)

Computer Science Instructor of well known international Center


Also, Machine Learning and Deep learning Practitioner

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Till Now, We have Learn!
Workshop Part 1
What is class
What is object
What is namespaces
What is function/Method
What is Constructor and its types
What is Destructor

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Object-oriented programming
Pillars of object-oriented programming

1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Encapsulation
In object-oriented programming, encapsulation refers to the
bundling of data with the methods that operate on that data,
or the restricting of direct access to some of an object's
components.

Encapsulation, its can be accessed/done by Access Modifier /


Access Specifier
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Access modifiers (or access specifiers) are keywords in object-oriented
languages that set the accessibility of classes, methods, and other members. Access
modifiers are a specific part of programming language syntax used to facilitate the
encapsulation of components.
In the C#, there are Six access Modifier whereas just for information 4 in Java!

1. Public: The type or member can be accessed by any other code in the same assembly or another assembly that
references it.
2. Private: The type or member can be accessed only by code in the same class or struct.
3. Protected: The type or member can be accessed only by code in the same class, or in a class that is derived from
that class.
4. Internal : The type or member can be accessed by any code in the same assembly, but not from another assembly.
5. Protected Internal : The type or member can be accessed by any code in the assembly in which it is declared, or
from within a derived class in another assembly.
6. Private Protected: It access modifier is a combination of the private and protected keywords. We can access
members inside the containing class or in a class that derives from a containing class, but only in the same
assembly(project). Therefore, if we try to access it from another assembly, we will get an error. (C# 7.0)
In Java
Public Private Protected Default
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Inheritance
Object-Oriented programming language is code reuse. This reusability is possible due to the
relationship b/w the classes. Object oriented programming generally support 4 types of
relationships that are: inheritance , association, composition and aggregation. All these
relationship is based on "is a" relationship, "has-a" relationship and "part-of" relationship.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Inheritance

Also, multiple and hybrid level, (preform by interface)


because in C#, and Java these are not allowed using
inheritance!

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Has a relationship:
Association is relation between two separate classes which establishes through
their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-
many.
In Object-Oriented programming, an Object communicates to other Object to use
functionality and services provided by that object. Composition and Aggregation are
the two forms of association.
Student has a ID, name etc…
also with new keyword!

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Polymorphism
The word polymorphism means having many forms. In
object-oriented programming paradigm, polymorphism is
often expressed as 'one interface, multiple functions’.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Polymorphism
Function Overloading
You can have multiple definitions for the same function name in the
same scope. The definition of the function must differ from each
other by the types and/or the number of arguments in the argument
list. You cannot overload function declarations that differ only by
return type.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Polymorphism
Operator overloading:
The concept of overloading a function can also be applied to 
operators. Operator overloading gives the ability to use the
same operator to do various operations.

Let’s understand the concept of + in the code…

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Till Now, We have Learn!
1)What is Encapsulation:
2)Inheritance:
3)Polymorphism (only static or compile time)

Theory + Practical in C#,


Thank you Very Much, end of Workshop Part 2
Now, next we will discuss about the pillars (rest of the) from
scratch in the Last Session (Part 3)!

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Workshop on
Workshop Part 3

Object-oriented programming
FAHAD HUSSAIN
MCS, MSCS, DAE(CIT)

Computer Science Instructor of well known international Center


Also, Machine Learning and Deep learning Practitioner

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Till Now, We have Learn!
Workshop Part 1 Workshop Part 2
What is class 1)What is Encapsulation:
What is object
What is namespaces 2)Inheritance:
What is 3)Polymorphism (only static
function/Method or compile time)
What is Constructor
and its types
What is Destructor

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Polymorphism
The word polymorphism means having many forms. In
object-oriented programming paradigm, polymorphism is
often expressed as 'one interface, multiple functions’.

Method Override … Method Hiding

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Polymorphism
Method Hiding:
For hiding the base class method from derived class
simply declare the derived class method with the new
keyword.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Method Overriding
Run Time Polymorphism means overriding a base class method in
derived class by creating a similar function and this can be achieved
by using override & virtual keywords along with inheritance
 principle.
 
By using run time polymorphism, we can override a base class 
method in derived class by creating a method with same name and
parameters to perform a different task.
 
In c#, the run time polymorphism can be achieved by using method
overriding and it is also called as late binding or dynamic binding.
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Abstraction
Abstraction is the detail hiding of (implementation hiding).
Abstraction will be done by: Abstract class
• By using Abstraction you can used abstraction 0
to 100%!!!

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Abstraction
Interface (It is special type of class but not exactly
class, containing the abstract method only) also used
for multiple inheritance in C#.
• By using Interface you can used abstraction
100%!!!
• Also, Interface used for multiple level
inheritance in C#, Java!
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Exception Handling
The try catch and finally block is a mechanism to detect and handle run-time errors in code.
It is provided by one of the built-in classes for common exceptions. The exceptions are anomalies that
occur during the execution of a program. They can be because of user, logic or system errors. If a user
(programmer) does not provide a mechanism to handle these anomalies, the compiler provide a default
mechanism, which terminates the program execution. 

 try – A try block is used to encapsulate a region of code. If any code throws an exception within that
try block, the exception will be handled by the corresponding catch.
 catch – When an exception occurs, the Catch block of code is executed. This is where you are able to
handle the exception, log it, or ignore it.
 finally – The finally block allows you to execute certain code if an exception is thrown or not. For
example, disposing of an object that must be disposed of.
 throw – The throw keyword is used to actually create a new exception that is the bubbled up to a try
catch finally block.

For further assistance, code and slide https://fahadhussaincs.blogspot.com/


Finally
Workshop Day 1, Part 1 Workshop Day 2, Part 2

What is class 1) What is Encapsulation:


What is object 2) Inheritance:
What is namespaces 3) Polymorphism
What is function/Method (only static or compile time)
What is Constructor and Workshop Day 3, Part 3
its types
What is Destructor 1) Polymorphism
(only static or compile time)
2) Abstraction VS interface
Thank you very much to stay with me! 3) Exception Handling

For further assistance, code and slide https://fahadhussaincs.blogspot.com/

You might also like