What is object-oriented programming?
Last updated: 2026-11-26
Object-oriented programming is a way of building programs from objects, each of which holds data together with functions that work on that data. The practical question is which data and which rules belong together.
Start learning No account needed
1. The words you need
| Term | Meaning | In the example |
|---|---|---|
| Class | A definition used to make objects | Book |
| Object, or instance | One thing made from a class | emma |
| Attribute | Data stored on an object | borrower |
| Method | A function defined by the class | lend |
| Inheritance | A class built on another class | ReferenceBook |
To decide where something goes, ask which data a rule reads or changes. The rule "a book that is out cannot be lent again" reads and changes the borrower of one book, so we put the borrower and the rule on the book.
2. A worked example: lending books
class Book:
def __init__(self, title):
self.title = title
self.borrower = None
def lend(self, name):
if self.borrower is not None:
return False
self.borrower = name
return True
def give_back(self):
self.borrower = None
class ReferenceBook(Book):
def lend(self, name):
return False
emma = Book("Emma")
dune = Book("Dune")
atlas = ReferenceBook("Atlas")
print("1", emma.lend("Ana"))
print("2", emma.lend("Ben"))
print("3", dune.lend("Ben"))
print("4", emma.borrower, dune.borrower)
emma.give_back()
print("5", emma.lend("Ben"))
print("6", atlas.lend("Ana"), atlas.borrower)
emma.borrower = "Cy"
print("7", emma.borrower)
Predict the seven lines before you read them. The script runs this code and checks the output. It lives at scripts/what-is-object-oriented-programming-worked-example.py in the Professor Turing repository.
1 True
2 False
3 True
4 Ana Ben
5 True
6 False None
7 Cy
page checks passed
The program makes three objects. Lines 1 and 2 call the same method on the same book, and the second call returns False because that object now remembers Ana. Line 3 succeeds because the other book has its own borrower. Line 5 succeeds after give_back clears the first one.
Line 6 shows inheritance. ReferenceBook gets __init__ and give_back from Book and replaces lend with its own version, so a call with the same name does something different. Replacing a method this way is called overriding.
3. A common mistake: going around the rules in a class
It is possible to define a class, create objects and still leave the rules somewhere else. Line 7 shows the risk. Writing to emma.borrower directly replaced Ben with Cy, and the check inside lend did not run. Python lets you do this. By convention, a name that starts with an underscore, such as _borrower, tells other programmers to leave it alone and use the methods.
A second mistake is reaching for inheritance whenever two things look alike. If one object merely has another, as a library has books, store it as an attribute. This is called composition. Inheritance fits when the new class can reasonably be treated as a kind of the old one. Our ReferenceBook is debatable, since it refuses to lend even when nobody has it, which a plain Book never does. Code written for any Book has to cope with that.
4. Test yourself
Case 1. A library limits each member to three books. Which object should hold the count and the rule?
Answer to case 1
A Member object is a reasonable home, since the rule reads one member's current loans. The lending step then has to ask both the member and the book.
Case 2. After the code above, what does dune.lend("Ana") return?
Answer to case 2
False. Ben still has that book, because give_back was called on the other one.
Case 3. A car has an engine. Inheritance or composition?
Answer to case 3
Composition. A car is not a kind of engine.
To practice writing classes yourself, see how to learn programming without copying code.
What the sources found, and their limits
Object-oriented programming. Wikipedia, CC BY-SA. It supports the description of objects as holding data and functions, the terms class and inheritance, composition as a has-a relationship, the underscore convention in Python, and the note that which features make a language object-oriented is contested. Source: Object-oriented programming, read on 2026-11-25.
The Python Tutorial, section 9, Classes. Python Software Foundation. Documentation under the PSF License, with its code examples also under the Zero-Clause BSD license. It supports classes as bundling data with functionality, instance attributes, overriding in derived classes, and the absence of enforced private variables. Source: The Python Tutorial, section 9, Classes, read on 2026-11-25.
Limits: the example uses Python, and other languages differ, for instance in how they hide data. Where a rule belongs is a design judgment with more than one defensible answer. The library, the code and the cases are ours.
How Professor Turing helps
Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.
Bring a small program you are designing and say which object you think should hold one of its rules. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, it asks how you got there instead of saying right or wrong. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.
FAQ
What is the difference between a class and an object? A class is the definition, such as Book. An object is one thing made from it, such as the book stored in emma, with its own attribute values.
Do I have to use classes in Python? No. Python also lets you write programs with plain functions and built-in types. A class helps when some data and the rules that change it need to stay together.
Start with what you're working on
Tell the tutor which program you are working on and let it ask you the questions.
Start learning No account needed
See also: how to learn programming without copying code · why is Python so hard · all study guides