Introduction to Computing Using Java Advanced OO Polymorphism

  • Slides: 22
Download presentation
Introduction to Computing Using Java Advanced OO: Polymorphism Abstraction through Interface Miscellaneous Topics about

Introduction to Computing Using Java Advanced OO: Polymorphism Abstraction through Interface Miscellaneous Topics about OO in Java 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 1

Subclass Object Reference ¬Given the following subclass relationship: class Octopus. Watch extends Octopus (class)

Subclass Object Reference ¬Given the following subclass relationship: class Octopus. Watch extends Octopus (class) Octopus. Watch (class) 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 2

Subclass Object Reference ¬Do you recall: short s = 999; Octopus. Watch my. Watch

Subclass Object Reference ¬Do you recall: short s = 999; Octopus. Watch my. Watch = new Octopus. Watch(“Michael Fung”); Octopus (class) my. Watch Octopus. Watch (object) 2005 -2009 10 c Octopus. Watch (class) Michael Fung, CS&E, The Chinese University of HK 3

Subclass Object Reference ¬ Super-class object reference to sub-class object: short s = 999;

Subclass Object Reference ¬ Super-class object reference to sub-class object: short s = 999; int i = s; // copy value of s to i Octopus. Watch my. Watch = new Octopus. Watch(“Michael Fung”); Octopus an. Octopus = my. Watch; // copy object reference my. Watch an. Octopus. Watch (object) 2005 -2009 10 c Octopus (class) Octopus. Watch (class) Michael Fung, CS&E, The Chinese University of HK 4

General VS Special ¬ A super-class type is considered to be general. ¬ A

General VS Special ¬ A super-class type is considered to be general. ¬ A sub-class type is said to be special. ¬ Why? ¬ Because we supplement the sub-class – Adding fields and methods ¬ Thus, an object of the sub-class type is also considered to be an object of the super-class type. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 5

Limit Yourself ¬ Super-class object reference to sub-class object: Octopus. Watch my. Watch =

Limit Yourself ¬ Super-class object reference to sub-class object: Octopus. Watch my. Watch = new Octopus. Watch(“Michael Fung”); my. Watch. show. Time(); // ok Octopus an. Octopus = my. Watch; an. Octopus. add. Value(100); an. Octopus. show. Time(); // copy object reference // ok // invalid operation my. Watch. use. Value(6. 5); // ok ¬ Object reference of the super-class type cannot refer to the fields/methods newly defined by the sub-class. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 6

Michael’s Nature Example Inheritance is also known as specialization! 2005 -2009 10 c Michael

Michael’s Nature Example Inheritance is also known as specialization! 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 7

What For? ¬Dynamic binding or known as Polymorphism. ¬By overriding a method declared in

What For? ¬Dynamic binding or known as Polymorphism. ¬By overriding a method declared in the super-class, we may send the same message to different objects of different (class) type. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 8

Polymorphism ¬ Super-class object reference to sub-class object: Octopus. Watch my. Watch = new

Polymorphism ¬ Super-class object reference to sub-class object: Octopus. Watch my. Watch = new Octopus. Watch(“Michael Fung”); Octopus my. Card = new Octopus(“Microphone”); Octopus an. Octopus; an. Octopus = my. Watch; an. Octopus. use. Value(10. 2); an. Octopus = my. Card; an. Octopus. use. Value(6. 5); // // // an object reference copy object reference ok ¬ The object an. Octopus via which in fact is sometimes sending messages to an Octopus object while sometimes to an Octopus. Watch object. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 9

Better Design Methodology ¬This supports incremental development. ¬We may create new classes from existing

Better Design Methodology ¬This supports incremental development. ¬We may create new classes from existing ones and override their methods. ¬In such case, messages sending to these “new” objects may behave differently. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 10

Backward Type Casting ¬ Backward type casting needs an explicit target type: Octopus. Watch

Backward Type Casting ¬ Backward type casting needs an explicit target type: Octopus. Watch my. Watch = new Octopus. Watch(“Michael Fung”); Octopus an. Octopus; an. Octopus = my. Watch; an. Octopus. use. Value(10. 2); // an object reference // copy object reference // ok Octopus. Watch an. Watch; a. Watch = (Octopus. Watch) an. Octopus; a. Watch. show. Time(); // specializing // ok then ¬ Beware of the type given!!! – It must be a correct/compatible one! 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 11

3 -Minute Soft Break 2005 -2009 10 c Michael Fung, CS&E, The Chinese University

3 -Minute Soft Break 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 12

Abstraction Through Interface interface Interface. Identifier { type 1 method 1(parameter 1); type 2

Abstraction Through Interface interface Interface. Identifier { type 1 method 1(parameter 1); type 2 method 2(parameter 2); . . . } class Class. Identifier implements Interface 1, Interface 2 { type 1 method 1(parameter 1) { } type 2 method 2(parameter 2) { }. . . } 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 13

Interface ¬An interface is a specification. ¬It is a standard. ¬It is a list

Interface ¬An interface is a specification. ¬It is a standard. ¬It is a list of method signatures. ¬It is a guarantee: any class that implements this interface is guaranteed to provide these methods. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 14

Example ¬ The AWT uses interfaces extensively. interface Mouse. Motion. Listener { // simplified

Example ¬ The AWT uses interfaces extensively. interface Mouse. Motion. Listener { // simplified version public void mouse. Dragged(Mouse. Event e); public void mouse. Moved(Mouse. Event e); } // in some Component class: void add. Mouse. Motion. Listener(Mouse. Motion. Listener obj); ¬ The method is not requesting a class type or primitive type parameter. ¬ It is requesting a parameter of ANY class type that meets the Mouse. Motion. Listener standard. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 15

Self and Super Reference ¬ The keyword this is used whenever an object is

Self and Super Reference ¬ The keyword this is used whenever an object is referring to its own fields/methods, in case of name-conflict. this. value = value; this. value = given. Octopus. Card. value; ¬ The keyword super is used whenever an object is referring to fields/methods of its super-class, in case of overriding. super(); super. eat(); 2005 -2009 10 c // super-construction // both super-class and // sub-class define eat() Michael Fung, CS&E, The Chinese University of HK 16

How to Copy Objects? ¬ We haven’t talked about how to copy an object!

How to Copy Objects? ¬ We haven’t talked about how to copy an object! ¬ Simple intuitive way: create a new object and copy the value of the fields from the original object to the new object. – Pitfall: some of the fields may be objects again! – Impossible: some of the fields may be private! ¬ Cleaner way: define and use the clone() method. – Wait and look. . . 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 17

How Impossible is the Mission? Octopus (class) Octopus (object) value [private] 45. 00 value

How Impossible is the Mission? Octopus (class) Octopus (object) value [private] 45. 00 value [private] 0. 00 my. Card copy. Card 2005 -2009 10 c class Octopus { private double value; public void add. Value(double money) { value += money; } } class Processor { public void some. Processor. Method() { Octopus my. Card = new Octopus(); my. Card. add. Value(45. 00); Octopus copy. Card = new Octopus(); copy. Card. value = my. Card. value; // the above statement is invalid! } } Michael Fung, CS&E, The Chinese University of HK 18

The clone() Method ¬ To copy an object, we need clone(): class Octopus {

The clone() Method ¬ To copy an object, we need clone(): class Octopus { double value; String name; public Octopus(double value, String given. Name) { this. value = value; // this usage name = given. Name; } public Object clone() { Octopus new. Card = new Octopus(0, name); new. Card. value = this. value; // this usage return new. Card; } public static void main(String [] args) { Octopus michael = new Octopus(100, "Michael Fung"); Octopus copy; copy = (Octopus) michael. clone(); } 2005 -2009 Michael Fung, CS&E, The Chinese University of HK } 10 c 19

Field-by-Field Copying ¬To copy an object itself rather than the object reference. ¬Recall: Octopus

Field-by-Field Copying ¬To copy an object itself rather than the object reference. ¬Recall: Octopus my. Card = new Octopus(“Michael Fung”); Octopus an. Octopus; // an object reference an. Octopus = my. Card; // copy object reference ¬We define a method to – Create a new object and; – Copy the “meat” of the object. 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 20

Variable VS Object ¬ Assignment operators = VS clone() ¬ Comparison operators == VS

Variable VS Object ¬ Assignment operators = VS clone() ¬ Comparison operators == VS equals() public boolean equals(Octopus target) { return this. value == target. value; } System. out. println(“They have equal value: ” + michael. equals(copy)); 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 21

End Note ¬Readings and References – Sections 6. 5, 9. 1, 9. 2, 9.

End Note ¬Readings and References – Sections 6. 5, 9. 1, 9. 2, 9. 3, 9. 4, 9. 5, 9. 6 2005 -2009 10 c Michael Fung, CS&E, The Chinese University of HK 22