Constructors - Coffee Example

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Let's Learn

Constructors
Leave constructors,
Let's Get Some Coffee
//let's create a coffee
Coffee myCoffee = new Coffee();
//add 150 ml milk
myCoffee.addMilk(150);
//add 2 spoon sugar
myCoffee.addSugar(2);
//add 1 spoon coffee powder
myCoffee.addCoffee(1);
There it is!
Now let's try using constructors
Coffee myCoffee = new Coffee(150, 2, 1);
Woohoo!
Coffee in
just one
step!!
so what goes behind?

Class Coffee{
int milk, sugar, coffee;
Coffee();
Coffee(int milk, int sugar, int coffee){
this.milk = milk;
this.sugar= sugar;
this.coffee= coffee;
}

You might also like