1 / 15

Inheritance & Dynamic Binding

Inheritance & Dynamic Binding. Class USBFlashDrive. We better introduce a new class USMBFlashDrive and save() is defined as a method in that class. Computer. class Computer { private string mfr ; private int year ; … public void set_mfr (…) {…}

barton
Download Presentation

Inheritance & Dynamic Binding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 class Computer { private string mfr; private int year; … public void set_mfr(…) {…} public string get_mfr() {…} public void editAfile(){… ; ;} } • - String: mfr • Int: year • Double: price private USBFlashDriveusb; + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} public void set_usb(USBFlashDrive u){…} public USBFlashDriveget_usb(){…} usb.save() USBFlashDrive - String: loc + save() {…}

  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); USBFlashDrivemyusb = new USBFlashDrive(…); mylaptop.editAfile(); mylaptop.setusb(myusb); Don’t forget to connect myusb to mylaptop!!!

  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 USBFlashDrive • - String: mfr • Int: year • Double: price - String: loc + save() {…} + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} IPhone + save() {…} How Can iPhone be used WITHOUT CHANGE Computer’s Class???

  6. Inheritance • Inheritance: is-a relationship. USBFlashDrive • 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 - String: loc Parent class + save() {…} IPhone + save() {…} + call(…){…} Child class/subclaass

  7. How Can Software Be Implemented Like this? • We have two Classes: class USBFlashDrive{ … public void save() { // usb’s save here ……… } } class Computer { private USBFlashDriveusb; … public void set_usb(USBFlashDriveu) { usb = u;} public USBFlashDriveget_usb() { return usb;} …. public void editAfile(){… ; usb.save();} } 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 USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_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 USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_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 USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_usb() { return usb;} …. public void editAfile(){… ; } } Compiler Complains: USBFlashDrive doesn’t have the call method!!! usb.save(); usb.call(); X

  12. Dynamic Types • Once compiler doesn’t complain, you can run the program. Dynamic Type decides which method is called class Computer { private USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_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 myiphone= new IPhone(…); mylaptop.set_usb(iphone); mylaptop.editAfile(…); class Computer { private USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_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); USBFlashDrivemyiphone= new IPhone(…); mylaptop.set_usb(iphone); mylaptop.editAfile(…); class Computer { private USBFlashDriveusb; … public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDriveget_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 myiphone = new USBFlashDrive(…); NO !!! USBFlashDrive - String: loc What does compiler say? + save() {…} IPhone + save() {…}

More Related