JeffGrigg icon

Constructor Calls Overridden Method

JeffGrigg | PRO | 08/20/18 07:45:25 PM UTC | 0 ⭐ | 955 👁️ | Never ⏰ | []
Java 5 |

0 B

|

None

|

0 👍

/

0 👎

public class A extends Base {
    double height = 6.0;
 
    public static void main(String args[]) {
        System.out.println("hello");
        A a = new A();
    }
 
    public A() {
        super();    // [The compiler does this if you delete this line.]
        System.out.println("in A() constructor");
        showAttributes();
        System.out.println("end of A() constructor");
    }
 
    protected void showAttributes() {
        System.out.println("A.showAttributes() : " + getClass().getSimpleName() + "(height=" + height + ")");
        super.showAttributes();     // [Added this line]
    }
}
 
class Base {
    double weight = 150.0;
 
    protected Base() {
        System.out.println("in B() constructor");
        showAttributes();
        System.out.println("end of B() constructor");
    }
 
    protected void showAttributes() {
        System.out.println("B.showAttributes() : " + getClass().getSimpleName() + "(weight=" + weight + ")");
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎