The former US president was speaking in Berlin alongside Angela Merkel to an audience of 70,000 people at a spot where the Berlin Wall once stood.
In what appeared to be a reference to Trump's vow to build a wall along the border with Mexico to keep out illegal immigrants and drugs, Mr Obama said it was not possible to stay isolated from troubles abroad.
Barack Obama took aim at Donald Trump as he shared the stage with Angela Merkel
In this new world that we live in we can't isolate ourselves. We can't hide behind a wall
He said: “If there are disruptions in these countries, if there is bad governance, if there is war or if there is poverty, in this new world that we live in we can't isolate ourselves. We can't hide behind a wall."
Mr Trump is yet to act on his controversial election campaign promise to build a wall along the US-Mexico border.
In his first speech in Europe since leaving the White House, Mr Obama said he had spent the last four months "trying to catch up with my sleep" and spending more time with his family.
Reunited- Barack Obama looked happy to be standing alongside Angela Merkel
Get Quotes on Home Insurance
A smiling Mr Obama and Mrs Merkel waved to the crowds
Unable to let his rivalry with Mr Trump go, the former president turned his attention to US healthcare.
He said: “I’m very proud of the work I did as president," he said to cheers, adding he was especially proud of health care reform.
“My hope was to get 100 percent of people health care. We didn't quite achieve that but we were able to get 20 million people health care who didn't have it before. Certainly I have some regrets that we weren't able to get everyone health care.
The crowds in Berlin were pleased to see Mr Obama
Security has been boosted in Berlin ahead of Mr Obama's visit
“Now some of the progress we made is in peril because a significant debate is taking place in the United States.”
Mr Trump is attempting to dismantle the so-called Obamacare.
The comments could make an already potentially awkward meeting between the US president and Mrs Merkel this afternoon even frostier.
Mr Obama’s Berlin visit has also sparked concerns he could be attempting to help the German Chancellor’s re-election campaign.
Angel Merkel's awkward kisses
Wed, June 29, 2016
German chancellor Angela Merkel is certainly one of EU's most powerful women, we take a look through the awkward embraces she has with world leaders.
1 of 17
Angela Merkel's awkward embraces
Germany goes to the polls in September with Mrs Merkel hoping to secure her fourth term in office.
Mr Obama and Mrs Merkel forged a close transatlantic alliance during eight years in office with the pair becoming firm friends.
He chose to make his final phone call as president to Mrs Merkel and in one of their final press conferences together implied he would vote for the German Chancellor.