Ayoub Falah Ayoub Falah - 14 days ago 17
Java Question

Chicken Egg Lifecycle

I am trying to solve the following Problem Description

The solution must satisfy the following requirements:


  1. Chicken implements the
    IBird interface
    .

  2. Chicken lays an egg that hatches into other chicken.

  3. Eggs from other types of birds should hatch into that type of bird.

  4. Hatching an egg for the second time throws a
    IllegalStateException
    .



My solution looks like the following:

class Egg
{
private int numOfHatches;
Egg() {}

public Egg(Callable<IBird> createBird)
{
throw new UnsupportedOperationException("Waiting to be implemented.");
}

// To cause an egg to produce a chicken
public IBird Hatch() throws Exception
{
if (numOfHatches <= 0)
{
++numOfHatches;
return new Chicken();
}else
{
throw new IllegalStateException("An Egg can't hatch twice.");
}
}
}


Testing Code:

public static void main(String[] args) throws Exception {
Chicken chicken = new Chicken();
System.out.println(chicken instanceof IBird);
Egg egg = chicken.Lay();
IBird newChicken = egg.Hatch();
System.out.println(egg + " -> "+ newChicken);
System.out.println(newChicken instanceof IBird);
try
{
System.out.println(egg + " -> "+ egg.Hatch());

} catch (Exception e)
{
System.err.println(e.getMessage());
}
}


As anyone can see I did not use the
createBird
reference. Does anyone know How can I extend my implementation so that I use that reference and what it´s the real meaning behind that.

Thank you for any suggestions

ajb ajb
Answer

This line:

public Egg(Callable<IBird> createBird)

means that when an Egg is constructed, it should be given a functional interface object as a parameter. The Callable<IBird> class has one method, call, that takes no parameters and returns an IBird. Since it's an interface, you can tell it what you want call to do when you create the object (more on that later). Judging from the requirements, the intent here is that when you create a new Egg, you tell it what method the Egg will use to create a new bird. So if you create an Egg that will hatch a chicken, it will be a method to create a chicken; but if you create an Egg that's supposed to hatch a BlueFootedBooby, you give it a method to create a new BlueFootedBooby, or whatever.

In Java 8, you can do this easily with a lambda:

new Egg(() -> new Chicken());

The code () -> new Chicken() means "create a functional object where the method takes no parameters and returns new Chicken()". In Java 7 and earlier, you had to use a clunky syntax like

new Egg(new Callable<IBird>() {
    @Override
    public IBird call() {
        return new Chicken();
    }
}

Anyway, that's what the createBird parameter means. To use the call() method of this object, you'd say createBird.call() which returns an object of some class that implements IBird. The idea is that this will create a new bird. Of course, you don't want to create the bird when you construct the egg--you have to wait until the egg hatches, and you don't want to count your chickens before they're hatched, right? So you'll have to save the createBird in a variable.