Jesus was the adopted son of Joseph (Mt 1:20), and thereby became an adopted son of David (Mt 1:6-16) and heir to throne of David. We Christians who make the resurrection (Rom 8:23, Lk 20:35) become adopted sons of God, and thereby become heirs to the world (Rev 21:7). Jesus as Jew (Col 2:17, Heb 8:5) served as a type of our own adoption.
Jesus was also physically related to David through Mary, who was a descendant of King David’s son Nathan (Lk 3:31). It was necessary that Jesus be genetically related to David in order to be an heir, for God spoke through Nathan the prophet saying,
2 Samuel 7:12 And when thy days be fulfilled, and thou shalt sleep with thy fathers, I will set up thy seed after thee, which shall proceed out of thy bowels, and I will establish his kingdom.
13 He shall build an house for my name, and I will stablish the throne of his kingdom for ever.
So, it was necessary for Jesus to be genetically related to David in order for Him to be a candidate for the throne at all. Obviously, God had the whole thing planned out, even before David, because God does NOT say “thy son after thee” which would be the case if Jesus was genetically related through Joseph, but God says, “Thy SEED after thee”. Jesus was “seed” through Mary, but not “son” through Joseph, so God is shown to be meticulously accurate once again.