Both of them graduate during the same year. Are they both considered as the same class?
Yes and no. Yes, if there's a concept of the "Class of 20XX", then anyone graduating in 20XX will be considered part of it (regardless of when they started or how long they took). However, in my experience this terminology is much less common in graduate school. It makes some sense for programs that typically take a fixed amount of time, like medical school or law school, but it's not a particularly meaningful concept for Ph.D. programs. It's much easier to talk about a Class of 20XX if you can predict well in advance which class you'll be in.
When I was a graduate student, we primarily identified with the cohort we entered grad school with (because of time spent studying together and preparing for the qualifying exam), rather than those who ended up graduating in the same year. Nobody talked about a Class of 19XX. If anyone had, it would have been clear what it meant, but it would have sounded odd.