Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

Similar documents
Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

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

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

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

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

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

Credit where Credit is Due. Lecture 4: Fundamentals of Object Technology. Goals for this Lecture. Real-World Objects

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

Good-Enough Design. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/17/2009

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

Originality is Overrated: OO Design Principles

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

ECE 122. Engineering Problem Solving with Java

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

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

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

Inheritance, and Polymorphism.

INF 111 / CSE 121: Announcements Quiz #3- Thursday What will it cover?

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

Java Object Oriented Design. CSC207 Fall 2014

ENCAPSULATION AND POLYMORPHISM

Ready for the Real World. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 8 09/18/2007

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS1210 Lecture 28 Mar. 27, 2019

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

ITI Introduction to Computing II

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Inheritance and Encapsulation. Amit Gupta

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Object- Oriented Design with UML and Java Part I: Fundamentals

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

ITI Introduction to Computing II

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

Data Structures (list, dictionary, tuples, sets, strings)

What s Next. INF 117 Project in Software Engineering. Lecture Notes -Spring Quarter, Michele Rousseau Set 6 System Architecture, UML

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

isinstance and While Loops

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

Recursion. Recursion [Bono] 1

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Exam Duration: 2hrs and 30min Software Design

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

G52CPP C++ Programming Lecture 9

CS Week 14. Jim Williams, PhD

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

MechEng SE3 Lecture 7 Domain Modelling

PREPARING FOR PRELIM 2

Good-Enough Design & Version Control. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 11 02/16/2010

Java Programming Lecture 7

Part 2: The Material PART 2

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

The return Statement

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

Example: Class MSDie Introduction to Graphs and NetworkX

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

PROGRAMMING LANGUAGE 2

REVIEW OF THE BASIC CHARACTERISTICS OF OBJECT ORIENTATION

CS112 Lecture: Inheritance and Polymorphism

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

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

JAVA MOCK TEST JAVA MOCK TEST II

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Example: Fibonacci Numbers

Object-Oriented Programming

Lecture Notes on Programming Languages

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

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

CS1 Lecture 27 Mar. 26, 2018

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Basic Object-Oriented Concepts. 5-Oct-17

Structured Programming

Advanced Placement Computer Science. Inheritance and Polymorphism

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

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Reviewing OO Concepts

Test-Driven Development

CS 1110 Final, December 17th, Question Points Score Total: 100

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

COMP 110/L Lecture 19. Kyle Dewey

CIS 110: Introduction to computer programming

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Inheritance and Polymorphism in Java

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Programming II (CS300)

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Transcription:

Object Fundamentals, Part One Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009 1

Lecture Goals Introduce basic concepts, terminology, and notations for object-oriented analysis, design, and programming A benefit of the OO approach is that the same concepts appear in all three stages of development Start with material presented in Appendix II of your textbook Continue (in lecture 3) with additional material from previous versions of this class as well as from Head First Java by Sierra & Bates, O'Reilly, 2003 Will present examples and code throughout 2

Big Picture View OO techniques view software systems as systems of communicating objects Each object is an instance of a class All objects of a class share similar features attributes methods Classes can be specialized by subclasses Objects communicate by sending messages 3

Welcome to Objectville What were the major concepts discussed in Appendix II of the textbook? Unified Modeling Language (UML) Class Diagrams Inheritance Polymorphism Encapsulation 4

UML UML stands for Unified Modeling Language UML defines a standard set of notations for use in modeling objectoriented systems Throughout the semester we will encounter UML in the form of class diagrams sequence/collaboration diagrams state diagrams activity diagrams, use case diagrams, and more 5

Class Diagrams Class Name All parts are optional except the class name Attributes (member variables) Methods Airplane speed: int getspeed(): int setspeed(int) A class is represented as a rectangle This rectangle says that there is a class called Airplane that could potentially have many instances, each with its own speed variable and methods to access it 6

Translation to Code Class diagrams can be translated into code in a fairly straightforward manner Define the class with the specified name Define specified attributes (assume private access) Define specified method skeletons (assume public) May have to deal with unspecified information Types are optional in class diagrams Class diagrams typically do not specify constructors constructors are used to initialize an object 7

Airplane in Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Airplane { private int speed; public Airplane(int speed) { this.speed = speed; } public int getspeed() { return speed; } public void setspeed(int speed) { this.speed = speed; } } 8

Airplane in Python 1 2 3 4 5 6 7 8 9 10 class Airplane(object): def init (self, speed): self.speed = speed def getspeed(self): return self.speed; def setspeed(self, speed): self.speed = speed 9

Airplane in Ruby 1 class Airplane 2 3 attr_accessor :speed 4 5 6 7 def initialize(speed) end @speed = speed 8 9 end 10

Using these Classes? The materials for this lecture contains source code that shows how to use these classes Demonstration Airplane.java, Airplane.py, Airplane.rb Be sure to attempt to run these examples on your own It will be good experience to learn how to run Java, Python, and Ruby programs on your personal machine or on a Lab machine (either ITS or CSEL) 11

Inheritance Inheritance refers to the ability of one class to inherit behavior from another class and change that behavior if needed Inheritance lets you build classes based on other classes and avoid duplicating and repeating code Airplane speed: int getspeed(): int setspeed(int) Note: UML notation to indicate inheritance is a line between two classes with a triangle pointing at the base class or superclass Jet MULTIPLIER: int accelerate() 12

Inheriting From Airplane (in Java) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Jet extends Airplane { private static final int MULTIPLIER = 2; public Jet(int id, int speed) { super(id, speed); } public void setspeed(int speed) { super.setspeed(speed * MULTIPLIER); } public void accelerate() { super.setspeed(getspeed() * 2); } } Note: extends keyword indicates inheritance super() and super keyword is used to refer to superclass No need to define getspeed() method; its inherited! setspeed() method overrides behavior of setspeed() in Airplane subclass can define new behaviors, such as accelerate() 13

Inheritance in Python 1 2 3 4 5 6 7 8 9 10 11 12 13 class Jet(Airplane): MULTIPLIER = 2 def init (self, id, speed): super(jet, self). init (id, speed) def setspeed(self, speed): super(jet, self).setspeed(speed * Jet.MULTIPLIER) def accelerate(self): super(jet, self).setspeed(self.getspeed() * 2); 14

Inheritance in Ruby 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Jet < Airplane @@MULTIPLIER = 2 def initialize(id, speed) super(id, speed) end def speed=(speed) super(speed * @@MULTIPLIER) end def accelerate() @speed = @speed * 2 end end 15

Polymorphism: Many Forms From the textbook: When one class inherits from another, then polymorphism allows a subclass to stand in for the superclass. Implication: both of these are legal statements Airplane plane = new Airplane() Airplane plane = new Jet() Any code that uses the plane variable will treat it as an Airplane this provides flexibility, since that code will run unchanged, indeed it doesn t even need to be recompiled, when new Airplane subclasses are created 16

Encapsulation Encapsulation is when you hide parts of your data from the rest of your application and limit the ability for other parts of your code to access that data Encapsulation lets you protect information in your objects from being used incorrectly Closely Related Concept: Abstraction What features does a class provide to its users? What services can it perform? Indeed, abstraction is the MOST IMPORTANT concern in OO A&D!! 17

Encapsulation Example The speed instance variable is private in Airplane. That means that Jet doesn t have direct access to it. Nor does any client of Airplane or Jet objects Imagine if we changed speed s visibility to public 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Airplane... public void setspeed(int speed) { this.speed = speed; }... Jet... public void setspeed(int speed) { super.setspeed(speed * MULTIPLIER); }... The encapsulation of Jet s setspeed() method would be destroyed Demonstration 18

Summary OO software is a system of communicating objects UML provides standard notations for documenting the structure of OO systems Classes define the features of objects, both their data and behavior Inheritance allows classes to share behavior and avoid duplicating/ repeating code Polymorphism allows a subclass to stand in for its superclass Encapsulation occurs when you hide parts of your code from other parts, thereby protecting it 19

Quick Exercise Develop a UML class diagram for the following two classes A Person class that stores a person s name, age, and favorite color An Employee class that stores a person s job title and salary Create the diagram such that the following Java code fragment would have a chance at running: Employee e = new Employee( Ken, 41, blue, Associate Professor, 0); System.out.println( + e.getage()); Person p =. Employe e = new Employee(p, prof, 0); 20

Ken s Corner (I) Forgot to mention <http://slashdot.org/> on Tuesday Developer-Oriented discussion forum with the motto: News for Nerds. Stuff that Matters. Today s subject: Python Generators Imagine you have been asked to create a function that produces a sequence of numbers, say the Fibonacci sequence You might decide to take a parameter of the number of values to generate in the sequence and then return the values in a list 21

Ken s Corner (II) def fib_list(n): results = [] a, b = 0, 1 for i in range(n): results.append(b) a, b = b, a+b return results fib_list(5) returns [1, 1, 2, 3, 5] 22

Ken s Corner (III) This approach works great as long as you don t pass in an unreasonable number for n Why? Consider needing to process each element of this sequence, if you did this list = fib_list(some_huge_number) for i in list: <do something> You need to essentially process the list twice: first creating it and then looping over it if it s a really big list then you also can run into memory-related problems 23

Ken s Corner (IV) To get around this problem, python added a feature called generators which are functions that contain at least one use of the yield keyword When a generator encounters a yield, it saves all the values of its local variables and then returns the value specified by the yield keyword s expression The next time the generator is called, execution begins just after the statement that contained the yield keyword, with all local variables restored to their previous value 24

Ken s Corner (IV) def fib(n): count = 0 a, b = 0, 1 while count < n: yield b count += 1 a, b = b, a+b This produces the first n numbers in the Fibonacci sequence, one value at a time. When you call fib(100), you get back an iterator that can be used to loop over the values in the sequence. You can either put the generator directly into a for loop or you can call the generator s next() function to get the next value of the sequence. 25

Ken s Corner (V) for i in fib(100): print(i) The above will print the first 100 values of the Fibonacci sequence Note that this solves the problem on slide 23 You can now loop over and process the sequence at the same time You don t have to create a potentially huge list of numbers and then loop over it So generators are an excellent way of producing (possibly infinite) sequences in an efficient manner Generators can be used in lots of different ways including when implementing state machines, parsers, and looping over large data sets 26

Ken s Corner (VI) You don t need to use the fib_list() style of code to create lists either Instead, you can hand a generator to the list constructor and it will create a list from the generator In this case, you do have to be aware of memory constraints results = list(fib(20)) print(results) would produce [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765] 27

Coming Up Next Lecture 3: Object Fundamentals Continued No reading assignment Note: Lecture 3 will repeat some of the things mentioned in this lecture Lecture 4: Great Software Read Chapter 1 of the OO A&D book 28