Presentation is loading. Please wait.

Presentation is loading. Please wait.

DOMAIN MODEL—PART 4B: SPECIAL ASSOCIATIONS - INHERITANCE BTS430 Systems Analysis and Design using UML.

Similar presentations


Presentation on theme: "DOMAIN MODEL—PART 4B: SPECIAL ASSOCIATIONS - INHERITANCE BTS430 Systems Analysis and Design using UML."— Presentation transcript:

1 DOMAIN MODEL—PART 4B: SPECIAL ASSOCIATIONS - INHERITANCE BTS430 Systems Analysis and Design using UML

2 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

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

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

5 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.

6 Let’s look at a Bill Payment Example

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

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

9 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.

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

11 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 object—only specific account objects will be instantiated e.g. taxFreeSavings:TaxFreeSavings.

12 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.

13 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”

14 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

15 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.

16 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.

17 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; }

18 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() + 200.00); } }

19 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 = 100.00; 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() + 200.00);} //collect $200 if go was passed //don't reset position }

20 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() - 200.00); //fine of $200 p.setPosition(0); //go back to the start //do nothing if PassGo is true; do not collect $200 }

21 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.

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


Download ppt "DOMAIN MODEL—PART 4B: SPECIAL ASSOCIATIONS - INHERITANCE BTS430 Systems Analysis and Design using UML."

Similar presentations


Ads by Google