Sobes.tech
Junior — Middle

What features of Python make it an object-oriented programming language?

sobes.tech AI

Answer from AI

Python is an object-oriented language due to the following features:

  • Classes and objects: Python supports defining classes using the class keyword, from which objects (instances) are created.

  • Inheritance: classes can inherit properties and methods from other classes, allowing the creation of hierarchies and code reuse.

  • Encapsulation: Python supports restricting access to attributes and methods 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, enabling their interchangeable use.

  • Instance and class methods and attributes: objects can have their own data and behavior.

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!