Describing Similar Objects
Sometimes, the objects that we are describing are so similar that only values of the properties differ.
In this case, the objects that we describe have completely the same set of properties like camera, storage, etc.
Template
For objects that are very similar to each other (objects that have the same set of actions and properties), we can create a standard Form or Template that can be used to describe different objects in that category.
Mobile Template
Filled Template
Bundling Data
While modeling real-life objects with object oriented programming, we ensure to bundle related information together to clearly separate information of different objects.
Bundling of related properties and actions together is called Encapsulation.
Classes can be used to bundle related properties and actions.
Defining a Class
To create a class, use the keyword
Special Method
In Python, a special method
Code
Properties & Values
Code
In the above example,
Action
In the above example, the below function is an action
In OOP terminology, properties are referred as attributes actions are referred as methods
Using a Class
To use the defined class, we have to instantiate it. A class is like a blueprint, while its instance is based on that class with actual values.
Instance of Class
Syntax for creating an instance of class looks similar to function call.
An instance of class is Object.
Code
Class Object
An object is simply a collection of attributes and methods that act on those data.
Method Arguments & Return Values
Similar to functions, Methods also support positional, keyword & default arguments and also return values.
Code
Instance Methods of Class
For instance method, we need to first write
Code
Output
Multiple Instances
Code
Output
Type of Object
The class from which object is created is considered to be the type of object.