What is concrete method?
A concrete method is a method in a class that has a complete implementation and provides specific functionality. Unlike abstract methods, concrete methods have a method body that contains the actual code to perform a certain task. These methods are "concrete" in the sense that they have a tangible and defined behavior that can be executed when the method is called.
Key characteristics of concrete methods:
-
Method Body: Concrete methods have a method body that contains statements and logic to perform a particular operation.
-
Full Implementation: Concrete methods provide complete and specific functionality. When you call a concrete method, the code within its method body is executed.
-
No Obligation to Override: Concrete methods are not meant to be overridden by derived classes. They provide a specific behavior that remains consistent across all instances of the class.
-
Can Be Inherited and Used: Concrete methods in a base class are inherited by its derived classes, and they can be called on instances of the derived classes.
-
May Be Overloaded: Concrete methods can be overloaded in the same class, meaning you can define multiple methods with the same name but different parameter lists. This allows you to provide different implementations for different scenarios.
Here's an example of a concrete method in C#:
class Rectangle {
public double Width { get; set; }
public double Height { get; set; }
public Rectangle(double width, double height) {
Width = width;
Height = height;
}
public double CalculateArea() {
return Width * Height; // Concrete method with implementation
}
public void DisplayInfo() {
Console.WriteLine($"Rectangle: Width = {Width}, Height = {Height}");
}
}
In this example, both the 'CalculateArea' and 'DisplayInfo' methods are concrete methods:
-
'CalculateArea' calculates the area of the rectangle based on its width and height.
- 'DisplayInfo' displays information about the rectangle's dimensions.
These concrete methods provide specific functionality within the 'Rectangle' class and can be called on instances of the class or its derived classes without any obligation to override them.