![]() |
|
Method hiding and overriding in C#, Free online C# Programming Tutorial... |
| Lessons | C# Method Hiding and Overriding |
|
Inheritance is a very powerful feature of Object Oriented Programming, without which true OOP is nearly impossible. From your past programming experience you should have a sold underrstanding of classes, subclasses, interfaces and polymorphism etc. You must also be familiar with the new keyword and its usage. Inheritance in C# has the same meaning and implications as in Java and C++. We use : to indicate that one class inherits from the other class or a class implements an interface. public class SubClass : SuperClass Method Hiding and Overriding The main difference between hiding and overriding relates to the choice of which method to call where the declared class of a variable is different to the run-time class of the object it references. For example: //Super class method public virtual double getArea() { return length * width; } //Sub class method public override double getArea() { return length * length; } For one method to override another, the overridden method must not be static, and it must be declared as either 'virtual', 'abstract' or 'override'. Now look at the following: //Super class method public double getArea() { return length * width; } //Sub class method public new double getArea() { return length * length; } Where one method 'hides' another, the hidden method does not need to be declared with any special keyword. Instead, the hiding method just declares itself as 'new'.
Next >>> Lesson No. 18: C# Exception Handling
|