Can anyone explain me what's the point of overriding base class implementations (if its not abstract), because it's not ethical move to modify features of the parent class according to the wishes of derived classes. The best way is to make the base classes abstract and carry on the inheritance chain. Whereas when designing a class hierarchy initially we gather or elicit all the essential requirements/features that should be belonged to a base class. Therefore, in a middle of a project we do not need to change base class implementations. According to my knowledge, base class implementation should be a constant. Thus, under this aspect, there's no real use of "override" keyword in c#. If it's not so please explain me with an example.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Many times you will have dependencies in sub-classes, or may have to implement things differently.
There is nothing at all wrong with creating
In the example I gave, the difference between the base class and the derived class was in the mechanics of how the car accelerated, not in the values allowed for
This is a contrived example, but the principle is the same. Do use
Here's a non-abstract class which does some processing, and a sub class which only does processing at certain times of day.
At noon, it'll process both
Virtual/abstract methods and overriding are commonly used in. Template pattern.
The template pattern provides a way to define what you think would be a good general guideline for a class to follow, but you allow future implementers to be more specific, or even unique in their way of doing something.
How about a simple case where you MUST use override:
Any class that implements the IDisposable interface. (And any class that owns a resource that must be cleaned up should implement it.)
Note: The OP mention that it seems OK to you to override base implementations of abstract classes, so this answer is tailored around that assumption.
It's probably a bad idea to inherit from classes which were not designed for inheritance (see why are concrete types rarely useful as bases for further derivation). Many of the C# experts believe that C# classes should be sealed by default.
All that being said, the only difference between an abstract class and a concrete class that was designed for inheritance is that the latter can be instantiated. Alternatively, a concrete class is like an abstract class that has no methods lacking some form of default implementation. All of the design considerations that make it OK to inherit from an abstract class also make it OK to inherit from a concrete class that was designed for inheritance.
An interesting example is