Topic 32 - Polymorphism.

Slides:



Advertisements
Similar presentations
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
1 Inheritance Readings: Writing classes Write an Employee class with methods that return values for the following properties of employees at a.
Copyright 2008 by Pearson Education Building Java Programs Subtype Polymorphism; Sorting (an extended programming example)
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Critters; Subtype Polymorphism Reading: HW9 Handout, Chapter 9.2.
Building Java Programs Chapter 9
Unit 7 Textbook Chapter 9.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
AD Lecture #2 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism Abstract.
Inheritance - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 142 Computer Science II Zhen Jiang West Chester University
CS 112 Introduction to Programming Inheritance Hierarchy; Polymorphism Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-1: Inheritance reading:
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
Some Object-Oriented Programming (OOP) Review. Let’s practice writing some classes Write an Employee class with methods that return values for the following.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-3: Polymorphism reading: 9.3.
1 Interacting with the superclass (continued) suggested reading:9.4.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 9 Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs
Lecture 16: Polymorphism (Part I)
Building Java Programs Chapter 9
Chapter 11 Inheritance and Polymorphism
Building Java Programs Chapter 9
Inheritance - CIS 1068 Program Design and Abstraction
public class Doubler extends Base {
Building Java Programs
Building Java Programs Chapter 9
Lecture 17: Polymorphism (Part II)
© University of Washington, all rights reserved.
Building Java Programs
Topic 32 - Polymorphism.
Week 4 – OOP II EE422C - Software Design and Implementation II • Fall 2017 (Slides modified from originals by Prof. Mike Scott)
CSE 143 Lecture 22 The Object class; Polymorphism read
Polymorphism 28-Nov-18.
Building Java Programs
Building Java Programs
CSE 143 Lecture 22 The Object class; Polymorphism read
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Building Java Programs
Building Java Programs
Law firm employee analogy
Inheritance Readings: 9.1.
Lecture 17: Polymorphism (Part I)
Lecture 18: Polymorphism (Part II)
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Polymorphism 15-Apr-19.
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Polymorphism 21-Apr-19.
Building Java Programs
CSE 143 Lecture 23 Polymorphism; the Object class read
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 9 9-3: Polymorphism reading: 9.3
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Building Java Programs
Presentation transcript:

Topic 32 - Polymorphism

Polymorphism polymorphism: Ability for the same code to be used with different types of objects and behave differently with each. System.out.println can print any type of object. Each one displays in its own way on the console. CritterMain can interact with any type of critter. Each one moves, fights, etc. in its own way.

Coding with polymorphism A variable of type T can refer to an object of any subclass of T. Employee ed = new Lawyer(); You can call any methods from the Employee class on ed. When a method is called on ed, it behaves as a Lawyer. System.out.println(ed.getSalary()); // 50000.0 System.out.println(ed.getVacationForm()); // pink

Polymorphism and parameters You can pass any subtype of a parameter's type. public class EmployeeMain { public static void main(String[] args) { Lawyer lisa = new Lawyer(); Secretary steve = new Secretary(); printInfo(lisa); printInfo(steve); } public static void printInfo(Employee empl) { System.out.println("salary: " + empl.getSalary()); System.out.println("v.days: " + empl.getVacationDays()); System.out.println("v.form: " + empl.getVacationForm()); System.out.println(); OUTPUT: salary: 50000.0 salary: 50000.0 v.days: 15 v.days: 10 v.form: pink v.form: yellow

Polymorphism and arrays Arrays of superclass types can store any subtype as elements. public class EmployeeMain2 { public static void main(String[] args) { Employee[] e = { new Lawyer(), new Secretary(), new Marketer(), new LegalSecretary() }; for (int i = 0; i < e.length; i++) { System.out.println("salary: " + e[i].getSalary()); System.out.println("v.days: " + e[i].getVacationDays()); System.out.println(); } Output: salary: 50000.0 v.days: 15 v.days: 10 salary: 60000.0 salary: 55000.0

A polymorphism problem Suppose that the following four classes have been declared: public class Foo { public void method1() { System.out.println("foo 1"); } public void method2() { System.out.println("foo 2"); public String toString() { return "foo"; public class Bar extends Foo { System.out.println("bar 2");

A polymorphism problem public class Baz extends Foo { public void method1() { System.out.println("baz 1"); } public String toString() { return "baz"; public class Mumble extends Baz { public void method2() { System.out.println("mumble 2"); What would be the output of the following client code? Foo[] pity = {new Baz(), new Bar(), new Mumble(), new Foo()}; for (int i = 0; i < pity.length; i++) { System.out.println(pity[i]); pity[i].method1(); pity[i].method2(); System.out.println();

Diagramming the classes Add classes from top (superclass) to bottom (subclass). Include all inherited methods.

Finding output with tables method Foo Bar Baz Mumble method1 foo 1 baz 1 method2 foo 2 bar 2 mumble 2 toString foo baz method Foo Bar Baz Mumble method1 foo 1 baz 1 method2 foo 2 bar 2 mumble 2 toString foo baz method Foo Bar Baz Mumble method1 method2 toString It's annoying to flip back and forth between slides here. I suggest putting the classes into a text editor so you can switch windows back and forth to fill in the table.

Polymorphism answer Output: Foo[] pity = {new Baz(), new Bar(), new Mumble(), new Foo()}; for (int i = 0; i < pity.length; i++) { System.out.println(pity[i]); pity[i].method1(); pity[i].method2(); System.out.println(); } Output: baz baz 1 foo 2 foo foo 1 bar 2 mumble 2

Another problem The order of the classes is jumbled up. The methods sometimes call other methods (tricky!). public class Lamb extends Ham { public void b() { System.out.print("Lamb b "); } public class Ham { public void a() { System.out.print("Ham a "); b(); System.out.print("Ham b "); public String toString() { return "Ham";

Another problem 2 public class Spam extends Yam { public void b() { System.out.print("Spam b "); } public class Yam extends Lamb { public void a() { System.out.print("Yam a "); super.a(); public String toString() { return "Yam"; What would be the output of the following client code? Ham[] food = {new Lamb(), new Ham(), new Spam(), new Yam()}; for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); System.out.println(); // to end the line of output food[i].b(); System.out.println();

Class diagram

Polymorphism at work Lamb inherits Ham's a. a calls b. But Lamb overrides b... public class Ham { public void a() { System.out.print("Ham a "); b(); } public void b() { System.out.print("Ham b "); public String toString() { return "Ham"; public class Lamb extends Ham { System.out.print("Lamb b "); Lamb's output from a: Ham a Lamb b

The table method Ham Lamb Yam Spam a Ham a b() Yam a b Ham b Lamb b Spam b toString method Ham Lamb Yam Spam a b toString method Ham Lamb Yam Spam a Ham a b() Yam a b Ham b Lamb b Spam b toString

The answer Ham[] food = {new Lamb(), new Ham(), new Spam(), new Yam()}; for (int i = 0; i < food.length; i++) { System.out.println(food[i]); food[i].a(); food[i].b(); System.out.println(); } Output: Ham Ham a Lamb b Lamb b Ham a Ham b Ham b Yam Yam a Ham a Spam b Spam b Yam a Ham a Lamb b

Overriding Object's equals Method The Object class contains this method: public boolean equals(Object obj) many classes override this method many students mistakenly overload the method many headaches when placing objects in data structures

Overriding Object's equals Method overriding equals correctly follows a pattern So, it isn't that hard, if you follow the pattern Override equals for a Standard Playing Card