Junior — Middle
Can you explain what inheritance means in object-oriented programming?
sobes.tech AI
Answer from AI
Inheritance in object-oriented programming is a mechanism that allows creating a new class based on an existing one. The new class (child) inherits all properties and methods of the base class, and can add its own or override inherited ones.
For example, in C#:
class Animal
{
public void Eat() => Console.WriteLine("Eating...");
}
class Dog : Animal
{
public void Bark() => Console.WriteLine("Bark!");
}
// Usage:
Dog dog = new Dog();
dog.Eat(); // inherited method
dog.Bark(); // own method
This allows code reuse and building class hierarchies that reflect "is-a" relationships.