Skip to main content
added 352 characters in body
Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189

I think this article explains it:

https://msdn.microsoft.com/en-us/library/ms173153.aspx

basicallyBasically it depends on whether your beverage is being accessed as a Mocha or and Expresso vs what it actually is.

ieAssume you have a simple case where

Mocha : Espresso

then

((Espresso)mocha).GetDescription() == "espresso" if you 'new' rather than 'override'

so if you want to do

foreach(Beverage b in List) { //print description } 

then you should overideoverride

Now your particular example is complicated by your decorator. which also inherits from beverage.

you have

Espresso : Beverage

and

Mocha : Decorator : Beverage

so when you use 'new' on Mocha but call

((Beverage)mocha).GetDescription()

you get the Beverage result "unknown beverage"

I think this article explains it:

https://msdn.microsoft.com/en-us/library/ms173153.aspx

basically it depends on whether your beverage is being accessed as a Mocha or and Expresso vs what it actually is.

ie

((Espresso)mocha).GetDescription() == "espresso" if you 'new' rather than 'override'

so if you want to do

foreach(Beverage b in List) { //print description } 

then you should overide

I think this article explains it:

https://msdn.microsoft.com/en-us/library/ms173153.aspx

Basically it depends on whether your beverage is being accessed as a Mocha or and Expresso vs what it actually is.

Assume you have a simple case where

Mocha : Espresso

then

((Espresso)mocha).GetDescription() == "espresso" if you 'new' rather than 'override'

so if you want to do

foreach(Beverage b in List) { //print description } 

then you should override

Now your particular example is complicated by your decorator. which also inherits from beverage.

you have

Espresso : Beverage

and

Mocha : Decorator : Beverage

so when you use 'new' on Mocha but call

((Beverage)mocha).GetDescription()

you get the Beverage result "unknown beverage"

Source Link
Ewan
  • 84.5k
  • 5
  • 91
  • 189

I think this article explains it:

https://msdn.microsoft.com/en-us/library/ms173153.aspx

basically it depends on whether your beverage is being accessed as a Mocha or and Expresso vs what it actually is.

ie

((Espresso)mocha).GetDescription() == "espresso" if you 'new' rather than 'override'

so if you want to do

foreach(Beverage b in List) { //print description } 

then you should overide