CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

1 OBJECT-ORIENTED CONCEPTS. 2 What is an object?  An object is a software entity that mirrors the real world in some way.  A software object in OOP.
George Blank University Lecturer.
1 CIS224 Software Projects: Software Engineering and Research Methods Lecture 4 Class Models (Based on Fowler (2004, Chapters 3 & 5) and Stevens and Pooley.
Solutions to Review Questions. 4.1 Define object, class and instance. The UML Glossary gives these definitions: Object: an instance of a class. Class:
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Introduction to Design Patterns
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-Oriented PHP (1)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Object-oriented concepts.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Objects and Components. The adaptive organization The competitive environment of businesses continuously changing, and the pace of that change is increasing.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Object-Oriented Programming Chapter Chapter
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
Basic Characteristics of Object-Oriented Systems
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCE 240 – Intro to Software Engineering Lecture 3.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object-Oriented Design
Advanced Object-Oriented Programming
OOP What is problem? Solution? OOP
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented Programming
C++ Object Oriented 1.
Presentation transcript:

CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith

What is a good system like? A good system uses loosely coupled, encapsulated modules (components) that –have good interfaces allow access to important features hide irrelevant details of how module works –are good abstractions high cohesion appropriate information hiding simulate well behaviour and structure of identifiable things –are reusable –are replaceable

Why use an object-oriented approach? Supports the development of good systems Supports development that is –Architecture-centric –Component-based

What is an object? Object is a thing that has behaviour, state and identity ( Booch, G. (1991). Object-Oriented Design with Applications. Benjamin/Cummings. ) Thing –object should represent (i.e., be an abstraction of) an identifiable concept or physical thing State –all data encapsulated by object –object has a set of attributes/instance variables/data members –each attribute has a value –attribute can be mutable or immutable –set of attributes does not change, values of attributes may change Behaviour –the way an object acts and reacts in response to messages –set of messages understood by an object is fixed –way in which an object responds to a message may depend on its state Identity –Two objects with the same state are not necessarily the same object –Two objects can be equal without being the same object –An object occupies a particular place in memory and this gives it an identity –If two objects have identical state but they are not the same object, then the only thing that's different about them is their identity –An object retains its identity even when its state is changed

Example of an object myClock object that represents a clock myClock interface defines that it understands messages: getTime() setTime(newTime:Time) Time is a type whose elements are valid values of newTime How exactly myClock stores the time, gets the current time and sets a new time is not important to the user or client of the myClock object –This is implementational detail that is hidden by the interface, encapsulated within the object

Messages Selectors and arguments myClock understands two types of messages getTime() setTime(newTime:Time) every message has a selector getTime setTime a message may have arguments: getTime() has no arguments setTime(newTime:Time) has one argument, newTime, which must be of type Time arguments may be objects themselves –for example, Time may be a class of objects

Messages: Signatures every message has a signature –the combination of its selector and the pattern of types of its arguments for example, myClock might understand the messages readTime(string : String) –Reads a time value from a String readTime(file : File) –Reads a time value from a File These two messages have the same selector but different patterns of arguments: readTime(string : String) takes a String argument readTime(file : File) takes a File argument => signatures are different => could both be defined within a myClock object –Example of method overloading

Sending messages Message sent to an object by giving the object's name, followed by a full stop, followed by the message myClock.getTime() sends the message getTime() to myClock “object A sends the message msg() to object B” => message “B.msg()” occurs within one of A's methods Object can send messages to –itself –a value of one of its own attributes –an argument of one of its operations –a new object created by one of its operations

An object’s public interface object's public interface defines messages it will accept from anywhere interface usually provides signature (i.e., selector and arguments) and return type of each message –E.g., getTime() : Time public interface may also contain attributes but this is usually a bad idea –we may want to change object so that changing an attribute value has some other side effect –or may want to change the type of an attribute –can do this without changing interface if the attribute value is set or retrieved using operations –thus preserving encapsulation –Cf. example of polar and cartesian co-ordinate representations of position in a Point object

An object’s private interface methods within an object may need to access attributes directly –so an object may send itself any message that it understands and access any of its own attributes – even if they are private –e.g., a get() method will usually have to access an attribute directly within the same object => –object has a public interface which any part of the system can use and –larger private interface which the object itself and other privileged parts of the system can use an object may make different parts of its private interface available depending on its role within a particular context –it may then implement or realize two or more different public interfaces Cf. class implementing multiple interfaces in Java

Classes Each object has –Attributes Each attribute is a place to store data –Operations The messages that the object understands Each operation is implemented by a specific method Often want many objects with same attributes and methods –E.g., every object representing a customer in a company’s invoicing system –Therefore have a “template” that lets us make as many objects of a particular type as we want –This template is called a class definition

Classes and class definitions A class definition is an “object factory” or an object type specification –it defines the attributes and methods that are possessed by all objects of a particular type (or "class") Two objects are in the same class if they are created using the same class definition If the elements of a data type are objects, then that type is a class A class definition also specifies the visibility of each attribute and method –i.e., whether it is public or private Class definition therefore also defines the public and private interfaces of the objects in its class Instantiating a class means creating a new object of that class Every object is an instance of the class to which it belongs

Class example myClock is an object of class Clock with attribute - time : Time and operations + getTime() : Time + setTime(newTime : Time) Therefore each Clock object has –a private attribute called time which stores a value of type (or class) Time –a public operation with selector getTime which returns a value of type Time –a public operation with selector setTime which takes a single argument of type Time and returns no value the definition of class Clock would define specific methods to implement the operations, getTime() and setTime(newTime : Time) the public interface of class Clock would contain the two public operations, getTime() and setTime(newTime : Time) the private interface of class Clock would in addition contain the attribute, time

WRITE ONCE! A class definition can be used to make as many objects of a particular class as required Example of (probably) the most important principle in software engineering which is WRITE ONCE! Most important benefit of writing once is –You only have to define (and maintain) a thing in one place (where it is defined) – not everywhere it is used

Examples of the “Write Once” principle Using a loop to iterate over all the elements in an array –Only write once the operation that is performed on each element Defining a class and using its constructor to produce many objects of that class Factoring out code into subroutines, functions, methods or procedures

ALWAYS WRITE ONCE! ALWAYS LOOK FOR OPPORTUNITIES TO WRITE ONCE! If you write the same thing twice, that probably means you can improve your design If you only remember one thing from this course, it should be the “Write Once!” principle

Classes as components Classes (not objects) in an object-oriented system should ideally be loosely coupled, highly coherent, encapsulated modules that have good interfaces, are good abstractions and can therefore function as reusable, replaceable components Object-oriented programming languages usually support the construction of classes that can function as reusable and replaceable components

Inheritance Building a simulation of a zoo in which each animal is represented by an object Set of attributes depends on type of animal If define independent class for each animal then break “write once” principle

Inheritance Define class Animal which contains attributes common to all animals Define Bird to contain everything in Animal plus wingSpan Define Parrot to contain everything in Bird Define Quadruped to contain everything in Animal plus height, foreLegLength and hindLegLength Define Elephant to contain everything in Quadruped plus trunkLength and tuskLength Here only write each distinct attribute once Elephant inherits from Quadruped which inherits from Animal Bird inherits from Animal Quadruped is a subclass (or derived class) of Animal Quadruped is a superclass (or base class) of Elephant Quadruped is a specialization of Animal Animal is a generalization of Quadruped An Elephant is a Quadruped An Elephant is an Animal Inheritance is the process by which a class contains all the attributes and operations of its superclasses

Method overriding Each employee represented by an object in personnel system Way that net salary calculated depends on type of employee Managers can have stock options getNetSalary takes stock options into account in a Manager object but not an Employee object Method executed when Manager receives getNetSalary different from method executed when Employee receives getNetSalary message getNetSalary method in Employee is overridden in the Manager class

When to use inheritance Using inheritance increases coupling between classes which makes them less reusable So we should only allow a class to inherit from another class if it is a specialization of that other class For example –a German Shepherd is a special type of Dog, so it would be reasonable to define a class, GermanShepherd, that is a subclass of some more general class, Dog –A triangle is a special kind of polygon, so it would be reasonable to have a class, Triangle that inherits from a more general superclass Polygon

Substitutability The Liskov Substitution Principle: –If p is a variable that refers to an object of class x, then p may refer to any object from any subclass of x Liskov, B. & Wing, J. M (1994). A behavioral notion of subtyping. ACM TOPLAS, 16(6), (Available online at For example –We can use a Bird, a Quadruped or an Elephant anywhere that an Animal is used –We can use an Elephant anywhere that a Quadruped is used –We can use a Manager anywhere that an Employee is used –We can use a Triangle anywhere that a Polygon is used

Polymorphism The Liskov Substitution principle says that a variable that points or refers to an object of class x may point or refer to an object from any subclass of the class of x Such a variable is said to be polymorphic (Greek: poly = many, morph = shape) because it can refer to objects of many different “shapes” (i.e., classes)

Example of polymorphism We define a base class, Polygon, which is an abstraction of a polygon and three subclasses of Polygon: –Circle, Square and Triangle Suppose every Polygon knows how to display itself on a screen and that we invoke this behaviour in a Polygon (i.e., we get it to display itself) by sending it the message display() –i.e., every Polygon has an operation whose signature is display() The method executed when we send a display() message to a Triangle is different from the method executed when we send the display() message to a Square: aTriangle.display() draws the triangle represented by the variable, aTriangle aSquare.display() draws the square represented by the variable, aSquare

Example of polymorphism (cont.) Suppose that the variable polygonList refers to a collection of Polygon objects, which might include Squares, Triangles and Circles Say we want to iterate over polygonList, displaying each Polygon in turn: for each p in polygonList p.display() When this loop is executed, variable p might refer to a Circle, a Square or a Triangle –That is, p is polymorphic.

Dynamic binding When the following loop is executed for each p in polygonList p.display() –method executed when display() sent to p depends on class of p If p is a Triangle, a triangle is displayed If p is a Square, a square is displayed If p is a Circle, a circle is displayed At compile time, compiler only checks that p is a Polygon Does not know whether p is a Triangle, Square or Circle until run time Therefore specific method executed is only bound to the command p.display() at run time This is called dynamic or late binding

Summary Object-oriented design supports development of good systems An object is a thing with behaviour, state and identity An object has a public and a private interface A class definition is an object factory for making objects with a particular set of attributes and methods WRITE ONCE! A class that is a loosely coupled, coherent, encapsulated module with a good interface can function as a reusable, replaceable component Inheritance and method overriding The Liskov Substitution Principle Polymorphism and dynamic binding