20-CS-694 Advanced Programming Techniques Spring 2012

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next     All lectures

Can super invoke the parent's inherited method?

Answer: Yes

Check out the following code. Method vA() in class A is invoked when super.vA() is invoked in class D. The result printed is d.vA:12.

   import java.io.*;
   import java.util.*;

   interface I { int vA(); }

   class A implements I {
      int a;

      A(int x) { a = x; }
      public int vA()  { return a; }
   class B extends A {  B(int x) { super (x+1); } }

   class D extends B implements I {  
      D(int x) { super(x+1); }
      public int vA() { return super.vA(); }

   public class PG1 {
      public static void main (String argv[]) {
         D d = new D(10);
         System.out.println("\td.vA:" + d.vA());