Programming FundamentalsNot Started
OOP Basics
Classes, objects, attributes, and methods
0%
Knowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Object-Oriented Programming organizes code around objects — bundles of data (attributes) and behavior (methods).
python
class Dog:
def __init__(self, name):
self.name = name # attributedef bark(self): # method return f"{self.name} says Woof!"
fido = Dog("Fido") # create instance print(fido.bark()) # Fido says Woof! ```
Key concepts:
- class — blueprint for creating objects
- __init__ — constructor, called when creating an instance
- self — refers to the current instance
- Inheritance — a class can extend another class
Examples
Inheritance
Cat inherits from Animal and overrides the speak method
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow"
c = Cat("Whiskers")
print(c.speak()) # Whiskers says Meow