3. 3
Why OOP?
With object oriented programming, the goal is
to encapsulate your code into logical, hierarchical
groupings using classes so that you can reason about your code
at a higher level
4. 4
What is OOP?
Object oriented programming is a method of programming that attempts to
model some process or thing in the world as a class or object. OOP concepts
allow us to create specific interactions between objects. They make it
possible to reuse code without creating security risks or harming
performance and code readability
class - a blueprint for objects. Classes can contain methods (functions) and
attributes (similar to keys in a dict).
instance - objects that are constructed from a class blueprint that contain
their class's methods and properties.
Classes are created by keyword class, Attributes are the variables that
belong to a class.
Attributes are always public and can be accessed using the dot (.) operator.
Eg.: Myclass.Myattribute
5. 5
SELF AND INIT
The self parameter is a reference to the current instance of the class, and is used
to access variables that belongs to the class.
It does not have to be named self , you can call it whatever you like, but it has to
be the first parameter of any function in the class
All classes have a function called __init__(), which is always executed when the
class is being initiated.
Use the __init__() function to assign values to object properties, or other
operations that are necessary to do when the object is being created:
6. 6
Class definition syntax:
Class classname:
Statement-1
#class body
.
Statement-n
An object consists of :
State: It is represented by the attributes of an object. It also reflects the
properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the
response of an object to other objects.
Identity: It gives a unique name to an object and enables one object to interact
with other objects.
class Car:
def __init__(self, name, mileage):
self.name = name
self. mileage = mileage
------------------------------------------------------------
------
obj2 = Car("Honda City",24.1)
7. 7
The methods defined inside a class other than the constructor method are known as
the instance methods. Furthermore, we have two instance methods here- description()
and max_speed(). Let’s talk about them individually-
description()- This method is returning a string with the description of the car
such as the name and its mileage. This method has no additional parameter. This
method is using the instance attributes.
max_speed()- This method has one additional parameter and returning a string
displaying the car name and its speed.
Three important things to remember are-
1. You can create any number of objects of a class.
2. If the method requires n parameters and you do not pass the same number of
arguments then an error will occur.
3. Order of the arguments matters.
9. 9
ENCAPSULATION
Encapsulation helps with data security, allowing you to protect the data stored in a
class from system-wide access. As the name suggests, it safeguards the internal
contents of a class like a capsule.
You can implement encapsulation in Java by making the fields (class variables)
private and accessing them via their public getter and setter methods. JavaBeans are
examples of fully encapsulated classes.
Encapsulation in Java:
Restricts direct access to data members (fields) of a class
Fields are set to private
Each field has a getter and setter method
Getter methods return the field
Setter methods let us change the value of the field
10. 10
The __init__ method :
The __init__ method is similar to constructors in C++ and Java.
It is run as soon as an object of a class is instantiated. The
method is useful to do any initialization you want to do with
your object
Self keyword:
self represents the instance of the class. By using the
“self” we can access the attributes and methods of the class
in python. It binds the attributes with the given arguments.
11. 11
Encapsulation - the grouping of public and private attributes and methods into a
programmatic class, making abstraction possible
ENCAPSULATI
ON
12. 12
ABSTRACTION
Abstraction - exposing only "relevant" data in a class interface, hiding private attributes and
methods (aka the "inner workings") from users