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

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Inheritance Writing and using Classes effectively.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Topic 4 Inheritance.
Programming in Java CSCI-2220 Object Oriented Programming.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance ndex.html ndex.htmland “Java.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
BY:- TOPS Technologies
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lecture 17: Polymorphism (Part II)
Lecture 9-2: Interacting with the Superclass (super);
Programming Language Concepts (CIS 635)
ATS Application Programming: Java Programming
Object Oriented Analysis and Design
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
Method Overriding in Java
Sampath Kumar S Assistant Professor, SECE
Polymorphism.
Lecture 18: Polymorphism (Part II)
Object-Oriented Programming
Inheritance and Polymorphism
Sampath Kumar S Assistant Professor, SECE
C++ Programming CLASS This pointer Static Class Friend Class
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

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(){..} + 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(){…}

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);

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

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(…){…}

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(); Compiler Complains: USBFlashDrive doesn’t have the call method!!! X

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); 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?

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?

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() {…}