More on Polymorphism. Ever have one of those days?

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

More on Classes Inheritance and Polymorphism
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Java Review Interface, Casting, Generics, Iterator.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Polymorphism, Virtual Methods and Abstract Classes.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Abstract Classes.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
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.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Types in programming languages1 What are types, and why do we need them?
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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:
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Introduction to Polymorphism. Recall We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Polymorphism, Virtual Methods and Abstract Classes
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Designing for Inheritance
Lesson Objectives Aims
Polymorphism and access control
Initializing Objects.
CS18000: Problem Solving and Object-Oriented Programming
Introduction to Data Structure
Advanced Inheritance Concepts
Fundaments of Game Design
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CIS 110: Introduction to computer programming
Presentation transcript:

More on Polymorphism

Ever have one of those days?

Review Primitives –Exhibit no polymorphic behavior cast –Are required to be cast if a potential loss of information is possible Objects –All objects are descended from a common ancestor, class Object –When objects are created or instantiated some dynamic memory is allocated to the object. In this memory can be found “zones” representing each ancestor class starting from Object down to the actual type of the object. –A reference of any type in the ancestry of the object can refer to the object

Review Objects (continued) –During the compilation process each method invocation on a reference is checked to make sure that an object of the reference type has that method (includes searching up the inheritance tree). If not casting must be used. –During program execution each method invocation on a reference causes Java to find the lowest occurence of a method in the actual object. This is known as dynamic binding.

Generic Collection Classes We want to be able to make collection classes like: LinkedList, Set, Stack, Queue, etc. We only want to write them once. What are the problems we will face? How can these problems be solved?

Solutions Problem: If we write our class to hold a specific type of object it will have to be rewritten for each different type object we wish to hold Solution: Make collection classes hold Objects. Since all objects are children of Object we can polymorphically hold anything

Solutions Problem: If we need to tell if two objects are the same we can’t compare them using == since this just tells us if the references are referring to the same object. Solution: Include in the class a method with the following signature public boolean equals(Object o) Have this method perform the check in whatever fashion is appropriate for the class This will take on one of two forms

equals form 1 Given a class (class ID) which holds a name and a social security number you decide that objects of this class are equal if the SSN matches. Add this method public boolean equals(Object o) { if(getSsn() == ((ID)o).getSsn()) return true; else return false; } Why the cast?

equals form 2 You have a class LLNode and you want to be able to compare Nodes. The nodes contain an Object reference to the data and a LLNode reference to the next node. The nodes are equal if the data elements are equal. public boolean equals(Object o) { if(getData().equals(((LLNode)o).getData()) return true; else return false; }

Solution Problem: I want my collection to keep objects in order. A simple equals method is not enough. Solution: Take CS There you will learn to use a method that looks like this: public int compareTo(Object o) You will also learn about a lot of additional stuff like interfaces plus you’ll make new friends

Solution Problem: If I make my generic collection class hold Objects I’ll get errors when I remove objects and try to invoke their methods Solution: As you remove the object cast it to what it needs to be to understand the method call This is also a good place to use abstract classes and a well designed class hierarchy

Example You are creating an exhibit for an amusement park. It will feature robotic animals Some of the animals can speak, others cannot. Those that speak do so differently –Lions roar –Birds chirp –Hyenas laugh Basenjis say nothing

Solution Create a hierarchy like this: Animal BasenjiHyenaBirdLion Add a method to Animal called speak In each individual animal class override speak to do the appropriate thing for each animal

Solution Write code like this Queue q = new Queue(); q.enqueue(new Lion()); q.enqueue(new Hyena()); q.enqueue(new Bird()); q.enqueue(new Basenji()); while(! q.isEmpty()) { (Animal)(q.dequeue()).speak(); }