Explain different types of Inheritance
Explain different types of Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class to inherit properties and methods from an existing class. This promotes code reusability and establishes a parent-child relationship between classes. In .NET Core, as in other OOP languages, there are several types of inheritance:
Single inheritance is when a class inherits from only one base class. This is the most straightforward and common form of inheritance. In .NET Core, this is achieved using the : symbol.
class Animal {
public void Eat() {
Console.WriteLine("Animal is eating.");
}
}
class Dog : Animal {
public void Bark() {
Console.WriteLine("Dog is barking.");
}
}
Multilevel inheritance occurs when a class is derived from another derived class, forming a chain of inheritance.
class Animal {
public void Eat() {
Console.WriteLine("Animal is eating.");
}
}
class Mammal : Animal {
public void Run() {
Console.WriteLine("Mammal is running.");
}
}
class Dog : Mammal {
public void Bark() {
Console.WriteLine("Dog is barking.");
}
}
Hierarchical inheritance is when multiple classes inherit from a single base class.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.