❔ C# Inheritance
Quick question, is there a way to achieve this in C#?
I need derived classes of the Beverage class to have a concrete implementation of GetDescritpion method. Classes that derive from CondimentDecorator should implement their own GetDescription method.
9 Replies
You must add the key word
virtual
to the GetDescription()
method in Beverage
and then you can you override the method in the derived class with the override
keyword. Another way is that you use the new
keyword in the method in the derived class.If I make Beverage GetDescription method virtual then all derived classes can override it which isn't the goal.
Even if you don't make the method
virtual
, a derived class can always override it with new
@Gospodin I am unsure what your goal is in that case given the current description
what is it you're actually trying to solve?
I am not trying to solve anything concrete, rather I am curious about syntax. In the given example, classes that derive from Beverage should inherit GetDescription method and it's implementation from base class. But classes that inherit from CondimentDecorator should be able to override, that is, have their own implementation of Beverage's GetDescription method.
I am having this question since I am reading head first in design patterns book which is written in Java:
if i understood this well, this is what you want to accomplish
if the method in Test2 is excluded/removed then it works with the method inherited from Beverage
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.