}
Bear in mind that when overriding a method in the base class, the new method must have the same signature (parameter) as the overridden method. For example, the following is not allowed because the new Perimeter()
method has a single input parameter, but this signature does not match that of the Perimeter()
method defined in the base class (Shape
):
public class Circle : Shape {
//...
}
}
If you need to implement another new method also called Perimeter()
in the Circle
class but with a different signature, use the new keyword, like this:
public class Circle : Shape {
//...
}
}
When a class has multiple methods each with the same name but a different signature (parameter), the methods are known asPerimeter()
method of the Circle
class is now overloaded (see Figure 6-2). Note that IntelliSense shows that the first method is from the Shape
base class, while the second one is from the Circle
class.

Figure 6-2
See the 'Overloading Methods' section later in this chapter.
Sealed Classes and Methods
So far you've seen the class definition for Shape
, Rectangle
, and Circle
. Now let's define a class for the shape Square
. As you know, a square is just a special version of rectangle; it just happens to have the same length and width. In this case, the Square
class can simply inherit from the Rectangle
class:
public class Square : Rectangle {}
You can instantiate the Square
class as per normal and all the members available in the Rectangle
would then be available to it:
Square s = new Square();
s.length = 5;
s.width = 5;
Console.WriteLine(s.Perimeter()); //---20---
Console.WriteLine(s.Area()); //---25---
To ensure that no other classes can derive from the Square class, you cansealed
keyword. A class prefixed with the sealed
keyword prevents other classes inheriting from it. For example, if you seal the Square
class, like this:
public sealed class Square : Rectangle {}
The following will result in an error:
//---Error: Square is sealed---
A sealed class cannot contain virtual methods. In the following example, the Square
class is sealed, so it cannot contain the virtual method called Diagonal()
:
public sealed class Square : Rectangle {
}
This is logical because a sealed class does not provide an opportunity for a derived class to implement its virtual method. By the same argument, a sealed class also cannot contain abstract methods:
public sealed class Square : Rectangle {
}
You can also seal methods so that other derived classes cannot override the implementation that you have provided in the current class. For example, recall that the Rectangle
class provides the implementation for the abstract Area()
method defined in the Shape
class:
public class Rectangle : Shape {
public override double Area() {
return this.length * this.width;
}
}
To prevent the derived classes of Rectangle
(such as Square
) from modifying the Area()
implementation, prefix the method with the sealed
keyword:
public class Rectangle : Shape {
return this.length * this.width;
}
}
Now if you try to override the Area()
method in the Square
class, you get an error:
public sealed class Square : Rectangle {
}