DOMAIN MODEL SPECIAL ASSOCIATIONS – COMPOSITION AND INHERITANCE SYS466.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
When is Orientated Programming NOT? Mike Fitzpatrick.
Object-Oriented Design – Key Concepts Version 1.1 of : added learning objectives CompSci 230 Software Construction.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 8 Slide 1 System modeling 2.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
UML Class and Sequence Diagrams Violet Slides adapted from Marty Stepp, CSE 403, Winter 2012 CSE 403 Spring 2012 Anton Osobov.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
PRJ566: PROJECT PLANNING AND MANAGEMENT Class Diagrams.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Chapter 25 More Design Patterns.
Object Oriented Design and UML
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Introduction to Object-oriented programming and software development Lecture 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
An Object-Oriented Approach to Programming Logic and Design
Domain Model—Part 4A: Special associations - Composition.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
DOMAIN MODE: ASSOCIATIONS, MULTIPLICITY AND ATTRIBUTE-TEXT NOTATION SYS466.
Lab 04.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Domain Model—Part 3: Associations, Multiplicity and Attribute- Text Notation.
Information Systems Engineering
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Peyman Dodangeh Sharif University of Technology Fall 2014.
DOMAIN MODEL—PART 4B: SPECIAL ASSOCIATIONS - INHERITANCE BTS430 Systems Analysis and Design using UML.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Object Oriented Analysis and Design Class and Object Diagrams.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
12 OBJECT-ORIENTED DESIGN CHAPTER
9 - Class & Method Design Model Enhancement Design to Code Proposal Presentation.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Class Diagrams Revisited. Parameterized Classes Parameterized Classes - are used to represent relationships between templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
03/10/14 Inheritance-2.
03/10/14 Chapter 9 Inheritance.
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Object Oriented Analysis and Design
Object Oriented Analysis and Design
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Slides by Steve Armstrong LeTourneau University Longview, TX
Advanced Programming Behnam Hatami Fall 2017.
Software Design Lecture : 12.
Object Oriented System Design Class Diagrams
Presentation transcript:

DOMAIN MODEL SPECIAL ASSOCIATIONS – COMPOSITION AND INHERITANCE SYS466

To begin our discussion, lets look at an Order

Order and OrderLine ORDER contains many OrderLine objects. OrderLine objects have no life outside of order. OrderLine objects have no meaning outside of order. If you delete Order, all OrderLine objects will be gone. Order encapsulates OrderLine.

Order and OrderLine What does Order encapsulates OrderLine mean? Order takes responsibility for creation of OrderLine objects Order controls all communication to OrderLine objects No other class knows that OrderLine exists But OrderLine knows about other classes (e.g. Product) OrderLine might not even appear in higher level models; only Order.

public class Order { private intorderID; private Customer customer; private List productOrderedSet; public Order(int newOrdID){ orderID = newOrdID; customer = new Customer(); productOrderedSet = new ArrayList (); } public Order(){ orderID = 0; customer = new Customer(); productOrderedSet = new ArrayList (); } public void addOrderLine(int inQty, Product inProd) { OrderLine newOrderLine = new OrderLine(inQty,inProd); productOrderedSet.add(newOrderLine); } public String getOrderedProduct(int orderLineInd) { return productOrderedSet.get(orderLineInd).getProductName(); } public int getOrderedQty(int orderLineInd) { return productOrderedSet.get(orderLineInd).getQty(); } public void addCustomer(Customer setCustomerTo) { this.customer=setCustomerTo; } public String getCustName() { return customer.getCustName(); } public int getOrderID() { return orderID; } The Order class (simplified version)

public class Order { private intorderID; private Customer customer; private List productOrderedSet; ………………………………………………………….. public void addOrderLine(int inQty, Product inProd) { OrderLine newOrderLine = new OrderLine(inQty,inProd); productOrderedSet.add(newOrderLine); } public String getOrderedProduct(int orderLineInd) { return productOrderedSet.get(orderLineInd).getProductName(); } public int getOrderedQty(int orderLineInd) { return productOrderedSet.get(orderLineInd).getQty(); } ……………………………………………………………… } Order is responsible for creating OrderLine objects and for getting OrderLine information

public class OrderLine { private int qtyOrdered; private Product product; public OrderLine(int qty,Product prod){ qtyOrdered = qty; this. product = prod; } public OrderLine(){ qtyOrdered = 0; product = new Product(); } public int getQty(){ return qtyOrdered; } public String getProductName(){ return product.getProductName(); } OrderLine knows about Product and can execute Product functions (e.g. product.GetProductName)

public class Product { private int productID; private String productName; public Product(int setToID,String setToName){ productID = setToID; productName = setToName; } public Product(){ } public String getProductName(){ return productName; } public int getProductID(){ return productID; } public void setProductName(String setToName){ productName = setToName; } public void setProductID(int setToID){ productID = setToID; } Product does not know anything about OrderLine; it does not know that OrderLine exists.

Composition This strong relationship between Order and OrderLine is called a COMPOSITION and is shown using the filled in diamond symbol. Order is the container. OrderLine is the component or contained class.

The Composition Symbol In order to specify composition we need to do two things: First we use the aggregation symbol in Rose to create a relationship between Container (Order) and Component (OrderLine). We draw from Order to OrderLine.

The Composition Symbol Then we select the aggregate relationship that we just drew and make it a composition by selecting containment of OrderLine and choosing by value.

A Note About Aggregation The aggregation symbol on its own is used to denote a weaker relationshipweaker than composition but stronger than association. It has limited use in modeling and has no definite meaning when translated to code.

Copyright © 1997 by Rational Software Corporation Inheritance Inheritance is a relationships between a base class and its derived classes There are two ways to find inheritance: Generalization Specialization Common attributes, operations, and/or relationships are shown at the highest applicable level in the hierarchy

Copyright © 1997 by Rational Software Corporation Inheritance Generalization The capability to create base classes that encapsulate structure and behaviour common to several classes.

Copyright © 1997 by Rational Software Corporation Inheritance Specialization The ability to create derived classes that represent refinements to the base classtypically structure and behaviour are added to the new derived class.

Base Classes and Derived Classes Derived classes must know who their base class is, and they depend on their base class. Base classes should know nothing about their derived classes.

Lets look at a Bill Payment Example

Inheritance in the Bill Payment Example A better way to handle the account types might be with inheritance. First, lets look at all the different types of accounts we might have:

Types of Accounts NoInterestChequing InterestChequing AnnualBonusSavings HighYieldSavings WithdrawAnyTimeSavings TaxFreeSavings BusinessAccount and so on…

What do these accounts look like? Each of the accounts shares at least some of the same attributes and operations. The operations may act differently (e.g. interest calculations will be different) but will have the same name.

Inheritance To use inheritance we create a base class which is a generalization of all account classes

Inheritance If the base class has no objects (is never instantiated) then we call it abstract and show it as follows: That means there will be no such thing as an account:Account objectonly specific account objects will be instantiated e.g. taxFreeSavings:TaxFreeSavings.

To Show Inheritance: This is the generalization symbol When you see the generalization symbol you know that all derived classes will carry the defined attributes and operations, so there is no need to show them.

Inheritance A child class is a special type of the more general parent class E.g. A video is a type of library item A part-time student is a type of student A reserve item screen is a type of library screen

Inheritance Each of the DERIVED or CHILD classes is inherited from the BASE or PARENT class. Each derived class is a specialization of the base class. The base class is a generalization of all of the derived classes

Inheritance Each class that is derived from the base class must implement the attributes and operations of the base class but can have its own version of each for example most of the calculateInterest operations will be different, but they will all be called calculateInterest. Any program that uses any derived account object will be able to use the calculateInterest operation on any objects derived from account. The derived classes might have their own ADDITIONAL operations and attributes.

Inheritance in the Bill Payment example We could show the parent class in our diagram but we would have to be sure that the relationships were true of every child class e.g. could we pay bills from our savings accounts? Otherwise we would show child classes.

Monopoly Example public abstract class Square { public String sqName; public Boolean loseTurn; public MSquare() { } public abstract void landOn(Player p, boolean passGo); public String getName() { return sqName; }

Monopoly Example from Larman public class RegularSquare extends Square { public RegularSquare() { sqName = "Regular"; loseTurn = false; // for later } public void landOn(Player p, boolean passGo) { //if pass go collect $200 else do nothing if (passGo) {p.setNetWorth(p.getNetWorth() ); } }

Monopoly Example public class IncomeTaxSquare extends Square { public IncomeTaxSquare() { sqName = "IncomeTax"; loseTurn = false; // for later } public void landOn(Player p, boolean passGo) { double amount; double deduct; double defaultAmt = ; amount = p.getNetWorth(); deduct = min(defaultAmt, amount *.1); p.setNetWorth(amount - deduct); //deduct the lesser of 100 and 10% of net worth if (passGo) {p.setNetWorth(p.getNetWorth() );} //collect $200 if go was passed //don't reset position }

Monopoly Example public class GoToJailSquare extends Square { public GoToJailSquare() { sqName = "GoToJail"; loseTurn = true; // for later } public void landOn(Player p, boolean passGo) { p.setNetWorth(p.getNetWorth() ); //fine of $200 p.setPosition(0); //go back to the start //do nothing if PassGo is true; do not collect $200 }

Monopoly Example In the java code you see that each child class only has to contain code for the operation that is not specified or is different from that specified in the parent. Child classes would also have to include code for any additional attributes and operations they carry.

Why Use Inheritance? Less duplication More reusability More standardization ****** Less change impact Classes are more focused Easy to add a child …and so on…