In your IDE create a new project and create the following 3 classes:
An abstract model of a Kitchen is represented in the form of a class:
public class Kitchen{
private int sizeInSQM;
private String[] utensils;
private String chefName;
private int chefAge;
private boolean michelinAward;
public Kitchen(int size, String chefName, int age){
this.sizeInSQM = size;
this.chefName = chefName;
this.utensils = new String[10];
this.chefAge = age
this.michelinAward = false;
}
//sample accessor method
public String getChefName(){
return chefName;
}
//sample mutator method
public void setChefAge(){
chefAge += 1;
}
public int getNameLength(){
return chefName.length()
}
}
This seems reasonable and it will work fine.
Maybe it would be easier to manage chef-related data if we designed the Chef class. And so our new Kitchen class could look like this:
public class Kitchen{
private int sizeInSQM;
private String[] utensils;
private Chef chef;
public Kitchen(int size, Chef chef){
this.sizeInSQM = size;
this.chef = chef;
utensils = new String[10];
}
//sample accessor method
public Chef getChef(){
return chef;
}
//sample mutator method
public void setChef(Chef newChef){
this.chef = newChef;
}
}
Could we do this? Sure, why not?! Of course, now in our Java Project, we will need to design the Chef class.
Using what we know about Chefs, design the Chef class. Make sure you incorporate Data Hiding as part of the Encapsulation.
public class Chef{
//your Chef class design!
}
So, we know that a Kitchen contains a Chef! Let's consider a main program:
//Main program
Complete the main program by replacing the comments with code.
public static void main(String[] args){
//construct a Chef whose name is Joe
//construct a Kitchen (which HAS-A Chef, Joe).
//get the current Chef from the Kitchen
//display the current Chef's name - should be Joe
if(current Chef's michelin status is false){
//construct a new Chef whose name is Marie
//replace the Kitchen's current Chef with the new Chef
}else{
output Current-Chef's-Name + " is an excellent Chef!"
}
//the Kitchen is bankrupt! Assign the Kitchen reference to null
//is Joe still alive?! Try and print out his name!
}//end of main
So we can see that a Kitchen HAS-A Chef.
When an object HAS another object(s), this is known as aggregation, which is a form of containment.
The difference between aggregation and containment is:
In composition, one object owns another object. The contained object cannot exist independently. It is usually constructed in the parent class's constructor method.
If the parent object is destroyed, the contained object is destroyed too.
Think about a Room in a House. If the House is destroyed, the Room no longer exists.
In aggregation, one object uses another object. The aggregated object can exist independently.
If the parent object is destroyed, the aggregated object can still exist.
Think about a Chef in a Kitchen. If the Kitchen is destroyed, the Chef can still exist.
We can visualise the HAS-A relationship in a UML diagram:
This represents that a Kitchen HAS-A Chef.
Think of the diamond as the word HAS. It is fixed to the Kitchen. A Kitchen HAS-A Chef.
This video will help to introduce the concept of aggregation: HAS-A relationship between classes
Click the exercises button and see if you can complete the challenges.