Aggregation and Inheritance

Slides:



Advertisements
Similar presentations
Lecture 10.2 Different Types of Loops, including for and do.
Advertisements

CIS 234: Inheritance Dr. Ralph D. Westfall April, 2010.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
What is inheritance? It is the ability to create a new class from an existing class.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
© 2011 Pearson Education 1 Chapter 13 (Online): Object-Oriented Databases Modern Database Management 10 th Edition, International Edition Jeffrey A. Hoffer,
Domain Modeling Part2: Domain Class Diagram Chapter 4 pp part 2 1.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
Lecture 8.2 Aggregation and Inheritance. © 2006 Pearson Addison-Wesley. All rights reserved Designing with Aggregation and Inheritance Software.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Appendix A: UML Java Software Structures: Designing and Using Data.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Advanced Java Topics Chapter 9
OBJECT ORIENTED CONCEPT
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Interface, Subclass, and Abstract Class Review
public class Doubler extends Base {
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
An Introduction to Inheritance
UML UML Data Modeling.
Chapter 17 Linked Lists.
Chapter 19 Binary Search Trees.
Chapter 4 Inheritance.
Chapter 14 Graphs and Paths.
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Advanced Java Topics Chapter 9
Pre-AP® Computer Science Quiz Key
Pre-AP® Computer Science Quiz
CMSC 202 Inheritance.
Class Reuse with Inheritance
Chapter 10 Datapath Subsystems.
Understand and Use Object Oriented Methods
Chapter 20 Hash Tables.
Copyright © 2011 Pearson Education, Inc
Class Hierarchies and Type Conformance
Inheritance: Introduction
Chapter 5 Algorithm Analysis.
PreAP Computer Science Quiz
The Facts to Be Explained
Alternate Proofs of Selected HO Theorems
Introduction: Some Representative Problems
Circuit Characterization and Performance Estimation
The Classical Model with Many Goods
Chapter 6 Dynamic Programming.
Extending Classes Through Inheritance
Chapter 2 Reference Types.
Chapter 4 Greedy Algorithms.
Copyright © 2011 Pearson Education, Inc
Chapter 7 Inheritance.
Presentation transcript:

Aggregation and Inheritance Lecture 8.2 Aggregation and Inheritance

Designing with Aggregation and Inheritance Software Design Step 1 Identify the objects. Step 2 Find/design a class for each object. One of the Challenges deciding when to use aggregation and when to use inheritance SupplierClass SuperClass ClientClass SubClass © 2006 Pearson Addison-Wesley. All rights reserved

What is AGGREGATION? public class SomeAggregate { } SomeAggregate private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } SomeAggregate ClassA ClassB ClassC © 2006 Pearson Addison-Wesley. All rights reserved

Class Diagram Object Diagram public class SomeAggregate { } private ClassA partA; private ClassB partB; private ClassC partC1; private ClassC partC2; // methods omitted } Class Diagram Object Diagram SomeAggregate ClassA ClassB ClassC :SomeAggregate partA : ClassA partB : ClassB partC1 : ClassC partC2 : ClassC © 2006 Pearson Addison-Wesley. All rights reserved

When to aggregate? One class contains_a other. contains_a An equilateral triangle contains_a vertex. A dog has_a tail. A sandal includes_a sole. A company’s work roster contains_an employee. Cherry pie contains_a cherry pit. contains_a © 2006 Pearson Addison-Wesley. All rights reserved

When to inherit? One class is_a another class is_a An equilateral triangle is_a triangle. A dog is_an animal. A sandal is_a special kind of footware. An hourly employee is_an employee. Cherry pie is_a dessert. is_a Generalizations can be discovered from common attributes. © 2006 Pearson Addison-Wesley. All rights reserved

Create the best relationship from... record button camcorder record button tape recorder lens on/off switch viewfinder tape recorder on/off switch lens camcorder viewfinder © 2006 Pearson Addison-Wesley. All rights reserved

A Dilemma (which is it?) A Camcorder is_a tape recorder. or A Camcorder contains_a tape recorder. Either aggregation or inheritance will work in this case (see next slide). © 2006 Pearson Addison-Wesley. All rights reserved

public class Camcorder { private TapeRecorder rec; ••• //somewhere in the code rec.startRecording(); } public class Camcorder extends TapeRecorder { ••• //somewhere in the code startRecording(); } © 2006 Pearson Addison-Wesley. All rights reserved

java.awt.Container Container is an awt class well suited to inheritance. A Container object is a transparent rectangle. It is useful for grouping other graphical objects into a single unit. An Container subclass can reuse...  add and remove  setBounds and setLocation  getX, getY, getWidth, getHeight, getBackground  repaint A Container subclass may want to override...  setSize and setBounds  setBackground © 2006 Pearson Addison-Wesley. All rights reserved