Skip to content

Commit d5eaad9

Browse files
Update abstract.md - keyword abstract , added abstract class constructor example (#44223)
* abstract.md added information about abstract class constructor While we can't create an instance of an abstract class, we still can use its constructor - I've add an example showing that. * Update abstract.md - abstract class constructor example While we can't create an instance of an abstract class, we still can use an abstract class constructor. I've prepared an example of the use of abstract class constructor * Update abstract.md * Update abstract.md - abstract class constructor Updated remarks about using abstract class contructor * Update abstract.md - additional point about parameterless constructor in example 3 * Update csrefKeywordsModifiers.cs * Update abstract.md - example 3 put to the snippets * Update abstract.md - example code put to snippets * Update abstract.md - fixed lint issues * Update samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs * Apply suggestions from code review --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent f72a96a commit d5eaad9

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,21 @@ BaseClass bc = new BaseClass(); // Error
7474
```
7575

7676
You will get an error saying that the compiler cannot create an instance of the abstract class 'BaseClass'.
77-
77+
78+
Nonetheless, it is possible to use an abstract class constructor, as in the example below
79+
80+
## Example 3
81+
82+
[!code-csharp[csrefKeywordsModifiers#27](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#27)]
83+
84+
The `Shape` class is declared `abstract`, which means it cannot be instantiated directly. Instead, it serves as a blueprint for other classes.
85+
86+
- Even though you can't create objects of an abstract class, it can still have a constructor. This constructor is typically `protected`, meaning it can only be accessed from derived classes.
87+
In this case, the `Shape` constructor takes a `color` parameter and initializes the `Color` property. It also prints a message to the console.
88+
The `public Square(string color, double side) : base(color)` part calls the base class's constructor (`Shape`) and passes the `color` argument to it.
89+
- In the Shape class, the defined constructor takes a color as a parameter `protected Shape(string color)`. This means there's no longer a default parameterless constructor automatically provided by C# thus derived classes must use the `: base(color)` expression to invoke the base constructor. Setting the default value to color `protected Shape(string color="green")` will allow to omit the
90+
`: base(color)` expression in derived classes, still such constructor `protected Shape(string color="green")` will be invoked, setting the color to green.
91+
7892
## C# Language Specification
7993

8094
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]

samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,4 +798,49 @@ public override string Name
798798
}
799799
}
800800
//</snippet26>
801+
802+
public class AbstractExercise3
803+
{
804+
//<snippet27>
805+
public abstract class Shape
806+
{
807+
public string Color { get; set; }
808+
809+
// Constructor of the abstract class
810+
protected Shape(string color)
811+
{
812+
Color = color;
813+
Console.WriteLine("Created a shape with color {color}.");
814+
}
815+
816+
// Abstract method that must be implemented by derived classes
817+
public abstract double CalculateArea();
818+
}
819+
820+
public class Square : Shape
821+
{
822+
public double Side { get; set; }
823+
824+
// Constructor of the derived class calling the base class constructor
825+
public Square(string color, double side) : base(color)
826+
{
827+
Side = side;
828+
}
829+
830+
public override double CalculateArea()
831+
{
832+
return Side * Side;
833+
}
834+
}
835+
836+
public class Program
837+
{
838+
public static void Main(string[] args)
839+
{
840+
Square square = new Square("red", 5);
841+
Console.WriteLine("Area of the square: {square.CalculateArea()}");
842+
}
843+
}
844+
//</snippet27>
845+
}
801846
}

0 commit comments

Comments
 (0)