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 1099357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:43:28+00:00 2026-05-17T00:43:28+00:00

i have got below code which is working fine: public abstract class Beverage {

  • 0

i have got below code which is working fine:

  public abstract class Beverage
{
    public string description = "Unknown beverage";
    public virtual  string getDescription()
    {
        return description;
    }
    public abstract double cost();
}

public abstract class condimentDecorator : Beverage
{
   // public abstract string getDescription();
}

public class DarkRoast : Beverage
{
    public DarkRoast()
    {
        description = "DarkRoast";
    }
    public override  double cost()
    {
        return 2.10;
    }
}
public class Espresso : Beverage
{
    public Espresso()
    {
        description = "Espresso";
    }
    public override double cost()
    {
        return 1.99;
    }
}

public class HouseBlend : Beverage
{
    public HouseBlend()
    {
        description = "House Blend Coffee";
    }
    public override double cost()
    {
        return .89;
    }
}


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

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

public class Soy : condimentDecorator
{
    Beverage beverage;
    public Soy(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public override string getDescription()
    {
        return beverage.getDescription() + ", Soy";
    }
    public override double cost()
    {
        return .10 + beverage.cost();
    }
}

public class Whip : condimentDecorator
{
    Beverage beverage;
    public Whip(Beverage beverage)
    {
        this.beverage = beverage;
    }

    public override string getDescription()
    {
        return beverage.getDescription() + ", Whip";
    }
    public override double cost()
    {
        return .10 + beverage.cost();
    }
}

I am using it in this way:

 protected void Page_Load(object sender, EventArgs e)
    {
        Beverage beverage2 = new DarkRoast();
        beverage2 = new Mocha(beverage2);
        beverage2 = new Mocha(beverage2);
        beverage2 = new Whip(beverage2);
        Response.Write ("<br> " + beverage2.getDescription() + " : $" + beverage2.cost().ToString());
    }

Problem: i want all child class of “condimentDecorator” to forcefully override getDescription() funciton, for that i have written below code in “condimentDecorator” class:

 public abstract string getDescription();

but that makes changes in my current functioning and not give desired result it just shows “Unknown beverage” as value of getDescription() which is value of parent most class.

Normal result:

DarkRoast, Mocha, Mocha, Whip : $2.6 

Result after using “public abstract string getDescription()”:

Unknown beverage : $2.6 

Please suggest me what should i write/change so that i can force child classes of “condimentDecorator” to override “getDescription();” and also gets resutl as its working without it.

  • 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-17T00:43:29+00:00Added an answer on May 17, 2026 at 12:43 am

    I think your class hierarchy could use some rethinking.

    How about this:

    My suggestion is that you create an interface, IBeverage that you implement on everything that is drinkable. Then you create a base class for the “fundamental” beverages – DarkRoast, Espresso, HouseBlend – just like you did now.

    For the Condiments, you implement a new abstract base class implementing IBeverage, but not providing a default GetDescription implementation. This class could also take a IBeverage in its constructor to force other condiments to do the same.

    Something like this should work I think (untested, uncompiled – but you get the idea)

    public interface IBeverage
    {
      string GetDescription ();
    }
    
    public abstract class BeverageBase : IBeverage
    {
      public virtual string GetDescription () { return "Unknown Beverage"; }
    }
    
    public class DarkRoast : BeverageBase { ... }
    public class HouseBlend : BeverageBase { ...}
    
    public abstract class CondimentBase : IBeverage
    {
      public CondimentBase(IBeverage beverage)
      {
        Beverage = beverage;
      }
      protected IBeverage Beverage { get; set; }
      public abstract string GetDescription ();
    }
    
    public class Mocha : CondimentBase 
    {
      public Mocha(IBeverage beverage)
        : base (beverage)
      { }
      public string GetDescription()
      {
        return Beverage.GetDescription() + ", Mocha";
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got ASP application, and let me clear all code is working fine
In the below code, I have a corrupt hello.bz2 which has stray characters beyond
I have got a variable which contains function hierarchy like: string str= fun1(fun2(),fun3(fun4(fun5(34,33,'aa'),'value',fun6())) //
I have got the below stored procedure to return the list of Id, parentId
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have got a form into which information is entered, and a drop down
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I have got a table [newsletter] in the db, which saves the email address,
I have got a few tables which I am trying to join. I just
Got the simplified array working see below Following up on the complicated array to

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.