SE450: Overriding: super [16/35] Previous pageContentsNext page

super [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package subclass.ex3;

public class M {
  public static void main(String[] argv) {
    (new C()).m();
  }
}
class A {
  void p() {System.out.println("A.p");}
}
class B extends A {
  void p() {System.out.println("B.p");}
  void m() {
    System.out.println("B.m");
    this.p();  // static or dynamic binding?
    super.p(); // static or dynamic binding?
  }
}
class C extends B {
  void p() {System.out.println("C.p");}
}

Why is it like this?

file:M.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
package subclass.exB;

public class M {
  public static void main(String[] argv) {
    (new B()).m(); // does this cause an infinite loop?
  }
}
class A {
  void m() {System.out.println("A.m");}
}
class B extends A {
  void m() {
    System.out.println("B.m");
    super.m();
  }
}

file:M.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package subclass.exC;

public class M {
  public static void main(String[] argv) {
    (new C()).m(); // does this cause an infinite loop?
  }
}
class A {
  void m() {System.out.println("A.m");}
}
class B extends A {
  void m() {
    System.out.println("B.m");
    super.m();
  }
}
class C extends B {}

Previous pageContentsNext page