1 / 6

Java Upcasting and Downcasting

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. Java Upcasting and Downcasting – Test.java. class Animal {

mateja
Download Presentation

Java Upcasting and Downcasting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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); } }

  3. 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); } } 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 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 complete. In contrast, most of the type problems of Java are not manifest in a language like ML, but its type systems still holds a few (subtler) lurking problems. In short, there is still much to do before we can consider type system design a solved problem. . . . Types form a very valuable first line of defense against program errors. Of course, a poorly-designed type system can be quite frustrating: Java programming sometimes has this flavor. A powerful type system such as that of ML, however, is a pleasure to use. ML programmers, for instance, claim that programs that type correctly often work correctly within very few development iterations. Types that have not been subverted (by, for instance, casts in Java) perform several valuable roles: • When type systems detect legitimate program errors, they help reduce the time spent debugging. • Type systems catch errors in code that is not executed by the programmer. 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 deployment rather than during the testing stage. (Dually, however, passing a type checker makes many programmers construct poorer test suites—a most undesirable and unfortunate consequence!)

More Related