Middle+
What is object-oriented programming?
sobes.tech AI
Answer from AI
Object-Oriented Programming (OOP) is a programming paradigm based on the use of objects, which combine data and methods for working with that data.
Main concepts of OOP:
- Classes and objects: A class is a template or description of an object, and an object is a specific instance of a class.
- Encapsulation: Hiding the internal implementation of an object and providing access through methods.
- Inheritance: The ability to create new classes based on existing ones, inheriting their properties and methods.
- Polymorphism: The ability of objects of different classes to respond to the same messages (methods) in different ways.
Example in C#:
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal sound");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Woof");
}
}
// Usage
Animal myDog = new Dog();
myDog.Speak(); // Outputs "Woof"
OOP helps structure code, improves reusability, and support.