Object in Programming
A bright red color sedan car with a unique weird design, sitting alone in an empty airport, wondering what is happening. He/she doesn’t know that I am using him/her in this article 😜
A guy named Gary, tall, lean, with fair skin color and a beautiful face like mine, strides towards the car
The car couldn’t see Gary, but the car can hear the sound of Gary's long hard strides
Gary took his car key while walking towards the car and commending the car to open the door for him
The car didn’t like the commending part, but it opened the door for him since he had the key to the car
Gary got into the car, insert the magic stick into the starter, and rotate the stick 45⁰ clockwise, which takes a considerable amount of electricity to kickstart the engine, the engine slowly awakened from sleep with 🔥 . The car felt a little warmer and more relaxing after a long cold night in an airport
Gary's eyes are on the road, one hand on the steering, another hand on the gear, and his extended right leg was pushing the accelerator hard
The car was begging him to change the gear, as the vehicle has to make a weird noise with the accelerator being pressed in the neutral position
Intense music playing in the background, you may wonder why intense music is needed since Gary and the car were alone in an empty airport field, it’s not like they were in a race 😂
The Clutch felt a little pressure, the gear stick moves downward left like a knight piece in chess and the accelerator starts to feel a little relieved by the Garg’s right leg
The tires start rolling, the air starts flowing into the face of the car as it starts moving, the gear stick was making several jumps, and both the clutch and the accelerator continuously suffering from Gary’s legs
Both Gary and the car enjoyed their time together at the airport with the clean and unpolluted air flowing through them
I took my TV remote and skipped the commercial after the above scene, by gently tapping the confirm button
That’s the end of the story 😎
In the next section, we will understand What is Object in Programming with the help of the above story
Object:
The car, the hero of the story is an Object
What does the Car look like?
- The car's color is red
- The car’s type is Sedan
- The car has four doors, four tires, and four seats
- The car has a gearbox with a gear stick, an accelerator, a clutch, a brake, and a steer
- Oops, we missed the heart of the car, the mighty engine
- And many more parts…
What a Car can do?
- The car can open the door with a door key
- The car can awaken the engine with the car key
- The car can move forward with his/her four tires
- The car can able to change speed with the help of the accelerator, clutch, brake, and gear
An object has both properties and methods, using methods, the value of a property can be changed
In the case of cars, the properties are:
- Color
- Type
- Speed
- Door state - OPEN or CLOSE
- Engine state - ON or OFF
- Car state - ON or OFF
And the methods are:
- Paint shop— The color can be changed with the help of the paint shop
- Pressing the accelerator or hitting the brake pedal, will change the speed of the car
- Putting and turning the car key into the starter will change the car and engine state from OFF to ON respectively
Methods are like actions, that can be triggered either manually or automatically, and because of the actions, the value of zero or more properties may or may not change
Example In Python:
x = 5
y = 10
print('SUM:', x + y)
In Python, everything is an Object, even numbers also, the first statement x = 5
creates an int
object and x
is a reference to an int
object
You can also create an int
object using int
class like x = int(5)
. Class is like a blueprint, using which different copies of objects can be created, like car manufacturers creating thousands of copies of a particular car model
The second statement y = 10
creates another int
object with a real value of 10 and assigns the reference to y
In the third statement, x + y
creates another int object with a real value of 15 and prints the result SUM: 15
on screen. Here, x + y
is an action, which sums the value of x
and y
, and creates a new int
object. In this case, the value of x
or y
is not being changed because of the action, instead, a new object is getting created
To see all the available properties and methods of int
object, you can use dir(x)
, it will give the following output in Python REPL,
As you can see, x
has properties like
- real —
x.real
will give you the real value ofx
, which is5
- imag —
x.imag
will give you imaginary part of a number, sincex
doesn’t have any imaginary part, it is0
x
has methods(actions) like,
- conjugate —
x.conjugate()
will give you the complex conjuguate ofx
, which is5
, since there is no imaginary part in x - __add__ — This method is responsible for adding two numbers,
x.__add__(5)
will result in10
, you can also trigger/call the__add__
method using+
operator - For Substraction —
__sub__
method is used - For Multiplication —
__mul__
method is used - And many more…
For every actions, there will be methods
Conclusion:
Class is a blueprint for creating objects
Object has properties and methods, methods can be used to change the properties
If you like this article, give a thumps up and follow me for more interesting technical and personal related articles
If you have any suggestions/improvements, Please leave your suggestions in comment section, will bring those changes 🙋♂️