Section 11.8 Check Point Questions8 questions 

11.8.1
What is dynamic binding?
11.8.2
Describe the difference between method matching and method binding.
11.8.3
Can you assign new int[50], new Integer[50], new String[50], or new Object[50], into a variable of Object[] type?
11.8.4
What is wrong in the following code?
 1  public class Test {
 2    public static void main(String[] args) {
 3      Integer[] list1 = {12, 24, 55, 1};
 4      Double[] list2 = {12.4, 24.0, 55.2, 1.0};
 5      int[] list3 = {1, 2, 3};
 6      printArray(list1);
 7      printArray(list2);
 8      printArray(list3);
 9    }
10
11    public static void printArray(Object[] list) {
12      for (Object o: list)
13        System.out.print(o + " ");
14      System.out.println();
15    }
16  }
11.8.5
Show the output of the following code:
(a)
public class Test {
  public static void main(String[] args) {
    new Person().printPerson();
    new Student().printPerson();
  }
}

class Student extends Person {
  @Override
  public String getInfo() {
    return "Student";
  }
}

class Person {
  public String getInfo() {
    return "Person";
  }

  public void printPerson() {
    System.out.println(getInfo());
  }
}

(b)
public class Test {
  public static void main(String[] args) {
    new Person().printPerson();
    new Student().printPerson();
  }
}

class Student extends Person {
  private String getInfo() {
    return "Student";
  }
}

class Person {
  private String getInfo() {
    return "Person";
  }

  public void printPerson() {
    System.out.println(getInfo());
  }
}
11.8.6
Show the output of following program:
 1  public class Test {
 2    public static void main(String[] args) {
 3      A a = new A(3);
 4    }
 5  }
 6
 7  class A extends B {
 8    public A(int t) {
 9      System.out.println("A's constructor is invoked");
10    }
11  }
12
13  class B {
14    public B() {
15      System.out.println("B's constructor is invoked");
16    }
17  }
Is the no-arg constructor of Object invoked when new A(3) is invoked?
11.8.7
Show the output of following program:
public class Test {
  public static void main(String[] args) {
    new A();
    new B();
  }
}

class A {
  int i = 7;

  public A() {
    setI(20);
    System.out.println("i from A is " + i);
  }

  public void setI(int i) {
    this.i = 2 * i;
  }
}

class B extends A {
  public B() {
    System.out.println("i from B is " + i);    
  }

  public void setI(int i) {
    this.i = 3 * i;
  }
}
11.8.8
Show the output of following program:
public class Test {
  public static void main(String[] args) {
    Apple a = new Apple();
    System.out.println(a);
    System.out.println("---------------");
    
    GoldenDelicious g = new GoldenDelicious(7);
    System.out.println(g);
    System.out.println("---------------");

    Apple c = new GoldenDelicious(8);
    System.out.println(c);
  }
}

class Apple {
  double weight;
  
  public Apple() {
    this(1);
    System.out.println("Apple no-arg constructor");
  }
  
  public Apple(double weight) {
    this.weight = weight;
    System.out.println("Apple constructor with weight");
  }
  
  @Override 
  public String toString() {
    return "Apple: " + weight;
  }
}

class GoldenDelicious extends Apple {
  public GoldenDelicious() {
    this(5);
    System.out.println("GoldenDelicious non-arg constructor");
  }
  
  public GoldenDelicious(double weight) {
    super(weight);
    this.weight = weight;
    System.out.println("GoldenDelicious constructor with weight");
  }
  
  @Override 
  public String toString() {
    return "GoldenDelicious: " + weight;
  }
}