(objects are the red colored words)
example : Class → computer Object → i5, 6gb, 256(specs)
Objects
Object-oriented programming (OOP) models a given problem/scenario into a set of interacting objects. These objects have some data, also known as properties, and exhibit some behaviors. In Object-Oriented terms, we refer to this data as attributes, and behaviors are referred to as methods. Methods refer to what an object can do and what can be done on an object. The benefit of OOP is that it effectively represents real-world entities. For example, if we take a company, it has employees. If we implement a Human Resource management system using an OOP language, we can consider each employee as an object. Each employee object handles the processing and data management related to that employee. There are multiple employee objects - each corresponding to an individual employee.
The set of attributes of an object collectively defines its characteristics. The values of these attributes at a given point in time define the object’s state. As an example, consider an object referring to a bank account. It has a unique number, the account holder’s name, and the current balance. The account number or the account holder’s name may remain the same—however, the current balance changes. If we consider the state of a bank account object with these three attributes, the object may be in different states at different time points if any of these attributes change their values during the object’s lifespan. For example, if the current balance of an account object changes, that object is said to be in a new state.
In the case of a bank account object, depositing and withdrawing money can be taken as its behavior. Note that the behavior of an object might change its state. For example, when we withdraw money from the bank account, it changes the current balance of the bank account, which indicates a different state of the bank account object.
Classes (class is a design for a object)
first create a blue print of it an then design
Objects of a given type (e.g., bank account) have the same attributes. What differs across objects is the values of these attributes. For example, Peter’s bank account may have $500, while Ann’s account has $1000. To create different objects of the same type, we use a blueprint called a class. A class specifies what attributes and methods its objects can have. When creating objects from a class, we provide actual values for the attributes defined by the class. That way, we can create any number of objects from the same class.
To identify the set of classes that we should implement for a given problem, first, we need to look for the nouns in the given problem description. However, even class attributes are nouns. For example, when we say, 'employee has a name,’ both employee and name are nouns. However, only the employee should be an object. Thus, we need to have an “Employee” class. Thus, after identifying nouns, we need to see which of these nouns interact with other nouns. For example, consider the statement, 'an employee works in a division'. This shows an interaction between the division and the employee. Thus, both employee and division can be considered as objects.
Constructor
To create objects from a class, we have to call the 'constructor' of the class. The constructor can be considered a special method in the class, called ONLY when an object has to be created. A constructor must have its name as __init__,
and the first parameter should be the ‘self
’ keyword.Attributes
A class in Python can have different types of attributes. Attributes defined outside any method but within the class are called the class attributes. In contrast, values for instance attributes should be set for each object created. These attributes are declared inside the constructor and follow the ‘self’ keyword (e.g,self.age
). In a Python class, a variable name with a leading underscore indicates that the attribute should be treated as private to that class. Note that it is always possible to access such attributes from outside the class they have been defined in. However, this is not a good programming practice.
Methods