Java Programming Tutorial - - Composition




public class Coffee{
private int month; 
private int day; 
private int year; 
public Coffee(int m, int d, int y) {
month =m;
day=d;
year =y;
System.out.printf("The constructor for this is %s\n",this);
}
public String toString() {
return String.format("%d/%d/%d", month, day, year);
}
}




class cup {
private String name; 
private Coffee birthday; 
public cup (String theName, Coffee theDate) {
name = theName;
birthday = theDate;
}
public String toString() {
return String.format("My name is %s, my birthday is %s", name, birthday);
}
}





public class apples{
public static void main(String args[]) {
Coffee CofObject = new Coffee (4,5,6);
cup cupObject = new cup("Thileban",CofObject);
System.out.println(cupObject);
}
}

Comments