C
C#2y ago
Gospodin

❔ 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
Suiram1
Suiram12y ago
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.
Gospodin
GospodinOP2y ago
If I make Beverage GetDescription method virtual then all derived classes can override it which isn't the goal.
Suiram1
Suiram12y ago
Even if you don't make the method virtual, a derived class can always override it with new
RubyNovaDev
RubyNovaDev2y ago
@Gospodin I am unsure what your goal is in that case given the current description what is it you're actually trying to solve?
Gospodin
GospodinOP2y ago
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.
Gospodin
GospodinOP2y ago
I am having this question since I am reading head first in design patterns book which is written in Java:
Moods
Moods2y ago
Moods
Moods2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server