23-polymorphism-object

23-polymorphism-object - CSE143 Lecture23...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 23 Polymorphism; the  Object  class read 9.2 - 9.3 slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Polymorphism polymorphism : Ability for the same code to be used with  different types of objects and behave differently with each. System.out.println  can print any type of object. Each one displays in its own way on the console. Scanner  can read data from any kind of  InputStream . Every kind of  OutputStream  can write data, though they might  write this to different kinds of sources.
Background image of page 2
3 Coding with polymorphism A variable of type  T   can refer to an object of any subclass of  T . Employee ed = new Lawyer(); Object otto = new Secretary(); You can call any methods from  Employee  on  ed . You can  not   call any methods specific to  Lawyer  (e.g.  sue ). When a method is called on  ed , it behaves as a  Lawyer . System.out.println( ed.getSalary() ); // 50000.0 System.out.println( ed.getVacationForm() ); // pink
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Polymorphism/parameters You can pass any subtype of a parameter's type. public class EmployeeMain { public static void main(String[] args) { Lawyer lisa = new Lawyer(); Secretary steve = new Secretary(); printInfo(lisa); printInfo(steve); } public static void printInfo( Employee empl ) { System.out.println("salary = " + empl.getSalary()); System.out.println("days = " + empl.getVacationDays()); System.out.println("form = " + empl.getVacationForm()); System.out.println(); } } OUTPUT: salary = 50000.0 salary = 50000.0 vacation days = 21 vacation days = 10 vacation form = pink vacation form = yellow
Background image of page 4
5 Coding with polymorphism We can use polymorphism with classes like  OutputStream . Recall methods common to all  OutputStream s: Recall part of the inheritance hierarchy for  OutputStream : Method Description write (int b) writes a byte close () stops writing (also flushes) flush() forces any writes in buffers to be written OutputStream FileOutputStream PrintStream FilterOutputStream
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Streams and polymorphism A variable of type T can refer to an object of  any subclass  of T. OutputStream out = new PrintStream(new File("foo.txt")); OutputStream out2 = new FileOutputStream("foo.txt"); You can call any methods from  OutputStream  on  out . You can  not   call methods specific to  PrintStream  ( println ). But how  would  we call those methods on  out  if we wanted to? When  out  runs a method, it behaves as a  PrintStream .
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/04/2011 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 31

23-polymorphism-object - CSE143 Lecture23...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online