CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 9: Polymorphism Coming up: Binding.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L 9.1 – 9.3.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
1 Polymorphism  A reference can be polymorphic, which can be defined as "having many forms" obj.doIt();  This line of code might execute different methods.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Chapter 9 Polymorphism. © 2004 Pearson Addison-Wesley. All rights reserved Polymorphism Polymorphism is an object-oriented concept that allows.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 9 Polymorphism 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Chapter 9 Polymorphism.
03/10/14 Inheritance-2.
Polymorphism.
Designing for Inheritance
Overriding Methods & Class Hierarchies
Chapter 9 Polymorphism.
Chapter 8 Inheritance Part 2.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost

2 Lecture Outline Lab 5 questions Review: Inheritance Polymorphism Mid-semester review

3 Inheritance Inheritance allows us to create more specific types from existing classes This promotes:  Code Reuse  Maintainability We can change the behavior that we inherit by:  Overriding inherited methods  Adding behavior via new members Fields (Instance Variables) Methods

4 Inheritance Design Issues (When should we use inheritance?) Every derivation should be an is-a relationship  Child classes should define more specific behavior Think about the potential future of a class hierarchy, and design classes to be reusable and flexible Find common characteristics of classes and push them as high in the class hierarchy as appropriate Override methods as needed to tailor or change the functionality of a subclass Add new variables to children, but don't redefine (shadow) inherited variables, if possible

5 Inheritance Design Issues Allow each class to manage its own data; use the super reference to invoke the parent's constructor to set up its data Even if there are no current uses for them, override general methods such as toString and equals with appropriate definitions Use abstract classes to represent general concepts that lower classes have in common Use visibility modifiers carefully to provide needed access without violating encapsulation

6 Polymorphism Polymorphism is an object-oriented concept that allows us to create versatile software designs We will look at on  polymorphism and its benefits  using inheritance to create polymorphic references  using interfaces to create polymorphic references

7 Polymorphism The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects at different points in time The method invoked through a polymorphic reference can change from one invocation to the next All object references in Java are potentially polymorphic

8 Binding Consider the following method invocation: obj.doSomething(); At some point, this invocation is bound to the definition of the method that it invokes If this binding occurred at compile time, then that line of code would call the same method every time However, Java defers method binding until run time What we lose in predictability, we gain in program design flexibility

9 Polymorphism Suppose we create the following reference variable: Animal animal; Java allows this reference to point to an Animal object, or to any object of any compatible type This compatibility can be established using inheritance or using interfaces Careful use of polymorphic references can lead to elegant, robust software designs

10 Polymorphism Consider an abstract class (or interface) Lender, meant to model a credit facility generally: public abstract class Lender { public abstract double borrow (double amount); public abstract double repay (double amount); } public class CreditCard extends Lender { //... } public class LineOfCredit extends Lender { //... }

11 Polymorphism A variable with an abstract class for a type holds reference to object of a class that extends Lender and implements its abstract methods. Note that the object to which x refers doesn't have type Lender ; the type of the object is some subclass of Lender. Lender x; x = new CreditCard(); x = new LineOfCredit();

12 Polymorphism You can call any of the methods defined on the variable type: Which method is called? The borrow method defined by the LineOfCredit class is called, because (per the previous slide) it is the current type stored inside variable x. double remainingFunds = x.borrow(1000);

13 Polymorphism Which method is invoked depends on the actual object referenced. If x refers to a LineOfCredit object, Java Invokes LineOfCredit ’s borrow method If x refers to a CreditCard object, Java invokes CreditCard ’s borrow method Behavior can vary depending on the actual type of an object stored in x  However, notice that we are able to deal with x uniformly as a Lender regardless of what is actually stored inside

14 Polymorphism Java doesn’t know at compile time what type of object is stored within a polymorphic variable  So how does it know what version of a method to call? It uses late binding: the method call is resolved at runtime  a.k.a. dynamic binding Different from overloading; overloading is resolved by the compiler (early binding)

15 Polymorphism via Interfaces An interface name can be used as the type of an object reference variable Speaker current; The current reference can be used to point to any object of any class that implements the Speaker interface The version of speak that the following line invokes depends on the type of object that current is referencing current.speak();

16 Polymorphism via Interfaces Suppose two classes, Professor and Dog, both implement the Speaker interface, providing distinct versions of the speak method In the following code, the first call to speak invokes one version and the second invokes another: Speaker guest = new Professor(); guest.speak(); guest = new Dog(); guest.speak(); Blah blah blah. I’m really boring. Blah blah blah. Ruff. (Walk? Did you say Walk? Treat? I thought I heard “treat” somewhere in there.)

17 References and Inheritance An object reference can refer to an object of its class, or to an object of any class related to it by inheritance For example if we have a SavingsAccount object, then a variable of type BankAccount could be used, if the SavingsAccount class is derived from BankAccount BankAccount acct;... acct = new SavingsAccount(); BankAccount SavingsAccount

18 Inheritance and Assignment Assigning a child object to a parent reference is considered to be a widening conversion, and can be performed by simple assignment Assigning an parent object to a child reference can be done also, but it is considered a narrowing conversion and must be done with a cast The widening conversion is the most useful SavingsAccount savings = new SavingsAccount(); BankAccount account = savings; BankAccount account = new SavingsAccount(); SavingsAccount savings = (SavingsAccount)account; Trust me Java, I know this is a SavingsAccount

19 Polymorphism As we have seen, in Java, type of a variable doesn't definitively determine type of object to which it refers Method calls are determined by type of actual object, not type of object reference BankAccount aBankAccount = new SavingsAccount(1000); // aBankAccount holds a reference to a SavingsAccount BankAccount anAccount = new CheckingAccount(); anAccount.deposit(1000); // Calls "deposit" from CheckingAccount

20 Polymorphism Compiler needs to check that only legal methods are invoked Why is this not allowed? Object anObject = new BankAccount(); //... anObject.deposit(1000); // Compile-time Error! Object anObject = new BankAccount(); // Acceptable ways of casting: ((BankAccount)anObject).deposit(1000); BankAccount account = (BankAccount)anObject; account.deposit(1000);

21 Polymorphism Polymorphism: ability to refer to objects of multiple types with varying behavior Polymorphism at work: Depending on types of amount and other, different versions of withdraw and deposit are called  We don’t have to worry about the exact types we are working with public void transfer (double amount, BankAccount other) { this.withdraw(amount); other.deposit(amount); }

22 Polymorphism via Inheritance Consider the following class hierarchy: StaffMember ExecutiveHourly VolunteerEmployee

23 Polymorphism via Inheritance Now let's look at an example that pays a set of diverse employees using a polymorphic method [Example on board]

24 Conclusion I will be in Lab now for help with Lab 5 Lab 6 will be assigned on Tuesday  (Enjoy your weekend)