In order not to have to duplicate code, C# and other object-oriented languages support code inheritance, by which you can use one class as the base of another class. For example, let's say that your banking Web application requires a Checking class and a Savings class. When authoring these classes you realize that aside from a few fields and functions, they have a number of members in common. It would not be practical to create the Checking class and then copy most of the code to the Savings class. Simply copying the code can result in maintenance headaches ; consider what would happen if after copying the code to five different classes, you discover a bug in one of the functions. Instead of copying and pasting code it is possible to create a class called Account. The Account class would contain all the code that every type of Account would have in common. Then you simply write a Checking class or a Savings class that derives from the Account class. Deriving from a class means that you gain all the functionality of the base class plus you can add functions that are specific to each derived class. To inherit a class from another:
Tips
|