Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”

Slides:



Advertisements
Similar presentations
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
More about inheritance Exploring polymorphism 5.0.
1 Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
Inheritance and Polymorphism
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
1 Abstract Classes “I prefer Agassiz in the abstract, rather than in the concrete.” CS Computer Science II.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.
ECE122 Feb. 22, Any question on Vehicle sample code?
Programming in Java CSCI-2220 Object Oriented Programming.
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
More about inheritance Exploring polymorphism 5.0.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
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.
BY:- TOPS Technologies
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Inheritance, Polymorphism, and Interfaces 1 What’s past is prologue. Don’t write it twice — write it once and reuse it. Advanced Placement Computer Science.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 10 Collections Richard Gesick.
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
More about inheritance
More about inheritance
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Inheritance in Java.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
COS 260 DAY 19 Tony Gauvin.
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Topic 6 Generic Data Structures
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Designing for Inheritance
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Recitation 6 Inheritance.
Chapter 9 Inheritance and Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Lecture 19 - Inheritance (Contd).
Java Programming Language
Polymorphism.
Inheritance, Polymorphism, and Interfaces. Oh My
More about inheritance
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
Topic 10 Abstract Classes “I prefer Agassiz in the abstract,
ArrayLists 22-Feb-19.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Review for Midterm 3.
Polymorphism.
Advanced Programming in Java
CS 240 – Advanced Programming Concepts
Presentation transcript:

Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”

Polymorphism Another feature of OOP literally “having many forms” object variables in Java are polymorphic object variables can refer to objects or their declared type AND any objects that are descendants of the declared type Property p = new Property(); p = new Railroad(); // legal! p = new Utility(); //legal! p = new Street(); Object obj1; // = what? CS314 Polymorphism

Data Type object variables have: a declared type. Also called the static type. a dynamic type. What is the actual type of the pointee at run time or when a particular statement is executed. Method calls are syntactically legal if the method is in the declared type or any ancestor of the declared type The actual method that is executed at runtime is based on the dynamic type dynamic dispatch CS314 Polymorphism

Clicker Question 1 Consider the following class declarations: public class BoardSpace public class Property extends BoardSpace public class Street extends Property public class Railroad extends Property Which of the following statements would cause a syntax error? (Assume all classes have a zero argument constructor.) A. Object obj = new Railroad(); B. Street s = new BoardSpace(); C. BoardSpace b = new Street(); D. Railroad r = new Street(); E. More than one of these CS314 Polymorphism

Method LookUp To determine if a method is legal the compiler looks in the class based on the declared type if it finds it great, if not go to the super class and look there continue until the method is found, or the Object class is reached and the method was never found. (Compile error) To determine which method is actually executed the run time system: starts with the actual run time class of the object that is calling the method search the class for that method if found, execute it, otherwise go to the super class and keep looking repeat until a version is found Is it possible the runtime system won’t find a method? CS314 Polymorphism

Clicker Question 2 A. !!live B. !eggegg C. !egglive D. !!! What is output by the code to the right when run? A. !!live B. !eggegg C. !egglive D. !!! E. eggegglive public class Animal{ public String bt(){ return "!"; } } public class Mammal extends Animal{ public String bt(){ return "live"; } } public class Platypus extends Mammal{ public String bt(){ return "egg";} Animal a1 = new Animal(); Animal a2 = new Platypus(); Mammal m1 = new Platypus(); System.out.print( a1.bt() ); System.out.print( a2.bt() ); System.out.print( m1.bt() ); CS314 Polymorphism

Why Bother? Inheritance allows programs to model relationships in the real world if the program follows the model it may be easier to write Inheritance allows code reuse complete programs faster (especially large programs) Polymorphism allows code reuse in another way Inheritance and polymorphism allow programmers to create generic algorithms CS314 Polymorphism

Genericity One of the goals of OOP is the support of code reuse to allow more efficient program development If a algorithm is essentially the same, but the code would vary based on the data type genericity allows only a single version of that code to exist in Java, there are 2 ways of doing this polymorphism and the inheritance requirement generics CS314 Polymorphism

A Generic List Class CS314 Polymorphism

Back to IntList We may find IntList useful, but what if we want a List of Strings? Rectangles? Lists? What if I am not sure? Are the List algorithms different if I am storing Strings instead of ints? How can we make a generic List class? CS314 Polymorphism

Generic List Class required changes How does toString have to change? why?!?! A good example of why keyword this is necessary from toString What can a List hold now? How many List classes do I need? CS314 Polymorphism

Writing an equals Method How to check if two objects are equal? if(objA == objA) // does this work? Why not this public boolean equals(List other) Because public void foo(List a, Object b) if( a.equals(b) ) System.out.println( same ) what if b is really a List? CS314 Polymorphism

equals method read the javadoc carefully! Must handle null Parameter must be Object otherwise overloading instead of overriding causes must handle cases when parameter is not same data type as calling object instanceof or getClass() don't rely on toString and String's equals CS314 Polymorphism

the createASet example public Object[] createASet(Object[] items) { /* pre: items != null, no elements of items = null post: return an array of Objects that represents a set of the elements in items. (all duplicates removed) */ {5, 1, 2, 3, 2, 3, 1, 5} -> {5, 1, 2, 3} CS314 Polymorphism

createASet examples String[] sList = {"Texas", "texas", "Texas", "Texas", "UT", "texas"}; Object[] sSet = createASet(sList); for(int i = 0; i < sSet.length; i++) System.out.println( sSet[i] ); Object[] list = {"Hi", 1, 4, 3.3, true, new ArrayList(), "Hi", 3.3, 4}; Object[] set = createASet(list); for(int i = 0; i < set.length; i++) System.out.println( set[i] ); CS314 Polymorphism