Class method override In C#
Forums:
class Shape {
public virtual int GetArea() {
return ;
}
}
class Square : Shape {
public int SideLength { get; set; }
public Square(int sideLength) {
SideLength = sideLength;
}
public override int GetArea() {
if (SideLength > 0) {
return SideLength*SideLength;
}
//call base class method
return base.GetArea();
}
}
var square = new Square(5);
var area = square.GetArea();
//area is 25
- Log in to post comments