CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

Similar documents
Inheritance, Polymorphism, and Interfaces

Chapter 15: Object Oriented Programming

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Classes, Objects, and OOP in Java. June 16, 2017

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

OVERRIDING. 7/11/2015 Budditha Hettige 82

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Interfaces and Polymorphism Advanced Programming

C18a: Abstract Class and Method

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance, polymorphism, interfaces

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 9. Interfaces and Polymorphism. Chapter Goals. Chapter Goals. Using Interfaces for Code Reuse. Using Interfaces for Code Reuse

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Object Fundamentals Part Two. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 3 09/01/2009

Inheritance and Polymorphism in Java

Polymorphism and Inheritance

type conversion polymorphism (intro only) Class class

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Polymorphism Part 1 1

CSC207 Week 3. Larry Zhang

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Software Design and Analysis for Engineers

Inheritance. Transitivity

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Binghamton University. CS-140 Fall Chapter 9. Inheritance

CHAPTER 9 INTERFACES AND POLYMORPHISM

Inheritance, Polymorphism and the Object Memory Model

Inheritance and Polymorphism

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Chapter 9 Inheritance

8. Polymorphism and Inheritance

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

CS-202 Introduction to Object Oriented Programming

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Java objects: a first view [Reading: chapters 1 & 2] CSC 142 B 1

COP 3330 Final Exam Review

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Announcements/Follow-ups

Object-Oriented Concept

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Introduction to Computer Science I

VIRTUAL FUNCTIONS Chapter 10

Polymorphism CSCI 201 Principles of Software Development

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

Clarifying Roles. Jonathan Worthington German Perl Workshop 2007

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

CSSE 220 Day 13. Encapsulation Coupling and Cohesion Scoping. Please download EncapsulationExamples from your SVN

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Object Oriented Programming: Based on slides from Skrien Chapter 2

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Object Orientated Analysis and Design. Benjamin Kenwright

C++ Important Questions with Answers

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

More Relationships Between Classes

CIS 190: C/C++ Programming. Lecture 11 Polymorphism

BM214E Object Oriented Programming Lecture 11

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Abstract Classes and Interfaces

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

1.00 Lecture 8. Using An Existing Class, cont.

Introduction to Inheritance

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Inheritance and Substitution (Budd chapter 8, 10)

CST242 Object-Oriented Programming Page 1

1: Introduction to Object (1)

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Goals of the Lecture OO Programming Principles

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Inheritance and Polymorphism

Inheritance, and Polymorphism.

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

CSE 70 Final Exam Fall 2009

Transcription:

CSSE 220 Interfaces and Polymorphism Check out Interfaces from SVN

Interfaces What, When, Why, How? What: Code Structure used to express operations that multiple class have in common No method implementations No fields When: When abstracting an idea that has multiple, different implementations

Interfaces What, When, Why, How? Why: Provide method signatures and documentation Create a contract that someone must follow How: Client Code Reuse, for example, Java Event Handlers public interface InterfaceName { //method definitions //We ll look more closely at the syntax in a later slide }

Interface Types: Key Idea Interface types are like contracts A class can promise to implement an interface MUST implement every method Client code knows that the class will have those methods Compiler verifies this Any client code designed to use the interface type can automatically use the class! Interfaces help to reduce coupling by tying your design to the interface and not the class implementation. A new interface implementation can be switched out for the original without changing the rest of the code Q1

Using Interface Types Once an interface is defined, it can be used as a type. An interface type can be used anywhere that a class type is used: Say we have an interface named Pet, and Dog and Cat implement this interface Variable Declaration: Pet d = new Dog(); Pet c = new Cat(); Parameters: public static void feedpet(pet p) { } Can call with any object of type Pet: feedpet(new Dog()); feedpet(new Cat());

Using Interface Types An interface type can be used anywhere that a class type is used (examples continued from last slide): Fields: private Pet pet; Generic Type Parameters: ArrayList<Pet> pets = new ArrayList<Pet>(); pets.add(new Dog()); pets.add(new Cat());

Notation: In UML Closed triangle with a dashed line in UML is an is-a relationship Read this as: InterImpl is-an InterfaceName Q2

Why is this OK? Pet p = new Dog(); p.feed(); p = new Cat(); p.feed(); Any child type may be stored into a variable of a parent type, but not the other way around. A Dog is a Pet, and a Cat is a Pet, but a Pet is not required to be a Dog or a Cat. But how does Java know which method implementation to use? Q3

Polymorphism! (A quick intro) Origin: Poly many Morphism shape Classes implementing an interface give many differently shaped objects for the interface type Java knows what method implementation to use thanks to: Late Binding: choosing the right method based on the actual type of the implicit parameter (variable before the dot) at run time For the p.feed() example: Java decides at runtime which implementation to use based on the type of the object instance. The Dog s feed method may specify dog food, and the Cat s may specify cat food. Q4

Notation: In Code public interface InterfaceName{ /** * regular javadocs */ void methodname(int x, int y); interface, not class Automatically public, so we don t specify it } /** * regular javadocs here */ int dosomething(graphics2d g); No method body, just a semi-colon public class InterImpl implements InterfaceName { } InterImpl promises to implement all the methods declared in the InterfaceName interface

Refactoring to an Interface stringtransforms package Review the code in the stringtransforms package Attempt to refactor the given code using an interface by thinking about what operation is performed repeatedly There is a hint at the bottom if you re not quite sure where to start, but only use it if you need

How does all this help reuse? Can pass an instance of a class where an interface type is expected But only if the class implements the interface We could add new functions to textcalculator s abilities without changing the calculator itself. Sort of like application plug-ins We can use a new TransformInterface without changing the method that uses the TransformInterface instance Use interface types for field, method parameter, and return types whenever possible