Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Object-Oriented Programming Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Session 15 Chapter 8: Understanding Inheritance. Lab Exercise Solution Recall that we needed to know if a mousePress occurred on a target. We tried to.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
OO as a language for acm l OO phrase l Mental model of key concepts.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Session 22 Chapter 11: Implications of Inheritance.
Java Object Oriented Programming Encapsulation, Inheritance, Polymorphism (Java: An Eventful Approach - Ch. 17, 21.7), Slides Credit: Bruce, Danyluk and.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Session 25 Test 2 Review. Test 2 Details Tuesday, November 22 in class –(Anybody planning on taking it on Friday?) Closed book Closed notes, except for.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
ISBN Chapter 12 Support for Object-Oriented Programming.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
A structured walkthrough
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Types of Programming Languages
Object Oriented Analysis and Design
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 10: Mechanisms for Software Reuse
Presentation transcript:

Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications

Decorated Ball import java.awt.*; public class DecoratedBall extends MovableBall { private MovableBall workerBall; public DecoratedBall( MovableBall aBall ) { super(); workerBall = aBall; } protected MovableBall worker() { return workerBall; } // *** MESSAGES DELEGATED TO THE INSTANCE VARIABLE public void move() { workerBall.move(); } public void paint( Graphics g ) { workerBall.paint( g ); } public void setColor( Color newColor ) { workerBall.setColor( newColor ); } public Color color() { return workerBall.color(); } protected int radius() { return workerBall.radius(); }

Decorated Ball protected int x() { return workerBall.x(); } protected int y() { return workerBall.y(); } protected Rectangle region() { return workerBall.region(); } protected void moveTo( int x, int y ) { workerBall.moveTo( x, y ); } protected void setMotion( double ndx, double ndy ) { workerBall.setMotion( ndx, ndy ); } protected double xMotion() { return workerBall.xMotion(); } protected double yMotion() { return workerBall.yMotion(); } }

Decorated Ball public class ExpandingBall extends DecoratedBall { public ExpandingBall( MovableBall aBall ) { super( aBall ); } public void move () { super.move(); region().height = ( region().height * 11 ) / 10; region().width = ( region().width * 11 ) / 10; } }

Decorated Ball public class DeceleratingBall extends DecoratedBall public DeceleratingBall( MovableBall aBall ) { super( aBall ); } public void move() { super.move(); setMotion( xMotion() * 0.95, yMotion() * 0.95 ); } }

Chapter 12: Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different objects in the same place in our program, i.e., polymorphism depends on objects that are substitutable for one another In object-oriented languages, polymorphism results from: –the is-a relationship of inheritance, –the mechanisms of message passing, and –concept of substitutability and polymorphic variables

Polymorphic Variable A polymorphic variable can hold many different types of values –Example: “PinBallTarget target” can be assigned a “Hole”, “ScorePad”, etc. Object-oriented languages often restrict the types of values to being subclasses of the declared type of the variable.

Varieties of Polymorphism Pure polymorphism - a single function (code body) can operate on data of any type. overriding deferred methods / generics overloading / ad hoc polymorphism – multiple functions (code bodies) with the same name each handle a different data type Most Least

Pure Polymorphism In dynamically-typed languages (e.g., Scheme) we can write one function that operate on data of any type: (define (sum-of-squares x y) (+ (square x) (square y) ) ) –neither the types of the formal parameters (x and y) or the result is known until run-time – sum-of-squares is not even limited to operating on numeric types –The only requires are that the square operator knows how to handle x’s and y’s type the + operator knows how to handle arguments of (square x)’s and (square y)’s types (In Java, that could be two Strings!) Some OOP languages like Smalltalk are dynamically-typed and support pure polymorphism at the method level

Overloading / Ad hoc Polymorphism Multiple function bodies have the same name with each operating on different types of data This common name is said to be overloaded In strongly-typed language (i.e., Java), the compiler can often determine the appropriate function and generate only a single code sequence Different forms of overloading exist –Overloading from separate classes: the same method name (or operator) is used by different classes that are not linked by inheritance (e.g, +, isEmpty()) –Parametric overloading: the same method name is used in the same class, but with different argument signatures

Overloading from Separate Classes All object-oriented languages permit the form of overloading Resolution of the overloaded name by the compiler is easy by observing the class of the receiver, e.g., myVector.isEmpty() Not bad programming style either if the name is meaningful in each class The meaning might be complete different (i.e., dissimilar) in each class, e.g., –“draw” in a card class, and –“draw” in a circle class

Parametric Overloading the same method name is used in the same class, but with different argument signatures Constructor methods often use parametric overloading, e.g., the Rectangle class Rectangle() Rectangle(int width, int height) Rectangle(int width, int height, int x, int y) Rectangle(Point origin) Rectangle(Dimension widthAndHeight) Rectangle(Point origin, Dimension widthAndHeight) Any method (not just constructors) can be overloaded as long as each method has a distinct argument signature

Overriding The superclass has a general method, say doSomething(), but a subclass defines a method with the same name that hides access to the superclass’s method. Two different forms of overriding: –replacement – code from the parent class is not executed at all –refinement – parent’s code is executed from the subclasses code by using “super.doSomething()”

Number - Abstract Class Example Parent class for numeric wrapper classes: Integer, Long, Double, etc. Subclasses must override abstract methods public abstract class Number { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue() public byte byteValue() {return (byte) intValue;} public short shortValue() { return (short) intValue(); } } // end Number

Generic Many languages are statically-typed, that is, they require us to declare the type of a variable in our code. Ada generics and C++ templates are constructs that provide polymorphism of this sort, in a statically-typed way. Java 5.0 (aka Java 1.5) supports generic classes, too!Java 5.0 template public class Decorator { Worker myInstanceVariable; public Worker getValue() { return myInstanceVariable; }... } This allows me to create decorators for objects of any type: –Decorator This mechanism enables programmers to create nearly-pure polymorphism by declaring the kind of object that can be "wrapped". The template gives enough information that the compiler can verify that the polymorphic behavior will exist at run-time.

Efficiency and Polymorphism The use of polymorphism tends to optimize program development time and reliability by aiding in ability to reuse code However, the trade-off is that at run-time the execution is slightly slower