The biggest thing that has worked for me is to preempt interruptions by giving him quality attention at mealtimes and during times when I "come up for air" between deep dives. I also make a point to get up from the chair periodically, and I take those times to give him a pat on the shoulder or a hug or something.
To a kid deep concentration can look a lot like anger. I make a point to respond to him in some fashion whenever he reaches out - give him a hug or something emotional and nonverbal. A lot of these interactions are just to show him that I'm there, I'm not angry. This low-level emotional stuff usually doesn't take me out of my front-brain programming head-space.
I used to worry that I wasn't always having "real" conversions with him. He'd jabber about whatever and I'd give "uh-huh" and "that's cool" answers. But I realized a lot of that is what linguists call phatic communication. What he seems to want from those conversations was a sense of connection rather than actual deep conversation. It would be a problem if we never connected, but as long as we had some quality time it seems okay if some of our conversations are on "autopilot".
I also talk about my work with him. For me it's like having a "rubber ducky" convo and he tells me it sounds like Star Trek technobabble. It gives him a sense that I'm doing complicated things inside my head but I still care about him.
But I have kids and we homeschool. My kids see me all day every day. My dad worked in an office from dawn till dusk. When I saw him at the end of the day, he was worn out and had no energy for irritating kids. I don't want that to be the experience my kids have.
Sometimes I'll go work from a coffeeshop or the library or whatever, if I feel like I can't handle the distraction. It's a balance. I'm never going to be a rockstar developer, I can only hope to be competent, and to hope that employers will value that.
My kids will only be kids once. Maybe in 5-8 years, I'll change it up. But for now, the distractions and the lowered productivity are worth it when I look at the big picture.
Separate room with closed door is the minimum if you have kids at home. Headphones with noise isolation are a cheap investment for a software engineer.
If adults or teenagers are actively interrupting, that’s more of an interpersonal issue that should be solved as such.
Part of it is having mature enough adults all the way around that can respect boundaries - if the door is closed, the room is not available. If the door is open, the room is available (even during work hours, let's say when I'm just catching up on emails.)
Something I've learned is there does need to be a little bit of negotiation - barring the room off for basically all of the working day isn't reasonable. So having those "up for air" moments is both a two-fold gift to myself (to remind me to get up, walk around, look at something else), and also a time where they can come in to talk and do their own projects on the family computer.
It does mean a lot of my heavier non-work drawing focus time gets shifted to night but them's the breaks for free rent. :)
If you don't want to or are unable to create said environment then you either go back to the office or accept interruptions.
People like me who live alone (or maybe only had one quiet roommate) were getting a lot of work done. Meanwhile, at one point, many parents literally suddenly had two jobs -- their normal one and then being a teacher at the same time.
What was amazing to me was that almost everyone in this discussion assumed that the other side had the same situation as them. It did not occur to most people with families that some people had an apartment to themselves. Or that rarely was considered in the arguments.
But the point is, I live alone, my family rarely calls, and if I am busy then my phone is often on silent. So I never had that issue at all. But your question does not consider that possibility.
This is easy for me to say but part of this is about having a conscientious and considerate family and also your own space. If it's a toddler then lock the door (obviously with someone else to supervise). If your spouse can't leave you alone after you clearly communicate that it is necessary, then you may literally need to A) work out of the house B) get a different job that is not work from home or C) get a better spouse.
We have two kids, 7m and 3y-ish, while the older is at kindergarten I do sync work with others or do focus work for about 4 hours.
When the older is back home I only do one meeting (rare) or push something that really really really needs to be pushed. After they go to bed I catch up on deep work for 2-3 hours.
If the house is noisy I will play classical music on my noise cancelling headphones. I will set 30 minute sprints with 0 distractions and I will ignore phone/email/slack. Then, take a break for a few minutes and resume.