<< Chapter < Page | Chapter >> Page > |
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
In some cases, multiple methods have the same name, but have different formal argument lists. These are overloaded methods.
In other cases, multiple methods have the same name, same return type, and same formal argument list. These are overridden methods.
This module concentrates on the use of method overloading to achieve compile-time polymorphism.
As you learned in an earlier module, every class in C# is a direct or indirect subclass of the class named Object . Methods defined in the Object class are inherited into all other classes. Some of those inherited methods may be overridden to make their behavior more appropriate for objects instantiated from the new subclasses. However, thismodule is not about overriding methods. Instead it is about overloading methods. I will cover method overriding in future modules.
Overloaded methods have the same name and different formal argument lists. They may or may not have the same return type.
Polymorphism manifests itself in C# in the form of multiple methods having the same name. As mentioned above, this module concentrates on method overloading , sometimes referred to as compile-time polymorphism . Subsequent modules concentrate on method overriding , sometimes referred to as runtime polymorphism .
During the compilation and execution of polymorphic code, the compiler and the runtime system must decide which of two or more methods having the same namein the same scope must be executed. With method overloading, that decision is made when the program is compiled. With method overriding, that decision isdeferred and made at runtime. Hence we have the terms compile-time polymorphism and runtime polymorphism .
Every class in C# (except for the class named Object ) extends some other class. If you don't explicitly specify the class that yournew class extends, it will automatically extend the class named Object .
Thus, all classes in C# exist in a class hierarchy where the class named Object forms the root of the hierarchy.
Some classes extend Object directly, while other classes are subclasses of Object further down the hierarchy (they extend classes that extend classes that extend Object ).
You learned in an earlier module that the class named Object defines default versions of the following methods and that every class in thehierarchy inherits them:
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?