Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9.

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

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
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 27: More on ArrayList, Reference Semantics Command Line Arguments reading:
Building Java Programs Chapter 9
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
Writing Classes (Chapter 4)
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.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
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!
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
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 Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs
Lecture 16: Polymorphism (Part I)
Building Java Programs Chapter 9
Building Java Programs Chapter 9
Building Java Programs
03/10/14 Inheritance-2.
Lecture 17: Polymorphism (Part II)
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
Topic 32 - Polymorphism.
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
Inheritance Readings: 9.1.
Lecture 17: Polymorphism (Part I)
Lecture 18: Polymorphism (Part II)
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
CSE 143 Lecture 23 Polymorphism; the Object class read
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:

Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-2: Polymorphism reading: 9.2 self-check: #5-9

Copyright 2009 by Pearson Education 2 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.

Copyright 2009 by Pearson Education 3 Coding with polymorphism A variable of type T can hold an object of any subclass of T. Character rob = new Robot(); You can call any methods from Character on rob. You can not call any methods specific to Robot (e.g. sayQuote ). When a method is called on sam, it behaves as a Robot. System.out.println(sam.getAttack()); // 20 System.out.println(sam.toString()); // HP: 100

Copyright 2009 by Pearson Education 4 Polymorphism + parameters Methods can accept superclass types as parameters. You can pass any subtype of that superclass. public class TestCharacters { public static void main(String[] args) { KnightWhoSaysNi knight = new KnightWhoSaysNi(); Robot robRobot = new Robot(); basicAbilities(knight); basicAbilities(robRobot); } // prints the result of some of the methods in a Character public static void basicAbilities(Character player) { System.out.println("toString: " + player); System.out.println("getAttack: " + player.getAttack()); System.out.println("isAlive: " + player.isAlive()); System.out.println(); } OUTPUT: toString: HP: 100 getAttack: 10 isAlive: true toString: HP: 100 getAttack: 20 isAlive: true

Copyright 2009 by Pearson Education 5 Polymorphism + arrays Arrays of superclass types can store any subtype as elements. public class TestCharacters2 { public static void main(String[] args) { Character[] c = { new KnightWhoSaysNi(), new Robot(), new Sorcerer(), new Oracle() }; for (int i = 0; i < e.length; i++) { System.out.println("attack: " + c[i].getAttack()); System.out.println("toString: " + c[i].toString()); System.out.println(); }

Copyright 2009 by Pearson Education 6 Polymorphism problems ~4-5 classes with inheritance relationships are shown. A client program calls methods on objects of each class. You must read the code and determine the client's output.

Copyright 2009 by Pearson Education 7 A polymorphism problem Assume 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 { public void method2() { System.out.println("bar 2"); }

Copyright 2009 by Pearson Education 8 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[] f = {new Baz(), new Bar(), new Mumble(), new Foo()}; for (int i = 0; i < f.length; i++) { System.out.println(f[i]); f[i].method1(); f[i].method2(); System.out.println(); }

Copyright 2009 by Pearson Education 9 Diagramming the classes Add classes from top (superclass) to bottom (subclass). Include all inherited methods.

Copyright 2009 by Pearson Education 10 Finding output with tables methodFooBarBazMumble method1 method2 toString methodFooBarBazMumble method1foo 1baz 1 method2foo 2bar 2mumble 2 toStringfoobaz methodFooBarBazMumble method1foo 1 baz 1 method2foo 2bar 2foo 2mumble 2 toStringfoo baz

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

Copyright 2009 by Pearson Education 12 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(); } public void b() { System.out.print("Ham b "); } public String toString() { return "Ham"; }

Copyright 2009 by Pearson Education 13 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(); // to end the line of output System.out.println(); }

Copyright 2009 by Pearson Education 14 Class diagram

Copyright 2009 by Pearson Education 15 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 { public void b() { System.out.print("Lamb b "); } } Lamb 's output from a : Ham a Lamb b

Copyright 2009 by Pearson Education 16 The table methodHamLambYamSpam a b toString

Copyright 2009 by Pearson Education 17 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 Ham a Ham b Ham b Yam Yam a Ham a Spam b Spam b Yam Yam a Ham a Lamb b Lamb b

Copyright 2009 by Pearson Education 18 Inheritance Mystery Summary Draw the inheritance hierarchy Create the chart Start from the top of the hierarchy downward When you see super.something(), copy the contents of the superclasses’s something() method into the chart When you see a (non-super) method call, copy the method call into the chart Not the contents of the method call, since this may change depending on inheritance! Use the chart to write the final output

Copyright 2009 by Pearson Education Building Java Programs Parameters and References Review

Copyright 2009 by Pearson Education 20 Parameters - value semantics Recall: For primitives, parameters are initialized by copying the value For objects, parameters are initialized by copying the reference An “arrow” to the object is copied (not the array or object itself). Consequence: If you change the primitive variable inside the called method, it has no effect on the variable in the scope of the caller If you change the array or object in the called method, it's the same array or object that the caller has, so the caller’s objects change too! More practice in section