Suppose that there is an existing StringTransformer
class
and also the following:
public interface StringTransformable { public String transform(String x); }
The following classes each extend StringTransformer
and implement StringTransformable
.
Note especially:
transform method's signature.
- The scope of the variables.
- Especially the use of
this.name
to refer to the field.
- The code conventions for indentation and curly-braces.
- I have not included comments, but comments would be important
in real code.
public class Capitalizer extends StringTransformer implements StringTransformable { public String transform(String thePhrase) { return thePhrase.toUpperCase(); } }
public class NameDropper extends StringTransformer implements StringTransformable { private String name; public NameDropper() { this.name = "Santa Claus"; } public NameDropper(String name) { this.name = name; } public String transform(String thePhrase) { return this.name + " says " + thePhrase; } }