160 likes | 309 Views
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 (…) {…}
E N D
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() {…}
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!!!
How Can iPhone Be Used? • Now, iPhone can be easily used as a USB Flash Drive
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???
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
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 ….. } }
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!!!
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.
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)
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
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
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?
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?
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() {…}