I'm trying to understand how composition in Java works and I'm getting stuck. I have a program that instantiates a Customer object who has a Computer object as an instance variable. There are two types of Computer objects in my program, a Desktop and a Laptop subclass, so I have my Desktop class extending Computer and my Laptop class also extending Computer. I have my Computer class as an abstract class but I know that you can't instantiate an abstract object. I'm not sure how to instantiate the Computer object in my Customer class. Any insight is appreciated ! Example code:
public class Customer
{
private String name;
private Computer computer;
public Customer(String n)
{
setName(n);
this.computer = new Computer(); //Can't do this b/c Computer is abstract
}
//Setters and getters for name
public Computer getComputer()
{
return new Computer(this.computer);
}
public void setComputer(Computer computer)
{
this.computer = new Computer(computer);
}
}
public abstract class Computer{
}
public class Desktop extends Computer{
}
public class Laptop extends Computer{
}