Inheritance vs Composition Picking the right tool.

Slides:



Advertisements
Similar presentations
Exercise 1 Consider the ER diagram below. Assume that an employee may work in up to two departments or may not be assigned to any department. Assume that.
Advertisements

Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
What is the major reason people work? Love of your job? To sharpen your soft skills? To meet new and exciting people? NO – to make money!!! Focus: How.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 Use Case Diagram Corrected to match newer conventions.
Designing A Payroll System Daniel POP, Ph.D. 2 General Description The system consists of a database of the company’s employees, and their associated.
Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
CS-2135 Object Oriented Programming
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,
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
 Love of your job? To sharpen your soft skills? To meet new and exciting people?  NO – to make money!!!  Focus: How to calculate different types of.
Chapter 9 Domain Models. Domain Model in UML Class Diagram Notation A “visual dictionary”
Lawrence ChungCS6359.0T1: Module 41 Module 4: Relationships.
The Software Development Life Cycle: An Overview Presented by Maxwell Drew and Dan Kaiser Southwest State University Computer Science Program.
Session 11 The Class Diagram: Aggregation and Generalization Written by Thomas A. Pender Published by Wiley Publishing, Inc. October 12, 2011 Presented.
1 Chapter 2 (Cont.) The BA’s Perspective on Object Orientation.
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.
NS 5.1.2: Consumer Arithmetic Students learn about calculating earnings for various time periods from different sources, including: Wage  Salary  Commission.
1 CSE 480: Database Systems Lecture 4: Enhanced Entity-Relationship Modeling Reference: Read Chapter 8.1 – 8.5 of the textbook.
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Domain Modeling Part2: Domain Class Diagram Chapter 4 pp part 2 1.
Class Diagrams In UML class diagrams are used to represent classes and relationships between classes. Each class is represented as a box divided into three.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
PROG Object Oriented Programming II With Java PROG Object Oriented Programming II With Java Class Relationships.
Generalization (Ch 26) a generalization is a relationship between a general thing (the superclass or parent class) and a more specific kind of thing (the.
Adv. Use cases Actor generalization – general and special actors Use case generalization – general and special use cases Include and extend relationships.
Object Oriented Software Development
Object Oriented Analysis: Associations. 2 Object Oriented Modeling BUAD/American University Class Relationships u Classes have relationships between each.
Enhanced Entity-Relationship (EER) Modeling. Slide 4- 2 Chapter Outline EER stands for Enhanced ER or Extended ER EER Model Concepts Includes all modeling.
Weak Entity Sets A weak entity is an entity that cannot exist in a database unless another type of entity also exists in that database. Weak entity meets.
Object Oriented Analysis and Design Class and Object Diagrams.
Classes & Inheritance Review
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Class Relationships A class defines a type of data Composition allows an object of another class to define an attribute of a class –Employee “has a”
12 OBJECT-ORIENTED DESIGN CHAPTER
January Ron McFadyen1 Domain Models Domain Model: a visual representation of conceptual classes or real-world objects in a domain of interest.
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Topics Inheritance introduction
CSC 480 Software Engineering PSP Project 2 August 27, 2004.
Class Diagrams Chapter 3. Classes and Objects Classes are the descriptions –definitions Objects are the things –instances.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Inheritance Doing More with Your Classes. Review of Classes Collection of instance variables and instance methods Treated like a data type Encapsulation.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
1 Chapter 13: Class Diagram Chapter 19 in Applying UML and Patterns Book.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
CS 350 – Software Design UML – The Unified Modeling Language – Chapter 2 The Unified Modeling Language is a visual language used to create models of programs.
EER Model – Chapter
Class diagram Description
Lecture 4 – Case Study TCP1201: 2017/2018.
Week 4 Object-Oriented Programming (1): Inheritance
ER MODEL Lecture 3.
Domain Class Diagram Chapter 4 Part 2 pp
MSIS 670 Object-Oriented Software Engineering
Data Modeling with Entity Relationship Diagrams (Cont.)
Code reuse through subtyping
Object Oriented Analysis and Design
Chapter 11: Class Diagram
Presentation transcript:

Inheritance vs Composition Picking the right tool

Inheritance – Technical mechanism for code reuse – Specific guarantee : Is-A Relationship Apple is-a Fruit Composition – Technical mechanism for code reuse – Indicate Has-A Relationship Car has-a Engine

Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance

Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance

Inherited Cylinder Have Circle, want to make Cylinder… – With inheritance

Is-A Gurantee DerivedCylinder can be treated like a Circle!!!

Composed Cylinder Have Circle, want to make Cylinder… – With Composition

Composed Cylinder Can't treat Cylinder as a Circle

Other Composition +'s Can have multiple members of same type – Triangle has three Points

Type Explosion Want to represent orders at a company – , Fax, Phone, InPerson orders – Credit, Check, Cash payment options

Type Explosion Option 1 : Inheritance Order class -Sub classes Order, FaxOrder, PhoneOrder… - Order subclasses: CreditOrder, CheckOrder, CashOrder… -FaxOrderSubClasses: FaxCreditOrder, FaxCheckOrder, FaxCashOrder… …

Type Explosion Option 2 : Composition OrderType class -subtypes: , Phone, … PaymentType class -subtypes: Credit, Check, … Order class -Members : OrderType, PaymentType

Type Explosion Option 3 : Multiple Inheritance OrderType class -subtypes: , Phone, … PaymentType class -subtypes: Credit, Check, … CreditOrder – multiple inheritance from , Credit CheckOrder – multiple inheritance from , Check …

Google says Composition is often more appropriate than inheritance. When using inheritance, make it public… Try to restrict use of inheritance to the "is-a" case: Bar subclasses Foo if it can reasonably be said that Bar "is a kind of" Foo.

Designing with Inheritance 1.Identify existing Is-A relationships – Use Inheritance to eliminate duplication 2.Identify remaining duplicate information 3.Look to introduce new parent classes – Where appropriate

Inheritance Design Sample Want to represent following worker types: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need name, title, salary, amount of sales, and method to calculate the amount for paycheck

1) Existing Is-A relationship: – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need name, title, salary, amount of sales, and method to calculate the amount for paycheck

1B) Inherit & Eliminate – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck

2) Look for other duplication – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: salaried, but gets bonus of 5% of sales Need amount of sales, and method to calculate the amount for paycheck

3) "Discover" common parent class – Employee Need name, title, method to calculate amount for paycheck – Hourly Need name, title, hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need name, title, salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck

3B) Inherit and eliminate – Employee Need name, title, method to calculate amount for paycheck – Hourly Need hours worked, hourly pay, method to calculate amount for paycheck – Salaried Need salary, method to calculate amount for paycheck – Salesperson: Need amount of sales, and method to calculate the amount for paycheck

Quick Challenge Sketch a quick UML diagram for classes below – Don't worry about behaviors, just state – Add classes where appropriate DVD Title, Release year, Director, Price CD Title, Release year, Genre, Price Book Title, Isbn, Price EBook Title, Isbn, Format (PDF/Kindle/etc…), Price