Understanding Inheritance and Dynamic Binding in Object-Oriented Programming

Slide Note
Embed
Share

Explore the concepts of inheritance and dynamic binding in Object-Oriented Programming. Learn how subclasses inherit attributes and methods from parent classes and how dynamic binding allows for method overriding. Discover how to implement these concepts in classes like USBFlashDrive, Computer, and iPhone without changing existing class structures.


Uploaded on Sep 20, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

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; - String: mfr - Int: year - Double: price + set_mfr( ){..} + get_mfr(){..} private USBFlashDrive usb; public void set_mfr( ) { } public string get_mfr() { } + editAfile() { } public void set_usb(USBFlashDrive u){ } public USBFlashDrive get_usb(){ } usb.save() USBFlashDrive public void editAfile(){ ; ;} } - 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); USBFlashDrive myusb = new USBFlashDrive( ); mylaptop.setusb(myusb); mylaptop.editAfile(); 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 USBFlashDrive usb; public void set_usb(USBFlashDrive u) { usb = u;} public USBFlashDrive get_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 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(){ ; } } 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 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); IPhone iphone = new USBFlashDrive( ); NO !!! mylaptop.set_usb(iphone); mylaptop.editAfile( ); USBFlashDrive - String: loc What does compiler say? + save() { } IPhone + save() { }

Related


More Related Content