Inheritance in Practice
Inheritance is a core concept in object-oriented programming, and in C#, it allows you to create new classes that build upon existing ones. This mechanism helps you reuse code, organize your application logically, and specialize behaviors without duplicating logic. When you use inheritance, you define a base class that provides common members, and then create derived classes that inherit from the base, gaining its properties and methods while adding or modifying features as needed. This approach is especially useful for modeling real-world relationships, such as a general Vehicle class and more specific types like Truck or Car.
- Base class: a class whose members are inherited by another class;
- Derived class: a class that inherits members from a base class;
- Method overriding: providing a new implementation of a virtual method in a derived class.
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using System; namespace ConsoleApp { public class Vehicle { public string Make { get; set; } public string Model { get; set; } public Vehicle(string make, string model) { Make = make; Model = model; } public virtual void DisplayInfo() { Console.WriteLine($"Vehicle: {Make} {Model}"); } } public class Truck : Vehicle { public int PayloadCapacity { get; set; } public Truck(string make, string model, int payloadCapacity) : base(make, model) { PayloadCapacity = payloadCapacity; } public override void DisplayInfo() { Console.WriteLine($"Truck: {Make} {Model}, Payload: {PayloadCapacity}kg"); } } public class Program { public static void Main(string[] args) { Vehicle vehicle = new Vehicle("Toyota", "Corolla"); vehicle.DisplayInfo(); Truck truck = new Truck("Ford", "F-150", 1200); truck.DisplayInfo(); } } }
In this example, you see two classes: Vehicle as the base class, and Truck as the derived class. The Truck class inherits the Make and Model properties, as well as the DisplayInfo method, from Vehicle. By using the : base(make, model) syntax in the Truck constructor, you ensure that the base class is properly initialized. The Truck class also introduces a new property, PayloadCapacity, and overrides the DisplayInfo method to include additional information specific to trucks. Method overriding is achieved using the virtual keyword in the base class and the override keyword in the derived class, allowing the derived class to provide a specialized implementation.
Shape.cs
12345678910111213141516171819202122232425262728293031323334353637383940namespace ConsoleApp { public abstract class Shape { public abstract double Area(); } public class Rectangle : Shape { public double Width { get; set; } public double Height { get; set; } public Rectangle(double width, double height) { Width = width; Height = height; } public override double Area() { return Width * Height; } } public class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { Radius = radius; } public override double Area() { return Math.PI * Radius * Radius; } } }
When designing class hierarchies, inheritance should be used thoughtfully. Favor inheritance when there is a clear "is a" relationship, such as a Truck "is a" Vehicle. Avoid deep or overly complex inheritance chains, as they can make maintenance difficult and reduce code clarity. Prefer composition over inheritance if the relationship is more about functionality than identity. Always use method overriding to provide specialized behavior in derived classes, and remember to mark base class methods as virtual if you intend them to be overridden. These practices help you create flexible and maintainable object-oriented code.
1. What is a base class?
2. How does a derived class extend functionality?
3. What is method overriding?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 4.17
Inheritance in Practice
Veeg om het menu te tonen
Inheritance is a core concept in object-oriented programming, and in C#, it allows you to create new classes that build upon existing ones. This mechanism helps you reuse code, organize your application logically, and specialize behaviors without duplicating logic. When you use inheritance, you define a base class that provides common members, and then create derived classes that inherit from the base, gaining its properties and methods while adding or modifying features as needed. This approach is especially useful for modeling real-world relationships, such as a general Vehicle class and more specific types like Truck or Car.
- Base class: a class whose members are inherited by another class;
- Derived class: a class that inherits members from a base class;
- Method overriding: providing a new implementation of a virtual method in a derived class.
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using System; namespace ConsoleApp { public class Vehicle { public string Make { get; set; } public string Model { get; set; } public Vehicle(string make, string model) { Make = make; Model = model; } public virtual void DisplayInfo() { Console.WriteLine($"Vehicle: {Make} {Model}"); } } public class Truck : Vehicle { public int PayloadCapacity { get; set; } public Truck(string make, string model, int payloadCapacity) : base(make, model) { PayloadCapacity = payloadCapacity; } public override void DisplayInfo() { Console.WriteLine($"Truck: {Make} {Model}, Payload: {PayloadCapacity}kg"); } } public class Program { public static void Main(string[] args) { Vehicle vehicle = new Vehicle("Toyota", "Corolla"); vehicle.DisplayInfo(); Truck truck = new Truck("Ford", "F-150", 1200); truck.DisplayInfo(); } } }
In this example, you see two classes: Vehicle as the base class, and Truck as the derived class. The Truck class inherits the Make and Model properties, as well as the DisplayInfo method, from Vehicle. By using the : base(make, model) syntax in the Truck constructor, you ensure that the base class is properly initialized. The Truck class also introduces a new property, PayloadCapacity, and overrides the DisplayInfo method to include additional information specific to trucks. Method overriding is achieved using the virtual keyword in the base class and the override keyword in the derived class, allowing the derived class to provide a specialized implementation.
Shape.cs
12345678910111213141516171819202122232425262728293031323334353637383940namespace ConsoleApp { public abstract class Shape { public abstract double Area(); } public class Rectangle : Shape { public double Width { get; set; } public double Height { get; set; } public Rectangle(double width, double height) { Width = width; Height = height; } public override double Area() { return Width * Height; } } public class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { Radius = radius; } public override double Area() { return Math.PI * Radius * Radius; } } }
When designing class hierarchies, inheritance should be used thoughtfully. Favor inheritance when there is a clear "is a" relationship, such as a Truck "is a" Vehicle. Avoid deep or overly complex inheritance chains, as they can make maintenance difficult and reduce code clarity. Prefer composition over inheritance if the relationship is more about functionality than identity. Always use method overriding to provide specialized behavior in derived classes, and remember to mark base class methods as virtual if you intend them to be overridden. These practices help you create flexible and maintainable object-oriented code.
1. What is a base class?
2. How does a derived class extend functionality?
3. What is method overriding?
Bedankt voor je feedback!