Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Inheritance in Practice | Composition, Inheritance, and Refactoring
C# OOP Class Construction Drills

bookInheritance 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.

Note
Definition
  • 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

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using 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

Shape.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
namespace 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?

question mark

What is a base class?

Select the correct answer

question mark

How does a derived class extend functionality?

Select the correct answer

question mark

What is method overriding?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give me a simple code example of inheritance in C#?

What is the difference between inheritance and composition?

How do I decide when to use method overriding?

bookInheritance in Practice

Swipe um das Menü anzuzeigen

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.

Note
Definition
  • 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

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using 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

Shape.cs

copy
12345678910111213141516171819202122232425262728293031323334353637383940
namespace 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?

question mark

What is a base class?

Select the correct answer

question mark

How does a derived class extend functionality?

Select the correct answer

question mark

What is method overriding?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 5
some-alt