(CNN) -- President Obama delivered the commencement address Friday to the U.S. Naval Academy's graduating class, speaking to an audience of 30,000 that included a former presidential candidate and proud parent of one of the graduates in attendance.
Jack McCain shakes hands with President Obama at Friday's commencement ceremony.
John Sidney McCain IV, more commonly known as Jack, on Friday became the fourth McCain to graduate from the Annapolis, Maryland, service academy and the fourth with the same name.
"America, look at these young men and women. Look at these sailors and Marines. Here are the values that we cherish. Here are the ideals that endure," Obama told the crowd.
About midday at the academy's commissioning and graduation ceremony, McCain received a Bachelor of Science degree, taking the oath of office and being commissioned as an ensign in the U.S. Navy 103 years after his great-grandfather did the same. Watch Obama hug John McCain's son »
With parents John and Cindy McCain watching, he shook hands with the president and then walked back into the crowd to applause and more hugs.
Joe McCain told CNN that his brother plans to join his father and great-grandfather as naval aviators.
"His grandfather, JSM Jr., was in submarines, commanding three different boats in World War II," he said.
That grandfather later became Adm. McCain and was commander of the Pacific Theater during the Vietnam War, when Jack's father, now Arizona Sen. John McCain, was being held as a prisoner of war in Hanoi.
In an interview last year with GoMids.com, Jack McCain reflected on the advice his father gave him on the day he arrived at the Naval Academy.
"My dad told me there is one thing McCains are good at, and that is not giving in to pressure, and honor -- keeping our honor regardless of what happens," Jack McCain said. "He then said, 'Don't lie, cheat or steal -- anything else is fair game.' "
John McCain, the Republican presidential nominee in 2008, frequently poked fun at his record at the Naval Academy while on the campaign trail, pointing out that he "stood fifth from the bottom" of his class and racked up dozens of demerits.
At a campaign event last October, McCain joked that Jack needed a "DNA check" because he had yet to receive a single demerit. (Jack McCain told GoMids.com that he did accumulate some demerits -- for wearing flip-flops outside and forgetting to shave).
McCain told Fox News on Thursday that his son is graduating "in better class standing than I had, which wasn't hard to do."
"I'm very proud of him," he said.
Meghan McCain, Jack's older sister, sent a message on Twitter Friday saying her younger brother, Jimmy, could not attend because he is serving overseas. McCain told Fox News he is returning home this weekend.
In his remarks to the students, Obama made a vow that he would not send them to war unless it is "absolutely necessary."
"It's a promise that as long as I am your commander in chief, I will only send you into harm's way when it is absolutely necessary, and with the strategy, the well-defined goals, the equipment and the support that you need to get the job done," he said.
"This includes the job of bringing the Iraq war to a responsible end and pursuing a new comprehensive strategy to disrupt, dismantle and defeat al Qaeda and its allies in Afghanistan and Pakistan."
Obama also promised additional assistance to military families, vowing that "we will be with you every step of the way, increasing your pay, increasing child care and helping families deal with the stress and separation of war." And he said new laws will help ensure that veterans are well taken care of.
Obama's commencement address was his third and final of the year. He was also commencement speaker at Arizona State University and the University of Notre Dame.