Object Oriented Design and UML

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Interfaces A Java interface is a collection
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
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.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Introduction to UML Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Programming in Java (COP 2250) Lecture 14 & 15 Chengyong Yang Fall, 2005.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Static class members.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Object-Oriented Design. Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Lecture 12 Inheritance.
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Object Oriented Programming
Object Oriented Programming
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Chapter 8 Inheritance.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
2009 Test Key.
Object-Oriented Design Part 2
Presentation transcript:

Object Oriented Design and UML Class Relationships Dependency Aggregation Interfaces Inheritance (not today) Reading for this Lecture: L&L 7.4 – 7.5

Class Relationships Classes in a software system can have various types of relationships to each other Three of the most common relationships: Dependency: A uses B Aggregation: A has-a B (as in B is an integral part of A) Interface: A is B (adjective) or A is-a B (noun) Inheritance: A is-a B We cover the first three now We cover inheritance later

Dependency A dependency exists when one class relies on another in some way, usually by invoking the methods of the other We've seen dependencies in previous examples and in Projects 1 and 2 We don't want numerous or complex dependencies among classes Nor do we want complex classes that don't depend on others A good design strikes the right balance

Project 2 UML Diagram Die DiceStats - MAX : int - MIN_ROLL : int - faceValue : int + Die() + roll() : int + setFaceValue(value : int) : void + getFaceValue( ) : int + toString() : String - MIN_ROLL : int - MAX_ROLL : int - MAX_LENGTH : int + main(args : String [ ]) : void Histogram values : int [ ] minIndex : int maxIndex : int maxLength : int + Histogram (values : int [ ], minIndex : int, maxIndex : int, maxLength : int) + drawHor(void) : void + drawVer(void) : void

Aggregation An aggregate is an object that is made up of other objects Therefore aggregation is a has-a relationship A Student has (a) Address object(s) (pg. 321) In code, an aggregate object contains references to its component objects as instance data - Student has 2 Address refs: private Address homeAddress, schoolAddress; The aggregate object itself is defined in part by the objects that make it up: these Addresses are important state for Student

Aggregation In the text’s example, a Student object has two Address objects, so Student is an “aggregate” See Student.java, Address.java (page 318-319) An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end, here the Student end vs. Note there is no arrow point on this connector, but many UML authors do use one

Dependency/Aggregation in UML StudentBody Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + main (args : String[]) : void + toString() : String Aggregation shown with this symbol Dependency shown with this symbol Address - streetAddress : String - city : String - state : String - zipCode : long + toString() : String

Aggregation There are two ways to include the component objects in an object that is an aggregation For a small constant number of components, can use parameters in the constructor public Car(CarPart chassis, CarPart engine) { ... } For a large or indefinite number of components, define an add method to add them one at a time: adding students to a Section: public void add(Student aStudent)

Interfaces A Java interface is a collection of constants and method headers with a name that looks like a class name, i.e. the first letter is capitalized An interface is used to identify a set of methods that a class will implement The class can have other methods too Example later: class Lion implements interface Predator Methods Lion needs to implement: chasePrey, eatPrey, the predator-related actions Lion can also have a purr() method

interface is a reserved word Interfaces interface is a reserved word None of the methods in an interface are given a definition {body} public interface Doable { // Doable constants public static final boolean DONE = true; public static final boolean NOT_DONE = false; // Doable required methods (signatures only) public void doThis(); public int doThat(); } A semicolon immediately follows each method header

Interfaces, without “public”, “static” or “final” except on whole thing public interface Doable { // Doable constants: // automatically public static final boolean DONE = true; boolean NOT_DONE = false; // Doable required methods // automatically public void doThis(); int doThat(); }

Interfaces An interface name can be either an adjective (like …able) or a noun (like a class name) An interface cannot be instantiated by itself A class implements an interface by: using the Java reserved word implements providing an implementation for each abstract method that is defined in the interface Classes that implement an interface can also implement their own methods and they usually do

given a definition, and here Interfaces public class Worker implements Doable { public void doThis () // whatever } public int doThat () // etc. Doable is an adjective implements is a reserved word Each method listed in Doable must be given a definition, and here “public” IS NEEDED!!

Interfaces In UML Interface box looks like a class box with stereotype <<interface>> <<interface>> Doable + DONE : boolean + NOT_DONE : boolean + doThis( ) : void + doThat( ) : int A “Generalization” arrow is used for “implements” (and also for “extends” later) Worker Each method listed in Doable becomes a method of Worker + doThis( ) : void + doThat( ) : int + doNothing ( ) : void + doSomething ( ) : void Worker can have other methods of its own

Interfaces A class can implement multiple interfaces All interface names are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements Interface1, Interface2, ... { // all methods of all interfaces }

Example from Wikipedia Idea: a Predator must be able to chase and eat prey public interface Predator { boolean chasePrey(Prey p); void eatPrey(Prey p); } public class Lion implements Predator { public boolean chasePrey(Prey p) { // programming to chase prey p (spec. for a lion) } public void eatPrey (Prey p) { // programming to eat prey p (spec. for a lion) } // other Lion methods public void purr();

Predator Interface In UML Interface box looks like a class box with stereotype <<interface>> <<interface>> Predator + chasePrey(Prey p) : void + eatPrey(Prey p) : void A “Generalization” arrow is used for “implements” (and also for “extends” later) Lion Each method listed in Predator becomes a method of Lion + chasePrey(Prey p) : void + eatPrey(Prey p) : void + purr( ) : void Lion can have other methods of its own

Example from Wikipedia Here Prey can itself be an interface, expressing Prey actions, say flee(Predator p) And a class can implement multiple interfaces, public class Frog implements Predator, Prey { ... } This class will need to have methods chasePrey, eatPrey, and flee, and can have more methods of its own

Interface Types You can go from Lion to Predator type: Now have useful Predator object ref You can pass an object of Lion class to a method as an object of Predator type. We say a Lion IS-A Predator, a Frog IS-A Prey, and also a Frog IS-A Predator Lion leo = new Lion(…); // Lion object Predator p = leo; // Predator ref to Lion Frog freddie = new Frog(…); freddie.flee(leo); leo.chasePrey(freddie); //also works

InterfaceTypes When you are using an object via an interface ref, you can only call the interface methods with it Lion-specific methods, such as purr(), are not accessible when a Lion object is being used as a Predator object because they are not defined in the interface Lion leo = new Lion(…); // Lion object Predator p = leo; // Predator ref to Lion leo.purr(); // works fine: Lions can purr() p.purr(); // doesn’t work!! Predators can’t purr() ((Lion)p).purr(); // cast to Lion—works (the object is really a Lion, after all)

Library Interfaces The Java standard class library contains many interface definitions that allow other classes to treat your new class as if it were that interface Note: Comparable is an adjective in this case The Comparable interface contains one abstract method called compareTo, which can compare an object with another object of the same type We discussed the compareTo method of the String class previously The String class implements Comparable, giving us the ability to put strings in lexicographic order

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type by providing a compareTo method The value returned from compareTo should be negative if obj1 is less than obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When you design a class that implements the Comparable interface, it should follow this intent if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is ” + “less than obj2");

The Comparable Interface It's up to you as the programmer to determine what makes one object less than another For example, you may define the compareTo method of an Lion class to order by weight or tooth-length or any other useful way The implementation of the method can be as straightforward or as complex as needed for the situation Then we can sort an array of Lions, for example