CS 111 › Lesson 10 of 12

Object-Oriented Programming: Classes

Lesson 10 · CS 111: Programming I — Python · OKSTEM College

Object-Oriented Programming: Classes

OOP bundles data and behavior into objects, making complex programs easier to organize and extend.

Work through the concepts using Python in your editor or Replit. Use the knowledge check below to test yourself.

Try It Yourself

Key Exercise

Open a Python file and implement the core concepts from this lesson. Start small, test each part, then combine.

Knowledge Check

In OOP, what is an instance?

That is the class.
Correct.
A method is a function defined in a class.
__init__ is the constructor method, not an instance.
Quick Recap

The class is the blueprint. An instance is a specific object created from that blueprint.

Quick Recap

Methods are functions belonging to a class. An instance is a particular object created by calling the class.

Quick Recap

__init__ is called when creating an instance, but __init__ itself is the constructor method, not the instance.

What is the purpose of self in a method definition?

self refers to the instance, not the class.
Correct.
self is a convention, not a keyword.
super() refers to the parent class.
Quick Recap

self is the instance being operated on. To reference the class use type(self) or the class name directly.

Quick Recap

self is just a naming convention - you could name it anything. But it's universally followed. It's not a Python keyword.

Quick Recap

super() provides access to the parent class. self refers to the current instance.

What does __init__ do?

That is __del__.
Correct.
That is __str__ or __repr__.
Copying uses __copy__ or copy.deepcopy().
Quick Recap

__del__ is the destructor. __init__ is called when an object is created to initialize its attributes.

Quick Recap

__str__ defines what print(obj) shows. __init__ sets up the object's initial state.

Quick Recap

__init__ initializes a new object. Copying is a separate operation.

What does @property decorator do?

Decorators don't affect speed.
Correct - obj.name instead of obj.name().
You need both @property and no setter for that.
That is @classmethod.
Quick Recap

@property makes a method callable without parentheses (like an attribute). No speed effect.

Quick Recap

@property alone just makes it accessible like an attribute. To make it truly immutable, define only a getter, no setter.

Quick Recap

@classmethod makes a method receive the class (cls) instead of the instance. @property makes a method behave like an attribute.

What is encapsulation?

That is just module organization.
Correct.
That is inheritance.
Encapsulation usually involves making attributes private.
Quick Recap

Encapsulation is a design principle: bundle data and the methods that operate on it together, hiding internal details.

Quick Recap

Inheritance lets a subclass reuse parent class behavior. Encapsulation is about bundling and hiding internals.

Quick Recap

Encapsulation often means making attributes private (prefixed with _) and exposing controlled access through methods/properties.