Results 1 to 2 of 2
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Help with Abstract Classes please.

    Java Code:
    public abstract class Component {
    	int age;
    	public int getAge() {
    		return age;
    	}
    }
    Java Code:
    public class ConrecteComponent extends Component {
    	public ConrecteComponent(int age) {
    		this.age = age;
    	}
    }
    Java 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;
    	}
    }
    Java Code:
    public class ConcreteDecoratorA extends Decorator {
    	int extraAge = 15;
    	public ConcreteDecoratorA(Component comp) {
    		super(comp);
    	}
    }
    Java 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
    Java Code:
    extraAge = 15;
    is not taken into account ?

    Thank you.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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)

Similar Threads

  1. Abstract classes
    By FadedAura in forum New To Java
    Replies: 4
    Last Post: 11-21-2011, 11:16 PM
  2. interface vs abstract classes
    By chandrasekhar.melam in forum New To Java
    Replies: 1
    Last Post: 10-05-2011, 03:46 PM
  3. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 03:30 PM
  4. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 09:31 AM
  5. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •