2024: Domino Containers - The Next Step. News from the Domino Container commu...
Object-oriented programming 3.pptx
1. 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/
2. Object-oriented programming
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
3. Object-oriented programming
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Pillars of object-oriented programming
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
4. Class ,Object and Function/Method
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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
5. Class ,Object and Function/Method
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
6. Class ,Object and Function/Method
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
7. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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
8. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
9. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
10. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
11. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
12. Constructor and its types
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
13. Constructor
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
14. Destructors
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
15. Till Now, We have Learn!
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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!
16. 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/
Workshop Part 2
17. Till Now, We have Learn!
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
What is class
What is object
What is namespaces
What is function/Method
What is Constructor and its types
What is Destructor
Workshop Part 1
18. Object-oriented programming
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Pillars of object-oriented programming
1)Encapsulation:
2)Inheritance:
3)Polymorphism:
4)Abstraction:
5)*Exception Handling
19. Encapsulation
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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
20. 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
21. Inheritance
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
22. Inheritance
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Also, multiple and hybrid level, (preform by interface)
because in C#, and Java these are not allowed using
inheritance!
23. 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!
24. Polymorphism
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
The word polymorphism means having many forms. In
object-oriented programming paradigm, polymorphism is
often expressed as 'one interface, multiple functions’.
25. Polymorphism
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
26. Polymorphism
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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…
27. Till Now, We have Learn!
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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)!
28. 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/
Workshop Part 3
29. Till Now, We have Learn!
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
What is class
What is object
What is namespaces
What is
function/Method
What is Constructor
and its types
What is Destructor
Workshop Part 1
1)What is Encapsulation:
2)Inheritance:
3)Polymorphism (only static
or compile time)
Workshop Part 2
30. Polymorphism
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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
31. Polymorphism
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
Method Hiding:
For hiding the base class method from derived class
simply declare the derived class method with the new
keyword.
32. Method Overriding
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
33. Abstraction
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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%!!!
34. Abstraction
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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!
35. Exception Handling
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
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.
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.
36. Finally
For further assistance, code and slide https://fahadhussaincs.blogspot.com/
What is class
What is object
What is namespaces
What is function/Method
What is Constructor and
its types
What is Destructor
Workshop Day 1, Part 1
1)What is Encapsulation:
2)Inheritance:
3)Polymorphism
(only static or compile time)
Workshop Day 2, Part 2
1)Polymorphism
(only static or compile time)
2) Abstraction VS interface
3) Exception Handling
Workshop Day 3, Part 3
Thank you very much to stay with me!