Inheritance & Dynamic Binding
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(){..}
class Computer {
private string mfr;
private int year;
private USBFlashDrive usb;
…
public void set_mfr(…) {…}
public string get_mfr() {…}
+ editAfile() {…}
public void set_usb(USBFlashDrive u){…}
public USBFlashDrive get_usb(){…}
public void editAfile(){… ; 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);
USBFlashDrive myusb = new USBFlashDrive(…);
mylaptop.setusb(myusb);
mylaptop.editAfile();
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
- String: mfr
- Int: year
- Double: price
+ set_mfr(…){..}
+ get_mfr(){..}
USBFlashDrive
- String: loc
+ save() {…}
+ editAfile() {…}
IPhone
+ save() {…}
How Can iPhone be used WITHOUT
CHANGE Computer’s Class???
Inheritance
• Inheritance: is-a relationship.
USBFlashDrive
- String: loc
+ save() {…}
IPhone
+ save() {…}
+ call(…){…}
Parent class
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
Child class/subclaass
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
…..
}
}
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 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.
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)
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();
X
}
Compiler Complains:
USBFlashDrive doesn’t have the
call method!!!
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
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);
class Computer {
mylaptop.editAfile(…);
private USBFlashDrive usb;
…
public void set_usb(USBFlashDrive u)
What does compiler do?
{ usb = u;}
public USBFlashDrive get_usb()
{ return usb;}
How the runtime does?
….
public void editAfile(){… ; usb.save();}
}
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);
class Computer {
mylaptop.editAfile(…);
private USBFlashDrive usb;
…
public void set_usb(USBFlashDrive u)
What does compiler do?
{ usb = u;}
public USBFlashDrive get_usb()
{ return usb;}
How the runtime does?
….
public void editAfile(){… ; usb.save();}
}
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
What does compiler say?
- String: loc
+ save() {…}
IPhone
+ save() {…}
© Copyright 2026 Paperzz