It was narrated from ‘Amr bin Shu’aib, from his father, from his grandfather, that the Messenger of Allah (ﷺ) said:
“Every child who is attributed to his father after his father to whom he is attributed has died, and his heirs attributed him to him after he died, he ruled that* whoever was born to a slave woman whom he owned at the time when he had intercourse with her, he should be named after the one to whom he was attributed, but he has no share of any inheritance that was distributed previously. Whatever inheritance he finds has not yet been distributed, he will have a share of it. But he cannot be named after his father if the man whom he claimed as his father did not acknowledge him. If he as born to a slave woman whom his father did not own, or to a free woman with whom he committed adultery, then he cannot be named after him and he does not inherit from him, even if the one whom he claims as his father acknowledges him. So he is an illegitimate child who belongs to his mother’s people, whoever they are, whether she is a free woman or a slave.”