Java Reference
In-Depth Information
1 public class Whale extends Mammal {
2 public void method1() {
3 System.out.println("spout");
4 }
5
6 public String toString() {
7 return "BIG!";
8 }
9 }
1 public class Squid extends SeaCreature {
2 public void method2() {
3 System.out.println("tentacles");
4 }
5
6 public String toString() {
7 return "squid";
8 }
9 }
What output is produced by the following code fragment?
public static void main(String[] args) {
SeaCreature[] elements = {new Squid(), new Whale(),
new SeaCreature(), new Mammal()};
for (int i = 0; i < elements.length; i++) {
System.out.println(elements[i]);
elements[i].method1();
elements[i].method2();
System.out.println();
}
}
12. Using the classes from the previous problem, write the output that is produced by the following code fragment:
public static void main(String[] args) {
SeaCreature[] elements = {new SeaCreature(),
new Squid(), new Mammal(), new Whale()};
for (int i = 0; i < elements.length; i++) {
elements[i].method2();
System.out.println(elements[i]);
elements[i].method1();
System.out.println();
}
}
Search WWH ::




Custom Search