jeudi 6 août 2020

What does it mean when interface is used as an arguments in java? [duplicate]

For example i have this interface:

public interface IceCream {
double cost();
}

And class that implements IceCream:

public class IceCreamDecorator implements IceCream{

private IceCream iceCream;
public IceCreamDecorator(IceCream iceCream) {
    this.iceCream = iceCream; // what is being tried to achieve in this constructor                                   
}
    
@Override
public double cost() {
    return this.iceCream.cost(); // Why do we return value back to interface?
 }  
}

and finally this is class that extends IceCreamDecorator:

public class VanillaIceCream extends IceCreamDecorator{

public VanillaIceCream(IceCream iceCream) {
    super(iceCream);
}

@Override
public double cost() {

    return 1.0 + super.cost();
 }  
}

Can someone please explain what the goal here? And what is going on behind the scene?

Aucun commentaire:

Enregistrer un commentaire