It is in the State's interest that the child not become a ward of the State. The law is not horrible, though in cases it is unfair.
With DNA testing for family histories, etc., a surprising number of people are now being discovered with different legal and biological fathers. Happens with mothers, too. After the initial shock of such findings wears off, then comes solving the mystery of who was the biological father and what was going on back whenever.
Interesting, researchers have studied how relatives discuss the features of young children. Female relatives of the mother make far more statements about the young child having features that look like those of the father and the father's family in an apparent effort to reinforce that the child is actually his. This is found in research on various culture.