Help with Abstract Classes please.
Code:
public abstract class Component {
int age;
public int getAge() {
return age;
}
}
Code:
public class ConrecteComponent extends Component {
public ConrecteComponent(int age) {
this.age = age;
}
}
Code:
public abstract class Decorator extends Component {
Component comp;
int extraAge;
public Decorator(Component comp) {
this.comp = comp;
}
public int getAge() {
return comp.age + extraAge;
}
}
Code:
public class ConcreteDecoratorA extends Decorator {
int extraAge = 15;
public ConcreteDecoratorA(Component comp) {
super(comp);
}
}
Code:
public class DecoratorTestClass {
public static void main(String[] args) {
ConrecteComponent myComponent = new ConrecteComponent(5);
ConcreteDecoratorA myDecorator = new ConcreteDecoratorA(myComponent);
System.out.println(myDecorator.getAge());
}
}
I am expecting the output to be 20 but the output is 5.
Why is the int is not taken into account ?
Thank you.
Re: Help with Abstract Classes please.
This is very similar to your other question. Again, you should read the inheritance java tutorials fully so that you understand it properly.
Here is the link again:
Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)