In the field of object-oriented programming, inheritance is a central aspect. With it, you can inherit from existing classes and add new functionalities in subclasses (subclasses). In this tutorial, you will focus on creating a specific subclass, the "Truck", to learn the basics of inheritance in C# in a practical manner.

Key Insights

  • Inheritance allows you to create a subclass from a base class.
  • Constructors are crucial for defining specific properties of a subclass.
  • The methods of the base class can be overridden in the subclass to define specific behaviors.

Step-by-Step Guide

Step 1: Use the Solution Explorer and Add Class

To create the Truck subclass, open your Solution Explorer. Here, right-click on your project and select the "Add" option. Choose to add a new class and name it "Truck." This class will contain all the properties and methods that are specific to trucks.

Create a subclass for trucks in C# and implement toll calculation

Step 2: Create Private Variable

In the Truck class, the next step is to create a private variable that will store the number of axles. This information is important as it plays a role in calculating toll fees. You can name the variable "numberOfAxles." This defines the first part of your truck's specific properties.

Step 3: Create Subclass Constructors

Now it's time to create the constructors for your Truck class. You will start with the overloaded constructor method. This method accepts parameters such as model, fuel consumption, and tank capacity. You will pass these values to the constructor method of the base class Car. You must ensure that the number of axles is also defined.

Step 4: Derive Subclass from Base Class

To ensure that the Truck class is recognized as a subclass of the base class Car, you append the base class in the header of the Truck class using a colon. This establishes the relationship between the Truck class and the base class Car, granting you access to their methods and properties.

Create a subclass for trucks in C# and implement toll calculation

Step 5: Create Second Subclass Constructor Method

In addition to the overloaded method, create a regular constructor method. This should have model, fuel consumption, and tank capacity as parameters but should not accept the number of axles as input. Instead, it will be set to two by default to handle the most common scenario for a truck.

Step 6: Override Drive Method

The next step is to override the Drive method, which is defined in the base class Car. In this method, you will calculate the travel costs based on the distance traveled, vehicle parameters, and whether the truck was driven on a highway. You need to ensure that the toll costs are calculated based on the number of axles.

Step 7: Use a Switch Statement to Calculate the Toll

Here, you can introduce a switch statement that handles different cases for the number of axles. This is important since trucks with varying numbers of axles have different toll rates. You can also use alternative control structures, but the switch statement provides clarity and readability here.

Step 8: Call the Subclass Constructor

Now that your Truck class is defined, you can create an instance of the class in the CS program. To do this, call the constructor of the Truck class to create a new Truck object that contains all the necessary parameters. You will also specify how much fuel the truck consumes and what specific properties it has.

Create a subclass for trucks in C# and implement toll calculation

Step 9: Drive the Truck and Output the Travel Costs

After you have instantiated the truck, let it drive by calling the Drive method. Pass the distance traveled and whether the truck was driven on the highway or not. Finally, output the travel costs calculated based on the number of axles in the console.

Create a subclass for trucks in C# and implement toll calculation

Summary – Creating Truck Subclass in C#

In this tutorial, you learned how to create a Truck subclass in C#. You gained knowledge of the basics of inheritance and applied it by implementing private variables and constructors for the subclass. You were able to override methods and define specific behaviors for trucks. With these skills, you can now write more complex object-oriented programs in C#.

Frequently Asked Questions

What is the difference between a class and a subclass?A class is a general template model, while a subclass inherits specific properties and methods from the base class.

How do I create a constructor in C#?A constructor is defined by the class it creates and has the same name as the class without a return type.

Can I override a method multiple times in the subclass?Yes, you can override a method in the subclass. This is useful to define specific behavior for different subclasses.

How do I use the switch statement in C#?The switch statement allows you to control different code paths based on the value of a variable. It is an effective way to avoid complex branching.