Creating a Subclass in C#: A Step-by-Step Guide
In object-oriented programming, a subclass is a class that inherits properties and methods from a parent class. This allows for code reusability and organization, making it a fundamental concept in C# development. In this guide, we will walk you through the steps of creating a subclass in C#.
Step 1: Understanding Inheritance
Before we dive into creating a subclass, it is important to understand the concept of inheritance. Inheritance is the process by which a subclass can inherit properties and methods from a parent class. This allows for the subclass to have access to all the functionality of the parent class while also having the ability to add its own unique properties and methods.
Step 2: Creating the Parent Class
The first step in creating a subclass is to create the parent class. This is the class from which the subclass will inherit properties and methods. Let's create a simple parent class called "Shape" that will have two properties, "color" and "size", and a method called "Draw".
```
public class Shape
{
public string color;
public int size;
public void Draw()
{
Console.WriteLine("Drawing shape...");
}
}
```
Step 3: Creating the Subclass
Now that we have our parent class, we can create our subclass. To do this, we use the colon (:) symbol after the subclass name, followed by the name of the parent class. Let's create a subclass called "Circle" that will inherit from the "Shape" class and add its own unique property, "radius".
```
public class Circle : Shape
{
public int radius;
}
```
Step 4: Overriding Methods
In some cases, you may want the subclass to have its own version of a method defined in the parent class. This is known as method overriding and is achieved by using the "override" keyword. Let's override the "Draw" method in our "Circle" subclass to draw a circle instead of a generic shape.
```
public class Circle : Shape
{
public int radius;
public override void Draw()
{
Console.WriteLine("Drawing circle...");
}
}
```
Step 5: Accessing Parent Class Members
In our subclass, we still have access to the properties and methods of the parent class. We can use the "base" keyword to access them. Let's modify our "Draw" method to also call the parent class's "Draw" method.
```
public override void Draw()
{
base.Draw();
Console.WriteLine("Drawing circle...");
}
```
Step 6: Using the Subclass
Now that we have our subclass created, we can use it just like any other class. Let's create an instance of our "Circle" subclass and set its properties. We can also call its "Draw" method to see the result.
```
Circle myCircle = new Circle();
myCircle.color = "red";
myCircle.size = 10;
myCircle.radius = 5;
myCircle.Draw();
```
This will output:
```
Drawing shape...
Drawing circle...
```
Step 7: Additional Functionality
One of the benefits of creating subclasses is the ability to add additional functionality to the subclass. Let's add a method called "CalculateArea" to our "Circle" subclass that will calculate the area of the circle based on its radius.
```
public class Circle : Shape
{
public int radius;
public override void Draw()
{
base.Draw();
Console.WriteLine("Drawing circle...");
}
public void CalculateArea()
{
double area = 3.14 * radius * radius;
Console.WriteLine("Area of circle: " + area);
}
}
```
Step 8: Using the Additional Functionality
We can now use our "CalculateArea" method on our "myCircle" instance to calculate and display the area of the circle.
```
myCircle.CalculateArea();
```
This will output:
```
Area of circle: 78.5
```
And there you have it, a step-by-step guide on creating a subclass in C#. Remember to use inheritance carefully and thoughtfully to maintain clean and efficient code. Happy coding!