Skip to main content
Informatik.uz
Bachelor’s · Year 2Chapter 1. Object-oriented programming

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.

Topic 1 of 22 min readUpdated October 27, 2026
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 is self.
  • Encapsulation protects the internal state and controls its changes through methods.

Practice tasks

  1. Write a Student class with the attributes name and year and the methods promote() (increases the year by 1) and info() (returns a string about the student). Create two objects and call the methods.
  2. Create a BankAccount class: the balance is an internal attribute (_balance); the methods deposit(amount) and withdraw(amount) must validate the amount (reject a negative amount or an amount larger than the balance).
  3. 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.

  1. 1What is the difference between a class and an object?
    A class is a template that describes the structure, and an object is a concrete instance created from that template.
  2. 2What does the `self` parameter stand for?
    A reference to the current object on which the method is called.
  3. 3Why is encapsulation needed?
    To protect internal data from incorrect changes and manage it only through controlled methods.

0 of 2 topics completed

Sign in to mark