Class and object
The foundation of object-oriented programming is the notions of class and object. We study attributes, methods, the constructor and the principle of encapsulation through Python examples and practical tasks.
All topics (1 of 2)
Object-oriented programming (OOP) builds a program as a set of interacting objects. An object has a state (data, called attributes) and behavior (actions, called methods). For example, a student object has the attributes name and year, and a method that moves the student to the next year.
A class is a template that describes the common structure of objects of one kind; an object is a concrete instance of the class. Any number of objects can be created from one class, and each has its own attribute values. The relation between a class and an object resembles a cake recipe and the finished cakes: there is one recipe and many cakes.
In Python a class is declared with the line class Student:. The constructor def __init__(self, name, year): is called automatically when an object is created and sets its attributes: self.name = name. Here self is a reference to the current object for which the method is running. An object is created as s = Student("Ali", 2), a method is called through a dot (s.promote()), and an attribute is accessed the same way (s.year).
One of the key principles of OOP is encapsulation: the internal state of an object is hidden and changed only through methods. For example, instead of assigning a value directly to the balance of a bank account, the method deposit(amount) is called, and it checks that the amount is positive. In Python the name of an internal attribute usually starts with an underscore (_balance): this is a convention meaning “do not touch from outside”.
Key takeaways
- OOP builds a program as a set of interacting objects.
- An object consists of attributes (state) and methods (behavior).
- A class is a template and an object is its concrete instance; many objects are created from one class.
- In Python the constructor is
__init__and the reference to the current object isself. - Encapsulation protects the internal state and controls its changes through methods.
Practice tasks
- Write a
Studentclass with the attributesnameandyearand the methodspromote()(increases the year by 1) andinfo()(returns a string about the student). Create two objects and call the methods. - Create a
BankAccountclass: the balance is an internal attribute (_balance); the methodsdeposit(amount)andwithdraw(amount)must validate the amount (reject a negative amount or an amount larger than the balance). - Design two classes in an area of your choice (a library, a shop or a game): write at least three attributes and two methods of each as a diagram, then implement one of them in code.
Review questions
Test yourself: think first, then reveal the answer.
1What is the difference between a class and an object?Show answerHide answer
A class is a template that describes the structure, and an object is a concrete instance created from that template.2What does the `self` parameter stand for?Show answerHide answer
A reference to the current object on which the method is called.3Why is encapsulation needed?Show answerHide answer
To protect internal data from incorrect changes and manage it only through controlled methods.
0 of 2 topics completed
Sign in to mark