CS221 - Computer Science II Polymorphism 1. CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
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.
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
More about inheritance Exploring polymorphism 3.0.
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.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
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!
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
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,
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
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.
Modern Programming Tools And Techniques-I
Polymorphism.
Chapter 11 Inheritance and Polymorphism
Objects as a programming concept
More about 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
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.
Chapter 9 Inheritance and Polymorphism
Lecture 19 - Inheritance (Contd).
Polymorphism.
More about inheritance
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
Advanced Programming in Java
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Polymorphism.
Presentation transcript:

CS221 - Computer Science II Polymorphism 1

CS221 - Computer Science II Polymorphism 2 Outline  Explanation of polymorphism.  Using polymorphism to make generic classes.

CS221 - Computer Science II Polymorphism 3

CS221 - Computer Science II Polymorphism 4  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 ClosedShape s = new ClosedShape(); s = new ClosedShape(); //legal! s = new Rectangle(); // legal!

CS221 - Computer Science II Polymorphism 5 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

CS221 - Computer Science II Polymorphism 6 Question 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 default 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

CS221 - Computer Science II Polymorphism 7 What’s the Output? ClosedShape s = new ClosedShape(1,2); System.out.println( s.toString() ); s = new Rectangle(2, 3, 4, 5); System.out.println( s.toString() ); s = new Circle(4, 5, 10); System.out.println( s.toString() ); s = new ClosedShape(); System.out.println( s.toString() );

CS221 - Computer Science II Polymorphism 8 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?

CS221 - Computer Science II Polymorphism 9 Question 2 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() );

CS221 - Computer Science II Polymorphism 10 Why Bother?  Polymorphism allows code reuse in another way (We will explore this next time)  Inheritance and polymorphism allow programmers to create generic algorithms