Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Dr. Philip Cannata 1 Dr. Philip Cannata 2 Dr. Philip Cannata 3 Java Upcasting and Downcasting Click here for example html or see the “Java Upcasting and Downcasting” link on the class webpage Click here for example code or see the Test.java link on the class webpage or see the next page. Dr. Philip Cannata 4 Java Upcasting and Downcasting – Test.java class Animal { static int value = 100; } class Cat extends Animal { int value = Animal.value + 1; } class Dog extends Animal { int value = Animal.value + 2; } public class Test { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.value); Dog d = new Dog(); System.out.println(d.value); Animal a = c; System.out.println(a.value); Object o = c; // System.out.println(o.value); a = d; type(c); type(d); type(a); // type(a.value); Dog d2 = (Dog) a; a = c; Dog d3 = (Dog) a; } public static void type(Animal a){ System.out.println("I am a " + a); } } Dr. Philip Cannata 5 Java Upcasting and Downcasting – Test.java class Animal { static int value = 100; Textbook Chapter 24 } In languages like Java, programmers think they have the benefit of a type system, but in fact many common programming patterns force programmers class Dog extends Animal { int value = Animal.value + 2; } to employ casts instead. Casts intentionally subvert the type system and leave checking for execution time. This indicates that Java’s evolution is far from public class Test { complete. In contrast, most of the type problems of Java are not manifest in a public static void main(String[] args) { language like ML, but its type systems still holds a few (subtler) lurking Cat c = new Cat(); problems. In short, there is still much to do before we can consider type System.out.println(c.value); system design a solved problem. . . . Dog d = new Dog(); System.out.println(d.value); Types form a very valuable first line of defense against program errors. Of Animal a = c; course, a poorly-designed type system can be quite frustrating: Java System.out.println(a.value); programming sometimes has this flavor. A powerful type system such as that Object o = c; of ML, however, is a pleasure to use. ML programmers, for instance, claim // System.out.println(o.value); that programs that type correctly often work correctly within very few a = d; development iterations. type(c); type(d); Types that have not been subverted (by, for instance, casts in Java) perform type(a); several valuable roles: // type(a.value); • When type systems detect legitimate program errors, they help reduce the Dog d2 = (Dog) a; time spent debugging. a = c; • Type systems catch errors in code that is not executed by the programmer. Dog d3 = (Dog) a; This matters because if a programmer constructs a weak test suite, many } parts of the system may receive no testing. The system may thus fail after public static void type(Animal a){ deployment rather than during the testing stage. (Dually, however, passing a System.out.println("I am a " + a); type checker makes many programmers construct poorer test suites—a most } undesirable and unfortunate consequence!) } class Cat extends Animal { int value = Animal.value + 1; } Dr. Philip Cannata 6