A beaker is filled upto brim with water at 4°C. Water overflows when beaker is either heated or cooled. Why?

Water has maximum density at 4°C. When heated or cooled, its density decrease and volume increase, which causes the water to overflow.