Junior — Middle
What are the main principles underlying object-oriented programming?
sobes.tech AI
Answer from AI
Principles of Object-Oriented Programming (OOP):
- Encapsulation — combining data and methods that operate on that data into a single object, hiding internal details.
- Inheritance — creating new classes based on existing ones to reuse code.
- Polymorphism — the ability of objects of different classes to respond to the same messages (methods) in different ways.
- Abstraction — highlighting only the important characteristics of an object, hiding complexity.
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()) # Different implementations of the same method