Why does a capacitor charge and discharge?
I think there's a small misconception about the properties of a capacitor. 

You probably heard, that capacitors tend to stabilise or keep a voltage within a circuit: 


  would resist a change in the voltage across its two ends


Think about the meaning of "resist" here. It does not mean, it can hold a fixed voltage against any external force. In fact a capacitor does in no way keep a voltage. The voltage of a capacitor reflects its current charge! And it reflects it linearily:
\$ U=q/C \$

How does charge change? A current flows through the terminals of a capacitor, and the charge changes. Hence the voltage changes. 

The conception of a capacitor keeping a voltage inside a circuit comes from that property. Voltage cannot change without modifying the charge. And for changing the charge a current has to flow leading to a voltage change. This is basically an energy transfer which takes time to happen in most realistic scenarios. This gives us the impression, a capacitor might resist a voltage change, but it is basically the speed of voltage change which is defined by the capacitor and its surrounding circuitry.

In your example (given all elements are ideal ones) the voltage change will take an infinitesimal time when the switch is put into position 1. This is, because there are no limiting resistors in the circuit. The current flowing through the capacitor will also be infinite for an infinitesimal time until the full voltage of the battery is reached. 

On the other hand, if switched over to 2, it will take eternally for the voltage of the capacitor reach 0 again, because of the current decreasing to an infinitesimal value extending the discharge process infinite.