C10, Mechanisms for Software Reuse. Substitutability A variable declared as one type holds a value of another type In Java either: Subclass: allPiles.

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
OOP Languages: Java vs C++
Chapter 3 Introduction to Collections – Stacks Modified
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
1 Computer Science 340 Software Design & Testing Inheritance.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Object Oriented Software Development
Session 19 Chapter 10 – Mechanisms for Software Reuse.
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.
Session 13 Pinball Game Construction Kit (Version 3):
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Topics Inheritance introduction
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Nested Classes CompSci 230 S Software Construction.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Inheritance and Polymorphism
Object Oriented Programming in Java Habib Rostami Lecture 10.
C19: Collection Classes (don’t forget to look at all the online code examples)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Modeling with Inheritance
Border Layout, using Panels, Introduction to PinBallGame
Lecture 10 Concepts of Programming Languages
Chapter 10: Mechanisms for Software Reuse
Presentation transcript:

C10, Mechanisms for Software Reuse

Substitutability A variable declared as one type holds a value of another type In Java either: Subclass: allPiles = new CardPile[13]; allPiles[0] = new DeckPile(335,30); Interface: FireButtonListener implements ActionListener … fire.addActionListener(new FireButtonListener);

Is-a rule, Has-a rule Is-a holds when the first concept is a specialization of the second concept: a Florist is-a ShopKeeper Rule: an X is-a Y “sounds correct” Has-a holds when the second concept is a component of the first concept: a Car has-a Engine Rule: an X has-a Y “sounds correct” Sometimes the distinction is not clear/easy

Inheritance of code, of behaviour Is-a achieved in two ways in Java: If behavior (actual code) and/or data values can meaningfully be inherited -> use subclasses (CannonGame extends Frame) If the specification of behaviors (method headers or “signatures”) makes sense -> use interfaces (FireButtonListener implements ActionListener)

Composition and Inheritance Sometimes the distinction is not easy. Suppose we want to implement a Stack using class Vector (which supplies almost all necessary behavior anyway): class Vector { public boolean isEmpty() {…} public int size() {…} public void addElement(Object value) {…} public Object lastElement() {…} public Object removeElementAt(int index) {…} …}

Using composition class Stack { private Vector data; public Stack() { data = new Vector();} public boolean empty() {return data.isEmpty();} public push(Object item) {data.addElement(item); return item;} public Object peek() {return data.lastElement();} public Object pop() { Object result = data.lastElement(); // or: peek(); data.removeElementAt(data.size()-1); return result; Clean code, e.g. Vector x = new Stack(); not possible!

Using Inheritance class Stack extends Vector { public Object push(Object item) { addElement(item); return item;} public Object peek() { return elementAt(size()-1);} public Object pop() { Object obj = peek(); removeElementAt(size()-1); return obj; } Simpler code, but unwanted stuff possible: Vector x = new Stack(); x.removeElementAt(17);

Compostion vs. Inheritance Not substitutable Simple, makes all methods explicit Easy to change underlying data-structure Can only access public stuff of the data-structure Longer code, but everything specified locally One additional indirection, (but JIT-inlining) Substitutable Allows for all, even inappropriate, parent class methods More tricky, especially if uses depend on parent methods Can access public and protected parent class stuff Shorter code, but must fully understand parent class, too Slight speed advantage, one indirection less

Combining Composition and Inheritance Subclasses of InputStream: ByteArrayInputStream, FileInputStream, … But: class FilterInputStream extends InputStream { protected InputStream is; … } So InputStream is both a parent and a component of FilterInputStream, used to add functionality “around” some InputStream, called a “filter” or “wrapper”

Dynamic composition Inheritance: link fixed at compile-time Composition: more flexible, can be changed during runtime, especially useful to model internal “state change”: class Frog { private FrogBehavior b; public Frog() { b = new TadPoleBehavior();} public grow() { if (b.growUp()) b = new AdultFrogBehavior(); b.grow(); b.swim(); }}

Inheritance of inner classes E.g. listener classes were constructed using inheritance, but are themselves components in the application class: public class PinBallGame extends Frame { … private class MouseKeeper extends MouseAdapter { …} private class PinBallThread extends Thread {…} }

Unnamed classes Syntactic suger as a shortcut for cases where we create only one instance of a class, and the class is relatively simple, e.g. FireButtonListener of CannonGame: class CannonWorld extends Frame { … private class FireButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) {…} } public CannonWorld() { fire.addActionListener(new FireButtonListener()); … }

“Unnamed” FireButtonListener class CannonWorld extends Frame { … public CannonWorld() { fire.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {…} });... }

Summary Two common techniques for reuse: Inheritance: “is-a” Composition: “has-a” Distinction not always easy “Filters” (as found in the IO library) combine inheritance and composition