Polymorphism CSC 171 FALL 2004 LECTURE 15. Reading Read Chapter 9 of Horstmann.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
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.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Hello AP Computer Science!. What are some of the things that you have used computers for?
Sadegh Aliakbary Sharif University of Technology Fall 2011.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Programming With Java ICS Chapter 8 Polymorphism.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Mixing integer and floating point numbers in an arithmetic operation.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February Objectives of this presentation:
C++ The reason why it is still in use. Table of Contents Disadvantages Disadvantages Advantages Advantages Compare with object-oriented programming language.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
Interfaces and Polymorphism CS 162 (Summer 2009).
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
Support standard JavaScript code with static typing Encapsulation through classes and modules Support for constructors, properties and.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Classes - Intermediate
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Object Oriented Programming Lecture 2: BallWorld.
Learning Plan 6 Java Programming Intro to Object Oriented Programming.
Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Web Design & Development Lecture 9
Lecture 17: Polymorphism (Part II)
Interfaces and Polymorphism
Chapter 11 – Interfaces and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Interfaces and Constructors
CMSC 202 Polymorphism.
CS18000: Problem Solving and Object-Oriented Programming
class PrintOnetoTen { public static void main(String args[]) {
Lecture 18: Polymorphism (Part II)
Object-Oriented Programming Part 3 Bank Account Embezzling
slides created by Ethan Apter
Presentation transcript:

Polymorphism CSC 171 FALL 2004 LECTURE 15

Reading Read Chapter 9 of Horstmann

Polymorphism Poly – “many” Morphism – “form”

Polymorphism Polymorphism in JAVA is the idea that the behavior of an object can vary based on it’s type.

Similar Idea “Overloading” a method. We don’t just have one possible “add” method

public static Rational add(Rational r1, Rational r2) { int newNum = r1.numerator * r2.denominator + r1.denominator * r2.numerator ; int newDenom = r1.denominator * r2.denominator; return new Rational(newNum,newDenom); } public Rational add(Rational r) { int newNum = this.numerator * r.denominator + r.denominator * this.numerator ; int newDenom = this.denominator * r.denominator; return new Rational(newNum,newDenom); }

Key Difference In general, we have multiple ways of doing something. The machine must decide on which to use. With overloading, this decision is made at compile time (early binding) With polymorphism, this decision is made at run time (late binding).

Excercise Let’s say we want to have a computer program write stories for us – Elisa – Boris

Example - exercise Both freshmen and seniors are “greeters”. When asked to greet, freshmen say “Ummm.. Like.. Yo!” When asked to greet, seniors say “Hello, and how are you this fine day?”

Exercise Write a method “greeting” that takes an object as a parameter. Have objects of the “Freshman” type return the string “Ummm.. Like.. Yo!” Have objects of the “Senior” type return the string “Hello, and how are you this fine day?”

public String greeting (Object o) { if (o instanceof Freshman) return “Ummm.. Like.. Yo!”; if (o instanceof Freshman) return “Hello, and how are you this fine day?” return “”; }

Example - exercise Define a “Greeter” interface (based on the defs of the previous lecture). Anything that implements “Greeter” must have a parameterless “greeting” method that returns a string.

public interface Greeter { public String greeting () ; }

Example - exercise Both freshmen and seniors are “greeters”. When asked to greet, freshmen say “Ummm.. Like.. Yo!” When asked to greet, seniors say “Hello, and how are you this fine day?”

public class Freshmen implements Greeter { public String greeting() { return “Ummm.. Like.. Yo!”; } public class Senior implements Greeter { public String greeting() { return “Hello, and how are you this fine day?” ; }

Why is this useful We can write code that works the same way for a lot of different types of objects. Even though the objects behave differently. Suppose we are writing a program that automatically generates stories. A common phrase involves a greeting followed by “, the person said”.

Write a JAVA method that takes a “Greeter” object and returns a string with the greeting followed by “he said”.

public String sentence(Greeter g) { return g.greeting() + “, he said.”; }

Using it in context public static void main(String args) { Freshmen ted = new Freshman: Senior bill = new Senior; System.out.println(“Ted looked at Bill. “ + sentence(ted)); System.out.println(“Bill regarded Ted. “ + sentence(bill)); }

We can accomplish the same thing (different behavior) with program logic instead of polymorphism However, the polymorphic solution allows us to reduce program complexity and encapsulate data. This reduces the potential for faults This allows us to add new types later.