
Object Oriented Programming
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of "objects". Python, C++, Java and many other programming languages are all object-oriented programming languages.
Firstly, let's delve into the key components of OOP.
Class
A class is a blueprint that defines attributes (data) and methods (functions) common to all objects of that class.
For example, if you want to make a toy car, you'd start with a blueprint that tells you what the car should look like and what it should be able to do. In programming, a class is like that blueprint. It defines the properties (characteristics) and behaviors (actions) that objects made from that class will have.
Create a Class
To define a class, use the class
keyword followed by the class name. The class name should start with a capital letter.
Example
Create a class named Person
, with a property named classification
:
# Define a classclass Person: # Define a class property named "classification" classification = 'mammals'
Object
An objet is an instance or example created from a blueprint (class) with its own specifications.
For example, you use a blueprint (class) for car manufacturing to create a toy car (object). The finished toy car that you can hold in your hand is like an object. So, while the class describes what the car should be like, the object is the actual car you can play with.
Create an Object
Example
Create an object "p1" from the Person
class. The "p1" object automatically inherits the class attribute classification
.
This step is called instantiation.
# Create an instance of the Person class # and assign it to the variable p1p1 = Person()print (p1.classification)
Output:
mammals
The object p1
is an instance of the Person
class. Consequently, p1
possesses the class attributes defined within the Person
class, such as the classification
of being mammals.
The __init__()
Function
The examples above are the most basic representations of classes and objects, not typically practical for real-world applications.
For instance, in addition to the common classification of mammals inherited from the class Person
, it would be advantageous for the p1
object to possess distinctive attributes such as hobbies and age. To achieve that, the built-in __init__()
function is used to initialize newly created instances with specific initial state and attributes.
The __init__()
function is a constructor method, that is automatically called when a new instance of a class is created. Its primary purpose is to initialize attributes of the newly created object.
Create a Class with __init__()
Function
Add __init__()
function to the Person class.
class Person: classification = 'mammals' def __init__(self, name, age, hobby): self.name = name self.age = age self.hobby = hobby
Explanation
classification = 'mammals'
:classification
is a class attribute that can be shared among all instances of thePerson
class.def __init__()(self, name, age, hobby)
: This line defines the__init__()
function.self
: Theself
parameter refers to instances or objects of the class. It's like a placeholer for new objects.- The parameters
name
,age
, andhobby
are function parameters used to receive data needed for creating new instances.
self.name = name
: this assigns the argumentname
(the second name of the code) to thename
(the first name of the code) attribute of objects.self.age = age
: this assigns the argumentage
(the second age of the code) to theage
(the first age of the code) attribute of objects.self.hobby = hobby
: this assigns the argumenthobby
(the second hobby of the code) to thehobby
(the first hobby of the code) attribute of objects.
Create instances from the Person class
Create two instances, p1
and p2
.
# Create an instance "p1" of the Person class# Pass values to name, age and hobby parametersp1 = Person(name="Alice", age=24, hobby='hiking')
print(p1.name)print(p1.age)print(p1.hobby)
print(p1.classification)
Output:
Alice
24
hiking
mammals
When creating p1
, the values for name
, age
, and hobby
are provided as keyword arguments to the Person
class constructor. These arguments are then assigned to the corresponding attributes of the p1
object using the lines within the class definition: self.name = name
, self.age = age
, self.hobby = hobby
. Therefore, the p1
object obtains attributes: name of "Alice", age of "24", hobby of "hiking".
# Create another instance "p2" of the Person class# Pass different values to name, age and hobby parametersp2 = Person("Jason", 28, 'soccer')
print(p2.name)print(p2.age)print(p2.hobby)
print(p2.classification)
Output:
Jason
28
soccer
mammals
Similarly, p2
obtains its own attributes during instantiation: name of "Jason", age of "28", hobby of "soccer".
Both p1
and p2
share the same classification
of mammals, since the classification
attribute is a class attribute1 common to all new instances. Attributes defined in the __init__()
function are instance attributes2 which can vary for different instances.
Annotation
1. Class attributes are defined at the class level (outside of any method, typically near the top of the class definition).
2. Instance attributes are defined in the __init__()
method or other methods.