A sealed class is one that cannot serve as base for another class. To mark a class as sealed, type the sealed keyword to the left of the class keyword. Here is an example: public sealed class Ball { public int TypeOfSport; public string Dimensions; } There is not much to do about a sealed class. Simply remember that no class can be derived from it.
|
|
An interface is a special class whose purpose is to serve as a template that actual classes can be based on. To create an interface, instead of the class keyword, you use the interface keyword. By convention, the name of an interface starts with I. Here is an example: interface ICourtDimensions { }
In an interface, you cannot declare fields like those we have used in other classes. Instead, if you want some type of member variable, you can create a property. If you create a property in an interface, you cannot define that property. One of the rules of an interface is that you cannot define any of its members. This is also valid for its properties. Therefore, if you create a property in an interface:
In the same way, you can create as many properties as you judge necessary in an interface. Besides the properties, an interface can also have other types of members such as methods.
An interface is used to lay a foundation for other classes. For this reason, it is the prime candidate for class derivation. To derive from an interface, use the same technique we have applied in inheritance so far. Here is an example of a class named SportBall that derives from an interface named ISportType: <script runat="server"> public class SportBall : ISportType { int players; string sport; } </script> Just as you can derive a class from an interface, you can create an interface that itself is based on another interface. Here is an example: <script runat="server"> public interface ISportType : IBall { SportCategory Type { get; } } </script> The C# language doesn't allow multiple inheritance, which is the ability to create a class based on more than one class. Multiple inheritance is allowed only if the bases are interfaces. To create multiple inheritance, separate the names of interface, with a comma. Here is an example: <script runat="server"> public interface ISportType : IBall, ICourtDimensions { SportCategory Type { get; } } </script> You can also involve a class as parent in a multiple inheritance scenario but there must be only one class. Here is an example in which a class called Sports derives from one class and various interfaces: <script runat="server"> public interface Sports: Player, IBall, ICourtDimensions { } </script>
After creating an interface, you can derive other interfaces or other classes from it. If you are deriving other interfaces from an interface, you can just proceed as you see fit. For example, you can add or not add one or more new properties and you can add or not add one or more methods. If you derive a class, from an interface, you must implement all properties that were created in the interface. In all of the classes we have defined so far, we were using a single file to implement the class. In C#, you can create a class (the same class) in different files. This means that you can start a class in one file and continue it in another file or in other files. This is referred to as partial implementation. To create a class in various files, start the class in one file but precede the class keyword with partial. If you had created a partial class, or you got a partial class from somebody (not as part of a DLL or nor from another type of library), and you find out that the class is not complete, you can then complement it. You can simply start another file and continue the class in it. Two other rules you must observe are that you must use the same name for the class and you must precede the class keyword with partial. |
|
|
||
Previous | Copyright © 2009-2016, FunctionX, Inc. | Next |
|