Lecture 5: Object-Oriented Design, Part 2 CompSci 230 Software Construction.

Slides:



Advertisements
Similar presentations
Object-Oriented Design – Key Concepts Version 1.1 of : added learning objectives CompSci 230 Software Construction.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page: Lecture.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
UML – Class Diagrams.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Object Oriented System Development with VB .NET
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Object-oriented Programming Concepts
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 22: Object-Oriented Programming COMP 144 Programming Language Concepts Spring 2002.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Object-Oriented Analysis and Design
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
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.
Object-Oriented Design, Part 2 CompSci 230 S Software Construction.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CompSci 230 Software Construction Course Revision: Themes A, B & C S
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.
Introduction To System Analysis and Design
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Class Diagrams: Advanced Concepts. 2 Overview Class diagrams are the most commonly used diagrams in UML. Class diagrams are the most commonly used diagrams.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Object Oriented Software Development
Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Salman Marvasti Sharif University of Technology Winter 2015.
Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
Chapter 4 Basic Object-Oriented Concepts. Chapter 4 Objectives Class vs. Object Attributes of a class Object relationships Class Methods (Operations)
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
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.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
CompSci 280 S Introduction to Software Development
Unified Modeling Language (UML)
Inheritance ITI1121 Nour El Kadri.
Object-Oriented Analysis and Design
Lecture 6: Object-Oriented Design, Part 3
Chapter 11 Object-Oriented Design
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
UML Class Diagrams: Basic Concepts
Software Engineering Lecture #11.
Week 6 Object-Oriented Programming (2): Polymorphism
Object-Oriented PHP (1)
Lecture 1 Introduction to Software Construction
Presentation transcript:

Lecture 5: Object-Oriented Design, Part 2 CompSci 230 Software Construction

Agenda & Reading COMPSCI 230: OOD22  Topics:  Abstraction and information hiding  Inheritance, instantiation, and polymorphism  Association, aggregation, and composition  Reading  The Java Tutorials, on InheritanceInheritance  Wikipedia, on Class DiagramClass Diagram

Learning Objectives COMPSCI 230: OOD23  Students will have a strong conceptual foundation for their future uses of the OO features of Java  Abstraction and information hiding (refined into two definitions)  Inheritance (is-a) vs composition/aggregation/association (has-a)  Polymorphism  Students will be able to discuss the OO features of an existing design (as expressed in a Java program or a class diagram)  Students will be competent at basic OO design  Teaching strategy in this unit: first an overview, then “dig deeper” the second time around…

Abstraction COMPSCI 230: OOD24  An abstraction is a view or representation of an entity that includes only the attributes of significance in a particular context.  Abstraction is essential when working with complex systems.  Without abstraction, the programmer faces an overwhelming level of detail. Complex Object Idealised Model Ignore Inessential Details Instance variables: Names : String DateOfBirth : Date Methods: getSurname(): String getFirstname (): String getAge(): Integer setSurname(name: String): void Customer Instance Variables: item: Vector Methods: push(object:Object): void pop(): Object Stack Instance Variables: topLeftCorner: Point Width: Integer Height: Integer Methods: paint(): void Rectangle

Information Hiding: two definitions COMPSCI 230: OOD25 1. A programming technique. The programmer is trying to hide info by  using language features (such as I nterface, protected, private ) to restrict access to implementation details.  In the extreme case, other programmers are not allowed to “look inside” your library classes to see their implementations. 2. A design technique. The designer is trying to hide info by  defining a model which is as simple as possible (but useful, and unlikely to change).  In the extreme case, other programmers can read your high-level design documents, but they can not read (and need not read) any existing implementation.  It is usually undesirable for programmers to rely on “undocumented functions” in an implementation, and type-1 info-hiding makes it harder for these to be discovered.  Undocumented functions are subject to change with every release  Extreme type-2 info-hiding is usually undesirable  Design documents are rarely complete, accurate, and unambiguous.  Important requirements are often expressed only in the test-suite or in informal understandings among the devteam, the QA team, and primary stakeholders.

Inheritance and Instantiation COMPSCI 230: OOD26  Inheritance: create new classes from existing classes.  Instantiation: create new instances from existing classes.  Inheritance is more powerful than instantiation.  When a subclass is created by inheritance from a superclass, some of the methods and attributes in its superclass may be added or redefined.  Inheritance is an “is-a” relation.  Example: “An orange is a fruit. An apple is a fruit.”  Every instance of an Orange is a Fruit (= its superclass), but it is more accurately described as being an instance of the Orange class (= its subclass).  If there is no important difference between oranges and apples, you should simplify your design!

Polymorphism COMPSCI 230: OOD27  Different objects can respond differently to the same message.  Inheritance is the “obvious” way to obtain polymorphic behaviour in your OO design, but it may not be the best way.  Instantiations are polymorphic, if the values in their attributes affect the behaviour of their methods.  Hmmm… if you have instantiated a million objects of a single Class, could they do anything useful? Hmmm….  Worker ants are (nearly) identical, but they won’t reproduce without a Queen ant.  Ants may be important members of an ecosystem, but only if the ecosystem contains other forms of life, some inanimate objects, and an energy source.  One way to conceive of OOD is that you’re designing an ecosystem with multiple species (Classes) in an evolutionary tree.  It is possible to write a useful program in a non-OO language!  Polymorphism is not necessary in programming, but it is fundamental to OO design.

OO Analysis: Basic Questions COMPSCI 230: OOD28  What classes and instances should be in my high-level design?  To get started on answering this question, you should identify important entities, and look for commonalities among different entities.  Similar entities might be instances of the same class… but maybe there are some “natural” subclasses?  How should my classes and instances be related to each other?  We have seen the Inheritance (“is-a”) relationship.  We have also seen the Instantiation (“instance-of”) relationship.  We will now look at a few other relationships that are fundamental to OO design.

4. Composition and Aggregation  Composition (“owns-a”, “has-a”):  An object is a component of at most one composition.  When a composition instance is destroyed, all objects belonging to this instance are destroyed as well.  Example: SimCard has-a SIM  Aggregation (“has-a”):  An object can be in many aggregations.  Example: Pond has-a Duck COMPSCI 230: OOD29  These relationships create a complex class from one or more existing classes (or instances), in a whole/part relationship.

Association COMPSCI 230: OOD210  In an association, an instance of one class is linked to an instance of another class.  An aggregation is an association, because the aggregating class has instance variables which refer to objects of its parts.  A composition is an association, because the “container” or “owner” has references to its parts,  An association may have no “container”, “owner”, or “whole”.  Example: every teacher has 0 or more students, and every student has 0 or 1 teachers.

Multiplicities COMPSCI 230: OOD211  The multiplicity of an association may be important enough to include in a high-level design document.  The filled-diamond notation for compositions implies that the “whole” (Composite) class has a multiplicity of 1..1 or 0..1 – because each Part can belong to at most one whole.  No department can exist unless it is part of a university, so the University’s multiplicity in this association is 1..1 (sometimes written “1”).  A university must have at least one department.  Multiplicities of 0..* are not very informative.

Inheritance COMPSCI 230: OOD212  Generalisation  Look for conceptual commonalities in the abstractions  Common attributes/state  Common methods or behaviour  “is-a” relationship  Subclass “is-a” kind of Superclass  Superclass (base class) defines the general state and behaviour  Subclass (derived) class :  Created from an existing class, by extending the existing class, rather than rewriting it  Examples  Person  Employee & Customer (Customer “is-a” Person) Example: Person.java & Employee.java

Composition Vs Inheritance COMPSCI 230: OOD213 Means of transportation Wheeled vehiclePackhorse BicycleAutomobile Component ButtonLabelTextComponent TextFieldTextArea CarEngineCylindersCrankshaftBodyWheels Human body BoneMuscles Nervous system Skin

Composition Vs Inheritance COMPSCI 230: OOD214  Suppose we want to create a class to represent circles:  Composition or Inheritance?  Circle is a point with a radius?  Circle has a point with a radius?  General guideline  Inheritance : Is-a  Composition : has-a  But there is no rulebook - the objective is clean, understandable and maintainable code that can be implemented in reasonable time  Some more guidelines:  Inheritance provides a means for constructing highly reusable components, but needs to be used very carefully  Choose composition first when creating new classes from existing classes. You should only used inheritance if it is required by your design. If you use inheritance where composition will work, your designs will become needlessly complicated

Composition COMPSCI 230: OOD215 import java.awt.Point; public class Circle { private Point p; private int radius; public Circle (int x, int y, int radius) { p = new Point (x, y); this.radius = radius; } public double getX() { return p.getX (); } public double getY() { return p.getY (); } public int getRadius () { return radius; } // additional code } Circle c1 = new Circle(); System.out.println("x=" + c1.getX() + ", y=" + c1.getY()); System.out.println("radius=" + c1.getRadius()); Example: Circle_a/Circle.java

Inheritance  We could reuse Point class since it already has code for representing a point position COMPSCI 230: OOD216 Circle c1 = new Circle(); System.out.println("x=" + c1.getX() + ", y=" + c1.getY()); System.out.println("radius=" + c1.getRadius()); import java.awt.Point; public class Circle extends Point { private int radius; public Circle() { } public int getRadius () { return radius; } // additional code } Inherited from Point class Instance variable: radius Example: Circle_i/Circle.java

Review COMPSCI 230: OOD217  Abstraction  The ability of a language (and a designer) to take a concept and create an abstract representation of that concept within a program  Information Hiding  How well does this language, designer, and programmer hide an object’s internal implementation?  Inheritance  The “is-a” relation: important for code reuse  Polymorphism  How does this language let us treat related objects in a similar fashion?  Composition, Aggregation, Association  Types of “has-a” relations: ways to build complex classes from simpler ones.