Skip to content

Commit 65a3dac

Browse files
CopilotBillWagnergewarren
authored
Clarify that abstract classes can contain implemented methods alongside abstract members (#47667)
* Initial plan * Add clarification and example for abstract classes with implemented methods Co-authored-by: BillWagner <[email protected]> * Address feedback: use expression-bodied members, update syntax, and target .NET 9 Co-authored-by: BillWagner <[email protected]> * Update docs/csharp/language-reference/keywords/snippets/abstract/Program.cs * Address feedback: move to snippets/shared, rename classes, add blank line Co-authored-by: BillWagner <[email protected]> * Delete redundant abstract.csproj file as requested Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 7389cda commit 65a3dac

File tree

2 files changed

+110
-3
lines changed

2 files changed

+110
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ 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 language="csharp" source="snippets/shared/Abstract.cs" id="snippet1":::
23+
24+
In this example, the `Vehicle` abstract class provides:
25+
26+
- **Implemented members**: `GetInfo()` method, `StartEngine()` method, and constructor - these provide common functionality for all vehicles.
27+
- **Abstract members**: `Move()` method and `MaxSpeed` property - these must be implemented by each specific vehicle type.
28+
29+
This design allows the abstract class to provide shared functionality while ensuring that derived classes implement vehicle-specific behavior.
30+
31+
## Example 2
1732

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

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

2641
- An abstract class may contain abstract methods and accessors.
2742

43+
- An abstract class can also contain implemented methods, properties, fields, and other members that provide functionality to derived classes.
44+
2845
- 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.
2946

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

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

64-
## Example 2
81+
## Example 3
6582

6683
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`.
6784

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

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

80-
## Example 3
97+
## Example 4
8198

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

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

0 commit comments

Comments
 (0)