plan for today remarks questions
play

Plan For Today Remarks & Questions a few remarks & - PowerPoint PPT Presentation

Plan For Today Remarks & Questions a few remarks & questions no strike review dynamic binding/polymorphism reminders: practice UML as time permits Assignment 1 due next Friday readings for


  1. Plan For Today Remarks & Questions • a few remarks & questions • no strike ☺ • review dynamic binding/polymorphism • reminders: • practice UML as time permits • Assignment 1 due next Friday • readings for OOP/UML Wednesday: debugging • may read ahead about GUIs (courseware) Friday: start GUIs • Spirit Rover & Java • JBuilder question & folders • UML questions: • class variables in object diagrams • concurrency in sequence diagrams • interrupts in activity diagrams CISC 323, extra slides for 19 Jan 2004 1 CISC 323, extra slides for 19 Jan 2004 2 Polymorphism Example: An Array of Employees Encapsulation, Inheritance, Polymorphism Employee people[] = .....; Webster's dictionary definition of polymorphism: // pay everybody for a 10-hour day. The capability of assuming different forms; the capability of for (int i = 0; i < people.length; i++) { widely varying in form. people[i].pay(10); which pay method??? In object-oriented programming, polymorphism refers to the capability of having objects whose specific class not known each people[i] may be plain Employee until run time. or Salesperson or Executive or Unionized CISC 323, extra slides for 19 Jan 2004 3 CISC 323, extra slides for 19 Jan 2004 4

  2. Example (2) Example (3) Employee people[] = .....; Employee people[] = .....; // pay everybody for a 10-hour day. // pay everybody for a 10-hour day. for (int i = 0; i < people.length; i++) { for (int i = 0; i < people.length; i++) { people[i].pay(10); people[i].pay(10); Solution: make the decision at run time based on actual type of At compile time: compiler doesn't know exact class of people[i] people[i]. Doesn't know which pay method to call Called dynamic binding . Compiler can't bind that call to an exact method CISC 323, extra slides for 19 Jan 2004 5 CISC 323, extra slides for 19 Jan 2004 6 How DoesThis Work? When Is Dynamic Binding Important? Inside each Java object is some identifying information – tells Whenever a child class has overridden a method of the parent, exact type of the object and the class of each object in the program is not obvious at compile time. john = new Employee("John", "programmer", 20); george = new Unionized("George", "electrician", 10); Dynamic binding involves overhead: space for extra information inside objects name: "John" name: "George" title: "programmer" title: "electrician" run time to decide which method to call wage: 20 wage: 10 payOwed: 0 payOwed: 0 Java class: Employee Java class: Unionized the Java compiler translates people[i].pay(10) into: look at the type of people[i] and call the appropriate pay method CISC 323, extra slides for 19 Jan 2004 7 CISC 323, extra slides for 19 Jan 2004 8

  3. Note to C++ Programmers Exercise 1 In C++ and some other OO languages: class Cat { class Lion extends Cat { int data; public Lion(int d) { must specify which classes and methods need public Cat(int d) { super(d); data = d; } // end constructor dynamic binding ("virtual") } // end constructor public void identify() { public void identify() { System.out.println("lion " Big headache, causes bugs and confusion System.out.println("cat " + data); + data); } // end identify But saves overhead. } // end identify } // end class Lion } // end class Cat Java: All classes and methods use dynamic binding. // in another class: Cat pippi = new Cat(5); Exception: final classes and methods pippi.identify(); Lion simba = new Lion(8); simba.identify(); Cat elsa = new Lion(15); elsa.identify(); CISC 323, extra slides for 19 Jan 2004 9 CISC 323, extra slides for 19 Jan 2004 10 Exercise 2 Moral of Exercise 2 Dynamic binding happens for the implicit parameter only public class Exercise2 { static void show(Cat c) { (the object before the dot) System.out.println("cat with data = " + c.data); } // end show static void show(Lion ln) { someCat.identify() – dynamic binding (run time) System.out.println("lion with data = " + ln.data); } // end show show(someCat) – compile-time binding class Cat { public static void main(String[] s) { int data; Cat pippi = new Cat(5); public Cat(int d) { show(pippi); data = d; } // end constructor Lion simba = new Lion(8); } // end class Cat show(simba); Cat elsa = new Lion(15); class Lion extends Cat { show(elsa); public Lion(int d) { super(d); } // end main } // end constructor } // end class Lion } // end Exercise2 CISC 323, extra slides for 19 Jan 2004 11 CISC 323, extra slides for 19 Jan 2004 12

  4. Exercise 3 Exercise 3, modified class Animal { class Cat extends Animal { class Animal { class Cat extends Animal { protected int data; public Cat(int x) { protected int data; public Cat(int x) { super(x); super(x); public Animal(int x) { } public Animal(int x) { } data=x; public int amethod(Cat c) { data=x; public int amethod(Animal a) { } return c.data-1; } return a.data+1; public int amethod(Animal a) { } public int amethod(Cat c) { } return a.data+1; } return c.data-1; } } } } } public class Exercise4 { public class Exercise3 { public static void main(String[] s) { public static void main(String[] s) { Cat garfield=new Cat(10); Animal snoopy=new Animal(1); System.out.println(garfield.amethod(garfield) ); Cat garfield=new Cat(10); } System.out.println(garfield.amethod(garfield) ); } System.out.println(snoopy.amethod(snoopy) ); System.out.println(snoopy.amethod(garfield) ); Exercise4.java:15: reference to amethod is ambiguous, both method amethod(Cat) in System.out.println(garfield.amethod(snoopy) ); Animal and method amethod(Animal) in Cat match } } CISC 323, extra slides for 19 Jan 2004 13 CISC 323, extra slides for 19 Jan 2004 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend