Answer :

Castles have flag because they depict the country that has hold over it.
They have flags because a flag can show one of two things : The lords coat of arms, and so which noble owns the castle the colours of the lords  mater for an example a kings coat of arms or a country's flag , showing where its allegiance lies.