Java Reference
In-Depth Information
3 Employee[] employees = { new Employee(), new Lawyer(),
4 new Secretary(), new LegalSecretary()};
5
6 // print information about each employee
7 for (Employee e : employees) {
8 System.out.print(e.getHours() + ", ");
9 System.out.printf("$%.2f, ", e.getSalary());
10 System.out.print(e.getVacationDays() + ", ");
11 System.out.print(e.getVacationForm() + ", ");
12 System.out.println(e); // calls toString
13 }
14 }
15 }
Even if you didn't understand inheritance, you might be able to deduce some
things about the hierarchy from the classes' names and the relationships among
employees in the real world. So let's take this exercise one step further. Instead of
using descriptive names for the classes, we'll use letters so that you have to read the
code in order to determine the class relationships and behavior.
Assume that the following classes have been defined:
1 public class A {
2 public void method1() {
3 System.out.println("A 1");
4 }
5
6 public void method2() {
7 System.out.println("A 2");
8 }
9
10 public String toString() {
11 return "A";
12 }
13 }
1 public class B extends A {
2 public void method2() {
3 System.out.println("B 2");
4 }
5 }
1 public class C extends A {
2 public void method1() {
3 System.out.println("C 1");
4 }
5
Search WWH ::




Custom Search