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 His earthly father, Joseph. It was necessary for Joseph to adopt Jesus in order for Jesus to be heir to David’s throne through the kingly lineage of Joseph.
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 a Jew (Col 2:17, Heb 8:5) served as a type of our own adoption. Very interesting.