Skip to content

Commit c366f66

Browse files
CopilotBillWagner
andcommitted
Add clarification and example for abstract classes with implemented methods
Co-authored-by: BillWagner <[email protected]>
1 parent 5518bf5 commit c366f66

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

docs/csharp/language-reference/keywords/abstract.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,22 @@ ms.assetid: b0797770-c1f3-4b4d-9441-b9122602a6bb
1212
# abstract (C# Reference)
1313

1414
The `abstract` modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the `abstract` modifier in a class declaration to indicate that a class is intended only to be a base class of other classes, not instantiated on its own. Members marked as abstract must be implemented by non-abstract classes that derive from the abstract class.
15+
16+
Abstract classes can contain both abstract members (which have no implementation and must be overridden in derived classes) and fully implemented members (such as regular methods, properties, and constructors). This allows abstract classes to provide common functionality while still requiring derived classes to implement specific abstract members.
1517

16-
## Example 1
18+
## Example 1 - Abstract class with mixed members
19+
20+
The following example demonstrates an abstract class that contains both implemented methods and abstract members:
21+
22+
[!code-csharp[AbstractWithImplemented](snippets/abstract/Program.cs#snippet1)]
23+
24+
In this example, the `Vehicle` abstract class provides:
25+
- **Implemented members**: `GetInfo()` method, `StartEngine()` method, and constructor - these provide common functionality for all vehicles
26+
- **Abstract members**: `Move()` method and `MaxSpeed` property - these must be implemented by each specific vehicle type
27+
28+
This design allows the abstract class to provide shared functionality while ensuring that derived classes implement vehicle-specific behavior.
29+
30+
## Example 2
1731

1832
In this example, the class `Square` must provide an implementation of `GetArea` because it derives from `Shape`:
1933

@@ -25,6 +39,8 @@ The `abstract` modifier indicates that the thing being modified has a missing or
2539

2640
- An abstract class may contain abstract methods and accessors.
2741

42+
- An abstract class can also contain implemented methods, properties, fields, and other members that provide functionality to derived classes.
43+
2844
- It is not possible to modify an abstract class with the [sealed](./sealed.md) modifier because the two modifiers have opposite meanings. The `sealed` modifier prevents a class from being inherited and the `abstract` modifier requires a class to be inherited.
2945

3046
- A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
@@ -61,7 +77,7 @@ The `abstract` modifier indicates that the thing being modified has a missing or
6177

6278
[!code-csharp[csrefKeywordsModifiers#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#2)]
6379

64-
## Example 2
80+
## Example 3
6581

6682
In this example, the class `DerivedClass` is derived from an abstract class `BaseClass`. The abstract class contains an abstract method, `AbstractMethod`, and two abstract properties, `X` and `Y`.
6783

@@ -77,7 +93,7 @@ You will get an error saying that the compiler cannot create an instance of the
7793

7894
Nonetheless, it is possible to use an abstract class constructor, as in the example below
7995

80-
## Example 3
96+
## Example 4
8197

8298
[!code-csharp[csrefKeywordsModifiers#27](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#27)]
8399

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//<snippet1>
2+
public abstract class Vehicle
3+
{
4+
protected string _brand;
5+
6+
// Constructor - implemented method in abstract class
7+
public Vehicle(string brand)
8+
{
9+
_brand = brand;
10+
}
11+
12+
// Implemented method - provides functionality that all vehicles share
13+
public string GetInfo()
14+
{
15+
return $"This is a {_brand} vehicle.";
16+
}
17+
18+
// Another implemented method
19+
public virtual void StartEngine()
20+
{
21+
Console.WriteLine($"{_brand} engine is starting...");
22+
}
23+
24+
// Abstract method - must be implemented by derived classes
25+
public abstract void Move();
26+
27+
// Abstract property - must be implemented by derived classes
28+
public abstract int MaxSpeed { get; }
29+
}
30+
31+
public class Car : Vehicle
32+
{
33+
public Car(string brand) : base(brand) { }
34+
35+
// Implementation of abstract method
36+
public override void Move()
37+
{
38+
Console.WriteLine($"{_brand} car is driving on the road.");
39+
}
40+
41+
// Implementation of abstract property
42+
public override int MaxSpeed => 200;
43+
}
44+
45+
public class Boat : Vehicle
46+
{
47+
public Boat(string brand) : base(brand) { }
48+
49+
// Implementation of abstract method
50+
public override void Move()
51+
{
52+
Console.WriteLine($"{_brand} boat is sailing on the water.");
53+
}
54+
55+
// Implementation of abstract property
56+
public override int MaxSpeed => 50;
57+
}
58+
59+
class Program
60+
{
61+
static void Main()
62+
{
63+
// Cannot instantiate abstract class: Vehicle v = new Vehicle("Generic"); // Error!
64+
65+
Car car = new Car("Toyota");
66+
Boat boat = new Boat("Yamaha");
67+
68+
// Using implemented methods from abstract class
69+
Console.WriteLine(car.GetInfo());
70+
car.StartEngine();
71+
72+
// Using abstract methods implemented in derived class
73+
car.Move();
74+
Console.WriteLine($"Max speed: {car.MaxSpeed} km/h");
75+
76+
Console.WriteLine();
77+
78+
Console.WriteLine(boat.GetInfo());
79+
boat.StartEngine();
80+
boat.Move();
81+
Console.WriteLine($"Max speed: {boat.MaxSpeed} km/h");
82+
}
83+
}
84+
/* Output:
85+
This is a Toyota vehicle.
86+
Toyota engine is starting...
87+
Toyota car is driving on the road.
88+
Max speed: 200 km/h
89+
90+
This is a Yamaha vehicle.
91+
Yamaha engine is starting...
92+
Yamaha boat is sailing on the water.
93+
Max speed: 50 km/h
94+
*/
95+
//</snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

0 commit comments

Comments
 (0)