The castle structure was well known in the Middle Ages, but its origin is older and has precedents in classical Greece. A mere wooden barrier was used as a defensive fence, but the evolution of weaponry and military techniques made this system useless; later on, the solidity of the stone constructions and the height of the walls was relied on.
During the Middle Ages, the castle not only served military functions but also served as a residence to the nobles and the kings themselves, eventually turning into a real fortified palace.
Although castle could be located in urban centers, it is common for it to be located in strategic places, usually at high points and close to a water source for supply, from where it could organize the defense of itself and of the homes that depended on it.
Because of many battles and presence of unregulated violent acts during Middle Ages, the castle was an essential element of the European landscape during the Early and even late Middle Ages. For five hundred years (X-XV centuries) it was the dominant material element in Western Europe. It was the symbol of the military power of the feudal lords and military orders but also of the kings who, as they increased their power, increased their control over the territory and its borders, consolidating a dense network of royal castles.
The castle had an evident defensive function, guaranteeing a safe space that was very difficult to conquer (the techniques of a conquest of fortresses in the Middle Ages were very diverse, and you can study them in this link). But the medieval fortress was also offensive and could constitute a base of operations against the outside and, therefore, was also made to attack.
In Europe, the first castles were made of wood, since the use of the stone did not become frequent until XI century and only became predominant in the following century.
The classic stone castle had a number of different elements that could vary according to times and geographical areas.
A symbolic element of the castle was the tower of the homage, main tower, the master’s residence and last place of defense in case of siege.
The different innovations that appeared throughout the medieval centuries were basically aimed at three objectives: that the attackers could not hold or cling onto the wall, to avoid the tunneling and to reduce the effectiveness of the attacking projectiles and siege weapons.
Most of the castles also had a small porch or small door, generally in a little visible place and dedicated exclusively to the secret escape of the royalty.
From the sixteenth century, with the decline of feudalism and the consolidation of absolutist monarchies, the nobility who owned the castles were abandoned in exchange for palace mansions in court. For this reason, and because they were obsolete in their military function, the castles lost all interest, and most of them fell to the current ruins.