1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is encapsulation?
Bundling data and methods that operate on the data into a single unit (class).
What’s a key benefit of encapsulation?
Protects data from unintended modification by restricting direct access.
What’s an example of encapsulation?
Using private variables and getter/setter methods in a class.
class Person:
def init(self, name):
self.__name = name # Private variable
def get_name(self): # Getter method
return self.__name
What is abstraction?
Hiding complex implementation details and exposing only what’s necessary.
What’s a key benefit of abstraction?
Simplifies usage and reduces code complexity.
What’s an example of abstraction?
Using abstract classes or interfaces.
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass # Forces subclasses to implement this method
What is inheritance?
Allowing a new class to inherit attributes and behaviors from an existing
class.
What’s a key benefit of inheritance?
Promotes code reuse and hierarchy.
What’s an example of inheritance?
Promotes code reuse and hierarchy.
class Dog(Animal):
def make_sound(self):
return "Woof!"
What is polymorphism?
Allowing different classes to be treated as instances of the same class through
a common interface.
What is a key benefit of polymorphism?
Flexibility and scalability in handling multiple object types.
What is an example of polymorphism?
Method overriding in subclasses.
def animal_sound(animal):
print(animal.make_sound()) # Can work with different
subclasses
dog = Dog()
animal_sound(dog) # Output: "Woof!"