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

plan for today remarks questions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CISC 323, extra slides for 19 Jan 2004 1

  • a few remarks & questions
  • review dynamic binding/polymorphism
  • practice UML as time permits

Wednesday: debugging Friday: start GUIs

Plan For Today

CISC 323, extra slides for 19 Jan 2004 2

  • no strike ☺
  • reminders:
  • Assignment 1 due next Friday
  • readings for OOP/UML
  • may read ahead about GUIs (courseware)
  • Spirit Rover & Java
  • JBuilder question & folders
  • UML questions:
  • class variables in object diagrams
  • concurrency in sequence diagrams
  • interrupts in activity diagrams

Remarks & Questions

CISC 323, extra slides for 19 Jan 2004 3

Encapsulation, Inheritance, Polymorphism Webster's dictionary definition of polymorphism: The capability of assuming different forms; the capability of widely varying in form.

Polymorphism

In object-oriented programming, polymorphism refers to the capability of having objects whose specific class not known until run time.

CISC 323, extra slides for 19 Jan 2004 4

Example: An Array of Employees

Employee people[] = .....; // pay everybody for a 10-hour day. for (int i = 0; i < people.length; i++) { people[i].pay(10); each people[i] may be plain Employee

  • r Salesperson or Executive or Unionized

which pay method???

slide-2
SLIDE 2

CISC 323, extra slides for 19 Jan 2004 5

Example (2)

Employee people[] = .....; // pay everybody for a 10-hour day. for (int i = 0; i < people.length; i++) { people[i].pay(10); At compile time: compiler doesn't know exact class of people[i] Doesn't know which pay method to call Compiler can't bind that call to an exact method

CISC 323, extra slides for 19 Jan 2004 6

Example (3)

Employee people[] = .....; // pay everybody for a 10-hour day. for (int i = 0; i < people.length; i++) { people[i].pay(10); Solution: make the decision at run time based on actual type of people[i]. Called dynamic binding.

CISC 323, extra slides for 19 Jan 2004 7

Inside each Java object is some identifying information – tells exact type of the object

How DoesThis Work?

john = new Employee("John", "programmer", 20); george = new Unionized("George", "electrician", 10);

name: "John" title: "programmer" wage: 20 payOwed: 0 Java class: Employee name: "George" title: "electrician" wage: 10 payOwed: 0 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 8

Whenever a child class has overridden a method of the parent, and the class of each object in the program is not obvious at compile time.

When Is Dynamic Binding Important?

Dynamic binding involves overhead: space for extra information inside objects run time to decide which method to call

slide-3
SLIDE 3

CISC 323, extra slides for 19 Jan 2004 9

Note to C++ Programmers

In C++ and some other OO languages: must specify which classes and methods need dynamic binding ("virtual") Big headache, causes bugs and confusion But saves overhead. Java: All classes and methods use dynamic binding. Exception: final classes and methods

CISC 323, extra slides for 19 Jan 2004 10

class Cat { int data; public Cat(int d) { data = d; } // end constructor public void identify() { System.out.println("cat " + data); } // end identify } // end class Cat

Exercise 1

// in another class: Cat pippi = new Cat(5); pippi.identify(); class Lion extends Cat { public Lion(int d) { super(d); } // end constructor public void identify() { System.out.println("lion " + data); } // end identify } // end class Lion Lion simba = new Lion(8); simba.identify(); Cat elsa = new Lion(15); elsa.identify();

CISC 323, extra slides for 19 Jan 2004 11

class Cat { int data; public Cat(int d) { data = d; } // end constructor } // end class Cat

Exercise 2

public class Exercise2 { static void show(Cat c) { System.out.println("cat with data = " + c.data); } // end show static void show(Lion ln) { System.out.println("lion with data = " + ln.data); } // end show } // end Exercise2 class Lion extends Cat { public Lion(int d) { super(d); } // end constructor } // end class Lion public static void main(String[] s) { Cat pippi = new Cat(5); show(pippi); } // end main Cat elsa = new Lion(15); show(elsa); Lion simba = new Lion(8); show(simba);

CISC 323, extra slides for 19 Jan 2004 12

Moral of Exercise 2

Dynamic binding happens for the implicit parameter only (the object before the dot) someCat.identify() – dynamic binding (run time) show(someCat) – compile-time binding

slide-4
SLIDE 4

CISC 323, extra slides for 19 Jan 2004 13

class Animal { protected int data; public Animal(int x) { data=x; } public int amethod(Animal a) { return a.data+1; } }

Exercise 3

public class Exercise3 { public static void main(String[] s) { Animal snoopy=new Animal(1); Cat garfield=new Cat(10); System.out.println(garfield.amethod(garfield) ); System.out.println(snoopy.amethod(snoopy) ); System.out.println(snoopy.amethod(garfield) ); System.out.println(garfield.amethod(snoopy) ); } } class Cat extends Animal { public Cat(int x) { super(x); } public int amethod(Cat c) { return c.data-1; } }

CISC 323, extra slides for 19 Jan 2004 14

class Animal { protected int data; public Animal(int x) { data=x; } public int amethod(Cat c) { return c.data-1; } }

Exercise 3, modified

public class Exercise4 { public static void main(String[] s) { Cat garfield=new Cat(10); System.out.println(garfield.amethod(garfield) ); } } class Cat extends Animal { public Cat(int x) { super(x); } public int amethod(Animal a) { return a.data+1; } }

Exercise4.java:15: reference to amethod is ambiguous, both method amethod(Cat) in Animal and method amethod(Animal) in Cat match