Method Overriding

Method overriding allows us to completely replace the operation of a method that is defined further up in the object hierarchy as indicated by inheritance relationships.

To make sense of overriding, let's look at basic inheritance in the .net framework.

Everything in .net inherits from the .net framework system class called object.

So int, the .net integer, inherits from object, string inherits from object, and classes we create inherit from object.
The base class has built-in properties and methods, such as a ToString method.

The base ToString method returns the fully qualified string name of the class.
Any child class can use properties and methods of any class above it in the hierarchy.

Because Product and Vendor inherit from Object, they both provide the ToString method.

What if we want ToString method to do something else when it is called on a Product object?

For example, say we want to display detailed information about that product, such as the product name and ID.

Well that the purpose of overriding.
With overriding, we can completely replace the operation of a method that is defined further up in the object hierarchy.  



Comments

Popular posts from this blog

Method Overloading

Building a Method