When were castles built in Europe?

Most of the castle in Europe were built in medieval times. They were built to protect the area from invaders and other kingdoms.