Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.

Similar presentations


Presentation on theme: "Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer."— Presentation transcript:

1 Inheritance & Dynamic Binding

2 Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer - String: mfr -Int: year -Double: price + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} USBFlashDrive - String: loc + save() {…} class Computer { private string mfr; private int year; … public void set_mfr(…) {…} public string get_mfr() {…} public void editAfile(){… ; ;} } private USBFlashDrive usb; usb.save() public void set_usb(USBFlashDrive u){…} public USBFlashDrive get_usb(){…}

3 Now You can use the save() service You need to buy a computer and a usb flash drive Computer mylaptop = new Computer(“Dell”, 2010,900); USBFlashDrive myusb = new USBFlashDrive(…); mylaptop.editAfile(); Don’t forget to connect myusb to mylaptop!!! mylaptop.setusb(myusb);

4 How Can iPhone Be Used? Now, iPhone can be easily used as a USB Flash Drive

5 How Can Software Be Implemented Like this? We have two Classes: Computer - String: mfr -Int: year -Double: price + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} USBFlashDrive - String: loc + save() {…} How Can iPhone be used WITHOUT CHANGE Computer’s Class??? IPhone + save() {…}

6 Inheritance Inheritance: is-a relationship. USBFlashDrive - String: loc + save() {…} IPhone + save() {…} Parent class Child class/subclaass Is-a relationship means: All subclass inherits all attributes and methods of parent class Subclass can override method of parent class. Subclass can have additional method that parent class doesn’t have + call(…){…}

7 How Can Software Be Implemented Like this? We have two Classes: class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } class USBFlashDrive{ … public void save() { // usb’s save here ……… } class IPhone extends USBFlashDrive { public void save() {// iphone’s save here ….. }

8 How Can Software Be Implemented Like this? In Computer’s Eye, iPhone is the same as usb flash drive when saving a file externally. Inheritance Relationship is also called “is-a” relationship!!! Iphone is a USBFlashDrive !!!

9 How Can Software Be Implemented Like this? Class Computer doesn’t distinguish between Iphone and USBFlashDrive. class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } Placehold that can reference to anything that is a USBFlashDrive, like iphone. But in runtime, which save() method is Called depends on the object referenced By usb.

10 So, Static Types vs Dynamic Types Every reference type variables have two types: static type and dynamic type. class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } Variable usb has two types: static type: USBFlashDrive dynamic type: USBFlashDrive or its subtypes (known at runtime)

11 Static Types Static types are used by compiler: class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; } } usb.save(); usb.call(); Compiler Complains: USBFlashDrive doesn’t have the call method!!! X

12 Dynamic Types Once compiler doesn’t complain, you can run the program. Dynamic Type decides which method is called class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } Which save() method is called depends On the dynamic type of usb

13 How it Works To run a program/get a service of save method, you need to buy a computer and iphone objects!!! Computer mylaptop = new Computer(“Dell”, 2010,900); IPhone iphone = new IPhone(…); mylaptop.set_usb(iphone); mylaptop.editAfile(…); class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } What does compiler do? How the runtime does?

14 More … To run a program/get a service of save method, you need to buy a computer and iphone objects!!! Computer mylaptop = new Computer(“Dell”, 2010,900); USBFlashDrive iphone = new IPhone(…); mylaptop.set_usb(iphone); mylaptop.editAfile(…); class Computer { private USBFlashDrive usb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } What does compiler do? How the runtime does?

15 How about this? Consider the following situation Computer mylaptop = new Computer(“Dell”, 2010,900); mylaptop.set_usb(iphone); mylaptop.editAfile(…); IPhone iphone = new USBFlashDrive(…); What does compiler say? NO !!! USBFlashDrive - String: loc + save() {…} IPhone + save() {…}


Download ppt "Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer."

Similar presentations


Ads by Google