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

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

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

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

Java Object Oriented Design. CSC207 Fall 2014

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

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

Inheritance, and Polymorphism.

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

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

JAVA MOCK TEST JAVA MOCK TEST II

Originality is Overrated: OO Design Principles

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Programming II (CS300)

Example: Class MSDie Introduction to Graphs and NetworkX

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

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

Inheritance and Polymorphism

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

PROGRAMMING LANGUAGE 2

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

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

Programming II (CS300)

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

Reviewing OO Concepts

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

ITI Introduction to Computing II

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

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

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

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

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

Exam Duration: 2hrs and 30min Software Design

Object Orientated Analysis and Design. Benjamin Kenwright

ITI Introduction to Computing II

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

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

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

Inheritance and Encapsulation. Amit Gupta

ECE 122. Engineering Problem Solving with Java

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

Arrays Classes & Methods, Inheritance

Programming I. Course 9 Introduction to programming

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

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

Reviewing OO Concepts

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

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP 110/L Lecture 19. Kyle Dewey

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

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

Computer Science 225 Advanced Programming Siena College Spring Topic Notes: Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

C++ Inheritance and Encapsulation

Object-Oriented Programming

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CMSC 132: Object-Oriented Programming II

What about Object-Oriented Languages?

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

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

MechEng SE3 Lecture 7 Domain Modelling

Lecture Notes on Programming Languages

FACADE & ADAPTER CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 7 09/13/2011

CS111: PROGRAMMING LANGUAGE II

Chapter 14 Abstract Classes and Interfaces

COURSE 2 DESIGN PATTERNS

CS1004: Intro to CS in Java, Spring 2005

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

Inheritance, Polymorphism, and Interfaces

8. Polymorphism and Inheritance

Defining Classes and Methods

CSC207 Week 3. Larry Zhang

CS Programming I: Inheritance

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

Programming 1 Semester 1, 2014 Assignment 3

Final Examination Semester 3 / Year 2010

Type Hierarchy. Lecture 6: OOP, autumn 2003

11. Abstract Classes and Interfaces

ENCAPSULATION AND POLYMORPHISM

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

Practice for Chapter 11

Unified Modeling Language (UML) Class Diagram

Introduction to UML What is UML? Motivations for UML Types of UML diagrams UML syntax Descriptions of the various diagram types Rational Rose (IBM.. M

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Programming Exercise 14: Inheritance and Polymorphism

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Module 10 Inheritance, Virtual Functions, and Polymorphism

Chapter 3: Inheritance and Polymorphism

Polymorphism: Inheritance Interfaces

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

QUESTIONS FOR AVERAGE BLOOMERS

Transcription:

Object Fundamentals Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007 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? 4

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

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

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

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

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 object-oriented 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 Airplane speed: int getspeed(): int setspeed(int) 6

Class Diagrams Airplane speed: int getspeed(): int setspeed(int) A class is represented as a rectangle 6

Class Diagrams Class Name Airplane speed: int getspeed(): int setspeed(int) A class is represented as a rectangle 6

Class Diagrams Class Name Attributes (member variables) Airplane speed: int getspeed(): int setspeed(int) A class is represented as a rectangle 6

Class Diagrams Class Name Attributes (member variables) Methods Airplane speed: int getspeed(): int setspeed(int) A class is represented as a rectangle 6

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 6

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 Airplane speed: int getspeed(): int setspeed(int) classes based on other classes and avoid duplicating and repeating code Jet MULTIPLIER: int accelerate() 12

Inheriting From Airplane 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 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 The encapsulation of Jet s setspeed() method would be destroyed 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); }... 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 one part of your code from some other part of your code, thereby protecting it 19

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 20