For example image you have a Soda machine that gives you 2 types of sodas Coke and Pepsi. Let’s implement it. At first it may look as below.
Imagine few months down the line you got tired of drinking only Coke and Pepsi and wants to add Sprite. You have to create a new method in SodaMachine class called getSoda () that takes Sprite as argument, which means we have to change SodaMachine class whenever you want to add new soda, so SodaMachine class is not closed for modifications.
Now, lets apply OCP, it will look as follows.
Now if you want to add a new drink you do not have to change SodaMachine class at all (see parameter type of getSoda()).
So all you have to do is create a new class called Sprite that implements SodaType interface. This tells us that SodaMachine is closed for modifications but you can still extend its functionality for any other type of sodas.