Outwrite0 writes a 0 byte to footxt outclose closes

This preview shows 6 out of 15 pages.

out.write(0); // writes a 0 byte to foo.txt out.close(); // closes the stream to foo.txt
Image of page 6

Subscribe to view the full document.

7 Polymorphism examples • You can use the object's extra functionality by casting. OutputStream out = new PrintStream(new File("foo.txt")); out.write(0); // ok out.println("hello"); // compiler error ((PrintStream) out) .println("hello"); // ok out.close(); // ok • You can't cast an object into something that it is not. Such code might compile, but it will crash at runtime. OutputStream out2 = new FileOutputStream("foo txt");
Image of page 7
8 Polymorphism mystery • 4-5 classes with inheritance relationships are shown. • A client program calls methods on objects of each class. Some questions involve type-casting. Some lines of code are illegal and produce errors. • You must read the code and determine its output or errors. For output, you must be precise For errors, you need only say that an error occurred (not identify what kind of error occurred) We always place such a question on our final exams!
Image of page 8

Subscribe to view the full document.

9 Polymorphism mystery • Steps to solving polymorphism mystery problems: 1. Look at the variable type. (If there is a cast, look at the casted variable type.) If the variable type does not have the requested method the compiler will report an error. 2. If there was a cast, make sure the casted variable type is compatible with the object type (i.e. ensure the object type is a subclass of the variable type). If they are not compatible, a runtime error ( ClassCastException ) will occur. 3. Execute the method in question, behaving like the object type. (The variable type and casted variable type no longer matter.)
Image of page 9
10 Exercise • Assume that the following classes have been declared: public class Snow { public void method2() { System.out.println("Snow 2"); } public void method3() { System.out.println("Snow 3"); } } public class Rain extends Snow { public void method1() { System.out.println("Rain 1"); } public void method2() { System.out.println("Rain 2"); } }
Image of page 10

Subscribe to view the full document.

11 Exercise public class Sleet extends Snow { public void method2() { System.out.println("Sleet 2"); super.method2(); method3(); } public void method3() { System.out.println("Sleet 3"); } } public class Fog extends Sleet { public void method1() { System.out.println("Fog 1"); } public void method3() { System.out.println("Fog 3"); } }
Image of page 11
12 Exercise What happens when the following examples are executed? Example 1: Snow var1 = new Sleet(); var1.method2(); Example 2: Snow var2 = new Rain(); var2.method1(); Example 3: Snow var3 = new Rain(); ((Sleet) var3).method3();
Image of page 12

Subscribe to view the full document.

13 Technique 1: diagram • Diagram the classes from top (superclass) to bottom.
Image of page 13
14 Technique 2: table method Snow Rain Sleet Fog method1
Image of page 14

Subscribe to view the full document.

Image of page 15
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern