Intern — Junior
Can you explain the main concepts of object-oriented programming?
sobes.tech AI
Answer from AI
Object-Oriented Programming (OOP) core concepts:
-
Encapsulation — combining data and methods that operate on that data into a single object, hiding internal implementation from the outside world.
-
Inheritance — a mechanism that allows creating new classes based on existing ones, inheriting their properties and behaviors.
-
Polymorphism — the ability of objects of different classes to respond to the same messages (method calls) in different ways.
-
Abstraction — highlighting essential characteristics of an object, hiding implementation details.
Example in C++:
class Animal {
public:
virtual void sound() { std::cout << "Some sound" << std::endl; }
};
class Dog : public Animal {
public:
void sound() override { std::cout << "Woof" << std::endl; }
};
int main() {
Animal* a = new Dog();
a->sound(); // Will output "Woof" thanks to polymorphism
delete a;
}