Originally posted by Glenny Dsilva: I am reading Head First Book on Design patterns 1. I would like to know why the getDescription() method in Condiment Decorator is marked abstract.
2. and does the instance variable gets overridden when i instantiate beverage class. (I was under the impression that instance variable are not overridden then what happens here).
1. A couple things work together to make that the right thing to do:
The default implementation in Beverage, the superclass of CondimentDecorator, doesn't provide the right functionality for a CondimentDecorator.
It forces any concrete subclass, such as Mocha, to provide a more specific implementation.
By making CondimentDecorator.getDescription() abstract, Beverage.getDescription() becomes "hidden".
2. Overridden? No, not in the sense of methods being overridden in subclasses. Each object of any of these classes will have a String instance variable named "description". The point is each object, even multiple instances of HouseBlend for instance, gets its own String. Now if description was turned into a class field with the static keyword, then the story would be different.
However, when you make a new HouseBlend object the description field is assigned a value of "Unknown Beverage", which is then immediately overwritten with "House Blend Coffee" in the HouseBlend constructor. I guess in that sense, the old value of description is "overridden" with the new value, but that's not the usual OO definition of override. [ August 10, 2005: Message edited by: Ryan McGuire ]
Originally posted by Glenny Dsilva: 1. Even after removing the abstract method getDescription() in CondimentDecorator Class I get the same output so whats the point in having that method i.e. getDescription() in CondimentDecorator;
The purpose of the abstract method is to produce compile time error should you forget to override the getDescription method in a concrete decorator.
2. The output for System.out.println(beverage2.getDescription() + beverage2.cost());
is Dark Roast,mocha,mocha 2.39 Pls. explain how mocha string is appended twice.
Well, you have decorated the beverage with mocha twice, so it gets printed twice. What did you expect?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus