Sobes.tech
Junior — Middle

Are the basic concepts of object-oriented programming implemented in Python?

sobes.tech AI

Answer from AI

Yes, Python supports the main concepts of object-oriented programming (OOP):

  • Classes and objects: you can create classes and instantiate objects from them.
  • Inheritance: classes can inherit properties and methods from other classes.
  • Encapsulation: supported through naming conventions (e.g., _protected and __private), although strict privacy is not enforced.
  • Polymorphism: objects of different classes can have methods with the same names, allowing them to be used interchangeably.

Example of a class in Python:

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):
    def speak(self):
        print(f"{self.name} says: Woof!")

dog = Dog("Bobby")
dog.speak()  # Outputs: Bobby says: Woof!
Are the basic concepts of object-oriented programming… - sobes.tech