Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:16:15+00:00 2026-05-24T00:16:15+00:00

Possible Repeat : Why my Virtual method is not overriden? I’ve been going through

  • 0

Possible Repeat: Why my Virtual method is not overriden?

I’ve been going through the Head First Design Patterns. On a side note, I started this book as a prerequisite to Code Complete 2. Anyway, I’m working with the Decorator pattern (the chapter can actually even be read online).

So, I have 4 classes:

  1. Beverage Class – Abstract
  2. Espresso Class – Inherits Beverage
  3. BeverageDecorator Class – Abstract
  4. Mocha Class – Inherits Beverage Decotrator

Here is the source code for classes 1, 3, 4.

Beverage Class:

    public abstract class Beverage
{
    public Beverage()
    {
        Description = "Unknown Beverage";
    }

    public String getDescription()
    {
        return Description;
    }

    public abstract double cost();

    public String Description { get; set; }

}

BeverageDecorator Class:

    public abstract class BeverageDecorator : Beverage
{
    public new abstract String getDescription();
}

Mocha Class:

    public class Mocha : BeverageDecorator
{
    Beverage beverage;

    public Mocha(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public override string getDescription()
    {
        return beverage.Description + ", Mocha";
    }

    public override double cost()
    {
        return beverage.cost() + .20;
    }
}

So they are pretty straight-forward. Then when I put this code in the Main() method I keep getting an “Unknown Beverage” description.

        static void Main(string[] args)
    {

        Beverage beverage = new Espresso();
        beverage = new Mocha(beverage);
        Console.WriteLine(beverage.Description +
            " $" + beverage.cost());

        Console.Read();

    }

Mocha goes from the class it inherits – Beverage Decorator – to the class above that – Beverage Class. Even though I have the method in the Beverage Decorator and I override it. Why is this happening? I know it has something to do with being abstract classes but I just can’t seem to figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:16:15+00:00Added an answer on May 24, 2026 at 12:16 am

    Well, there are two problems. Firstly, it looks like your Description property should call getDescription rather than the other way round. And then getDescription needs to be virtual in the Beverage class and overridden in BeverageDecorator rather than hidden.

    It doesn’t help that your code looks like a mixture of C# and Java. For example, do you really need a Description property and a getDescription method? Admittedly it’s slightly tricky to override half a property (at least, I always get confused about the exact rules) but it’s definitely odd to have camelCased names in C# code.

    Here’s an alternative implementation (no decorator class – it’s not clear how that was meant to help):

    using System;
    
    public abstract class Beverage
    {
        public Beverage()
        {
            Description = "Unknown Beverage";
        }
    
        private string description;
    
        public virtual string Description
        {
            get { return description; }
            set { description = value; }
        }
    }
    
    public class Espresso : Beverage
    {
        public Espresso()
        {
            Description = "Espresso";
        }
    }
    
    public class Mocha : Beverage
    {
        private readonly Beverage beverage;
    
        public Mocha(Beverage beverage)
        {
            this.beverage = beverage;
        }
    
        public override string Description
        {
            get { return beverage.Description + ", Mocha"; }
            set { /* Ignored */ }
        }
    }
    
    public class Test
    {
        static void Main()
        {
            Beverage espresso = new Espresso();
            Beverage mocha = new Mocha(espresso);
            Console.WriteLine(mocha.Description); // Prints Espresso, Mocha
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: img:hover not working in ie .answer_button { background:url(images/answer.png) no-repeat; width: 160px; height:
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
I know it's possible to repeat an entire texture by setting the wrap mode
Is it possible to invoke gnu parallel in a way that it would repeat
Trying not to repeat myself (to be DRY) here, help me out. =) I
I don't want to repeat alt text in title again? is this possible with
Is it possible to shorten a group by clause so that you do not
Is it possible to repeat a drawable in an ImageView? I manage to repeat
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.