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

Object-Oriented Programming 1

Oop Java Notes

Uploaded by

231400033
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Object-Oriented Programming 1

Oop Java Notes

Uploaded by

231400033
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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/


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/


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/


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/


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/


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/


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/


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/


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/


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/


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
It can be access by Access Modifier / Access Specifier, so, in the C# there are Six access Modifier in C# whereas just for
information 4 in Java!

1. Public
2. Private
3. Protected
4. Internal
5. Protected Internal
6. Private Protected

Using Private access modifier, we can Encapsulation the Code (Class, Method etc)…

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

You might also like