Skip to main content
added 273 characters in body
Source Link
davewp
  • 111
  • 3

JAVA

"B" overrides "foo()", but invokes "super.foo()". Because the overridden "foo()" in "A" is invoked by an instance of "B", the "this" reference is pointing to "B", not "A". Therefore, when I create another instance of "this", another "B" object is created.

public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        void foo() { 
            try {
                this.getClass().newInstance();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        void foo() { super.foo(); }
    }
}
public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        void foo() { 
            try {
                this.getClass().newInstance();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        void foo() { super.foo(); }
    }
}

JAVA

"B" overrides "foo()", but invokes "super.foo()". Because the overridden "foo()" in "A" is invoked by an instance of "B", the "this" reference is pointing to "B", not "A". Therefore, when I create another instance of "this", another "B" object is created.

public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        void foo() { 
            try {
                this.getClass().newInstance();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        void foo() { super.foo(); }
    }
}
deleted 81 characters in body
Source Link
davewp
  • 111
  • 3
public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        public void foo() { 
            try {
                Method m = this.getClass().getSuperclass().getMethod("foo", null);
                m.invokenewInstance(this, null);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        public void foo() { super.foo(); }
    }
}
public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        public void foo() { 
            try {
                Method m = this.getClass().getSuperclass().getMethod("foo", null);
                m.invoke(this, null);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        public void foo() { super.foo(); }
    }
}
public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        void foo() { 
            try {
                this.getClass().newInstance();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        void foo() { super.foo(); }
    }
}
Source Link
davewp
  • 111
  • 3

public class Main {  
  public static void main(String[] args) {
    new B();
  }

   static class A {       
        public void foo() { 
            try {
                Method m = this.getClass().getSuperclass().getMethod("foo", null);
                m.invoke(this, null);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    static class B extends A {
        B() { foo(); }
        
        public void foo() { super.foo(); }
    }
}