What is an Abstract Property in C#? Give an example.
Short answer
An abstract property is a property declared inside an abstract class without any implementation, using the abstract keyword, which forces every non abstract class that inherits from it to provide its own concrete implementation for that property.
Advertisement
AModel answer
An abstract property is a property declared inside an abstract class without any implementation, using the abstract keyword, which forces every non abstract class that inherits from it to provide its own concrete implementation for that property. Unlike a virtual property, an abstract property has no default behavior at all in the base class, meaning the base class simply defines what the property should look like while leaving the actual logic entirely up to each derived class.
public abstract class Employee {
public abstract decimal MonthlySalary { get; }
}
public class Manager : Employee {
public override decimal MonthlySalary {
get { return 8000m; }
}
}Real-world example
A payroll system defines an abstract MonthlySalary property on an Employee base class, forcing every specific employee type such as Manager or Developer to define exactly how their own salary should be calculated.
Likely follow-ups
- Can an abstract property have a default implementation?
- What is the difference between an abstract class and an interface when it comes to properties?
Related topics