I am new to magento2, I couldn't understand How image swapping works for configurable product in magento 2. How this feature implemented programatically while we change options of configurable product.
The thumbnail image in the shopping cart can be set to display the image from the configurable product record, or from the associated product.
ie: each select option or swatch is an associated Product. This can have a different image (or any other attribute) to the configurable product they are associated to.
Please read the official documentation: http://docs.magento.com/m2/ce/user_guide/catalog/product-create-configurable.html