Results 1 to 5 of 5
  1. #1
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    42
    Rep Power
    0

    Default Order of constructors and overriding

    This is my code. In the call to new B(), first the constructor for A is called and setI() is in there. Why it calls B.setI() while executing the constructor of A?
    Java Code:
    public class Test {
            public static void main(String[] args) {
                    new A();
                    new B(); 
            }
    }
    
    class A {
            int i = 0;
            public A() {
                    System.out.println("Constr A");
                setI(5);
        }
        public void setI(int i) {
                System.out.println("set in A");
                this.i = 2 * i;
        }
    }
    class B extends A {
            public B() {
                    System.out.println("Constr B");
            }
            public void setI(int i) {
                System.out.println("set in B");
                this.i = 5 * i;
            }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: Order of constructors and overriding

    When you override an inherited method, then that overridden method will be called when invoked by an instance of the extended class. This will happen whether you call the method directly, or indirectly. Consider the following:

    Java Code:
    class A {
        public void A1() {
           System.out.println("A1 in class A");
        }
        public void Q() {
              A1();
        }
    }
    
    class B extends A {
        public void A1() {
          System.out.println("A1 in class B");
        }
        public void R() {
             Q();
       }
    }
    When some instance of B calls R(), Q() will invoke the overridden version of A1() so the following will be printed.
    A1 in class B

    This is indirect invocation is what was happening in your constructor. A's constructor called the overridden version of the inherited method.

    Regards,
    Jim
    Last edited by jim829; 09-30-2017 at 05:12 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    42
    Rep Power
    0

    Default Re: Order of constructors and overriding

    Quote Originally Posted by jim829 View Post
    When you override an inherited method, then that overridden method will be called when invoked by an instance of the extended class. This will happen whether you call the method directly, or indirectly. Consider the following:

    Java Code:
    class A {
        public void A1() {
           System.out.println("A1 in class A");
        }
        public void Q() {
              A1();
        }
    }
    
    class B extends A {
        public void A1() {
          System.out.println("A1 in class B");
        }
        public void R() {
             Q();
       }
    }
    When some instance of B calls R(), Q() will invoke the overridden version of A1() so the following will be printed.
    A1 in class B

    This is indirect invocation is what was happening in your constructor. A's constructor called the overridden version of the inherited method.

    Regards,
    Jim
    Thanks, but still I don't get it. In the body of the constructor of A when I write setI() it will actually call B.setI()? Isn't the calling object an instance of A? Or is it because, in fact, the constructor of B will be executed as
    Java Code:
     B() { super(); ... }
    ?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,203
    Rep Power
    12

    Default Re: Order of constructors and overriding

    No object of instance A is involved. You are using an object of instance B (which has extended A). So even though A's constructor will be called, any overridden method in that constructor will be called, not the original one.

    In general, when B invokes any inherited and overridden method from A it will be calling the overridden method in B, not the one declared in class A. This is true even if the call is made from the extended (super) class.

    Here's another way to think of it. When B extends A, you have a set of methods associated with B which may be called. Some came from class A, and some came from class B. When B overrides one of those methods from A, it effectively replaces that method with another, thus changing the overall set of methods. So all calls whether coming from the subclass or the superclass will call that overridden method. The only exception (that I know) is when the overridden method is prefixed with super.

    Put the following statement in A's constructor. You will see that the active instance is not of A but of B, so B's methods will be called (including the overridden ones).

    Java Code:
    System.out.println(getClass().getName());
    Regards,
    Jim
    Last edited by Tolls; 10-02-2017 at 04:51 PM. Reason: Fix third para for jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    drimades is offline Member
    Join Date
    Mar 2012
    Posts
    42
    Rep Power
    0

    Default Re: Order of constructors and overriding

    It's clear. I didn't realize that the calling object is a B, even if the constructor of A is being executed.

Similar Threads

  1. Replies: 2
    Last Post: 01-26-2014, 03:41 PM
  2. overriding
    By sandeep chopparapu in forum New To Java
    Replies: 4
    Last Post: 05-14-2012, 08:54 AM
  3. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 07:47 AM
  4. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •