Advertisement
JeffGrigg

Constructor Calls Overridden Method

Aug 20th, 2018
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.97 KB | None | 0 0
  1. public class A extends Base {
  2.     double height = 6.0;
  3.  
  4.     public static void main(String args[]) {
  5.         System.out.println("hello");
  6.         A a = new A();
  7.     }
  8.  
  9.     public A() {
  10.         super();    // [The compiler does this if you delete this line.]
  11.         System.out.println("in A() constructor");
  12.         showAttributes();
  13.         System.out.println("end of A() constructor");
  14.     }
  15.  
  16.     protected void showAttributes() {
  17.         System.out.println("A.showAttributes() : " + getClass().getSimpleName() + "(height=" + height + ")");
  18.         super.showAttributes();     // [Added this line]
  19.     }
  20. }
  21.  
  22. class Base {
  23.     double weight = 150.0;
  24.  
  25.     protected Base() {
  26.         System.out.println("in B() constructor");
  27.         showAttributes();
  28.         System.out.println("end of B() constructor");
  29.     }
  30.  
  31.     protected void showAttributes() {
  32.         System.out.println("B.showAttributes() : " + getClass().getSimpleName() + "(weight=" + weight + ")");
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement