I'm currently taking my first Java class and we've recently gone over the topics of polymorphism and inheritance. I was trying a few things in order to get a better understanding of how things work and I was wondering if someone can help me understand why the following code gives me the output and also the issues that can arise when we combine inheritance and overriding of methods.
public class AClass {
public void m1() {
System.out.println("A.m1()");
this.m2();
}
public void m2() {
System.out.println("A.m2()");
}
public static void main(String [] args) {
AClass A = new AClass();
BClass B = new BClass();
A.m1();
A.m2();
B.m1();
public class BClass extends AClass {
@Override
public void m2() {
// TODO Auto-generated method stub
System.out.println("B.m2()");
}
}
The output that I get is:
A.m1()
A.m2()
A.m2()
A.m1()
B.m2()
Also, If I add a super.m1() call in my m2() method for class B, I get a stack overflow error. Can somebody tell me why that happens? Thanks!

m2inBClasscallsm1inAClass, which callsm2, and so on (infinite recursion).