Junior — Middle
What is the main goal of using object-oriented programming?
sobes.tech AI
Answer from AI
The main goal of object-oriented programming (OOP) is to structure code in a way that makes it more understandable, maintainable, and extendable by modeling real-world entities as objects.
OOP allows combining data and methods that operate on that data into unified structures — classes and objects. This promotes:
- Encapsulation — hiding internal implementation and protecting data.
- Inheritance — reusing code and creating class hierarchies.
- Polymorphism — the ability to use objects of different classes through a common interface.
Example in Python:
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof"
class Cat(Animal):
def speak(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(animal.speak())
This simplifies extending functionality and maintaining the code.