Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 7 Arrays and Vectors Introducing Arrays Introducing Arrays Declaring Arrays, Creating Arrays, and Initializing Arrays Declaring Arrays, Creating.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
Inheritance and Access Control CS 162 (Summer 2009)
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Coming up: Inheritance
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CS 112 Programming 2 Lecture 10 Abstract Classes & Interfaces (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
Chapter 15 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 11 Object-Oriented Design
Chapter 10 Object-Oriented Modeling
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Thinking in Objects
Chapter 12 Abstract Classes and Interfaces
Chapter 9 Thinking in Objects
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 10 Thinking in Objects
Presentation transcript:

Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines F Wrapper Classes F Generic Sort Class F Generic Matrix Class F Generic Linked List Class

Software Development Process

Relationships among Classes F Association F Aggregation F Inheritance

Association Association represents a general binary relationship that describes an activity between two classes.

Representing Association in Classes An association is usually represented as a data field in the class. or

Aggregation Aggregation is a special form of association, which represents an ownership relationship between two classes. Aggregation models the relationship like has-a, part-of, owns, and employed-by.

Representing Composition in Classes A composing class is usually represented as a data field in the composed class. public class Publisher { /**Constructors*/ /**Methods*/ } public class Consultant { private Publisher[] p; /**Constructors*/ /**Date fields*/ /**Methods*/ } public class Magzine { private Publisher p; /**Constructors*/ /**Date fields*/ /**Methods*/ }

Inheritance Inheritance models the is-a relationship between two classes.

Inheritance public class Student extends Person { /**Constructors*/ /**Methods*/ } public class Faculty extends Person { /**Constructors*/ /**Methods*/ }

Weak Inheritance Relationship A weak is-a relationship can be represented using interfaces. For example, the weak is-a relationship “students are comparable based on their grades” can be represented by implementing the Comparable interface, as follows:

Weak Inheritance Relationship, cont. public class Student extends Person implements Comparable { /** Data fields */ /** Constructors */ /** Methods */ /** Implement the compareTo method */ public int compareTo(Object object) { //... }

Class Development 1. Identify classes for the system. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 4. Create classes.

Example 9.1 Borrowing Mortgages Name BorrowerPersonMortgageAddress

Example 9.1 Borrowing Mortgages, cont. The following is a test program that uses the classes Name, Person, Address, Borrower, and Mortgage. BorrowMortgage Run

Example 9.2 The Rational Class RationalRunTestRationalClass

Class Design Guidelines F Designing a Single Class. F Using Modifiers public, protected, private and static F Using Inheritance or Composition F Using Interfaces or Abstract Classes

Designing a Single Class F A class should describe a single entity or a set of similar operations. A single entity with too many responsibilities can be broken into several classes to separate responsibilities. The String class, StringBuffer class, and StringTokenizer class all deal with strings, for example, but have different responsibilities.

Designing a Single Class, cont. F Classes are usually designed for use by many different customers. To make a class useful in a wide range of applications, the class should provide a variety of ways for customization through properties and methods.

Designing a Single Class, cont. F Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. Therefore, you should design a class that imposes no restrictions on what or when the user can do with it, design the properties to ensure that the user can set properties in any order, with any combination of values, and design methods to function independently of their order of occurrence.

Designing a Single Class, cont. F Provide a public default constructor and override the equals method and the toString method defined in the Object class whenever possible.

Designing a Single Class, cont. F Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. Always place the data declaration before the constructor, and place constructors before methods. Always provide a constructor and initialize variables to avoid programming errors.

Using Modifiers public, protected, private and static F Each class can present two contracts – one for the users of the class and one for the extenders of the class. Make the fields private and accessor methods public if they are intended for the users of the class. Make the fields or method protected if they are intended for extenders of the class. The contract for the extenders encompasses the contract for the users. The extended class may increase the visibility of an instance method from protected to public, or change its implementation, but you should never change the implementation in a way that violates that contract.

Using Modifiers public, protected, private and static, cont. F A class should use the private modifier to hide its data from direct access by clients. You can use get methods and set methods to provide users with access to the private data, but only to private data you want the user to see or to modify. A class should also hide methods not intended for client use. The gcd method in the Rational class in Example 9.2, “The Rational Class,” is private, for example, because it is only for internal use within the class.

Using Modifiers public, protected, private and static, cont. F A property that is shared by all the instances of the class should be declared as a static property.

Using Inheritance or Composition In general, the difference between inheritance and composition is the difference between the is-a relationship and the has-a relationship. For example, an apple is fruit; thus, you would use inheritance to model the relationship between the classes Apple and Fruit. A person has a name; thus, you would use composition to model the relationship between the classes Person and Name.

Using Inheritance or Composition, cont. Sometimes, the choice between inheritance and composition is not obvious. For example, you have used inheritance to model the relationship between the classes Circle and Cylinder. One could argue that a cylinder consists of circles; thus, you might use composition to define the Cylinder class as follows:

Using Inheritance or Composition, cont. public class Cylinder { private Circle circle; /** Constructors */ /** Methods */ }

Using Inheritance or Composition, cont. Both designs are fine. Which one is preferred? If polymorphism is desirable, you need to use the inheritance design. If you don’t care about polymorphism, the composition design gives more flexibility because the classes are less dependent using composition than using inheritance.

Using Interfaces or Abstract Classes Both interfaces and abstract classes can be used to generalize common features. How do you decide whether to use an interface or a class? In general, a strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes.

Using Interfaces or Abstract Classes, cont. For example, since an orange is a fruit, their relationship should be modeled using class inheritance. A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can be modeled using interfaces. For example, all strings are comparable, so the String class implements the Comparable interface. A circle or a rectangle is a geometric object, for example, so Circle can be designed as a subclass of GeometricObject. Circles are different and comparable based on their radius, for example, so Circle can implement the Comparable interface.

Using Interfaces or Abstract Classes, cont. Interfaces are more flexible than abstract classes, because a subclass can extend only one superclass, but implement any number of interfaces. However, interfaces cannot contain concrete methods. You can combine the virtues of interfaces and abstract classes by creating an interface with a companion abstract class that implements the interface. So you can use the interface or its companion class whichever is more convenient.

Wrapper Classes F Boolean F Character F Short F Byte F Integer F Long F Float F Double

The Integer Class and The Double Class F Constructors  Class Constants MAX_VALUE, MIN_VALUE F Conversion Methods

Example 9.3 Sorting an Array of Objects Objective: The example presents a generic method for sorting an array of objects. The objects are instances of the Comparable interface and they are compared using the compareTo method. GenericSort Run

Example 9.4 Designing Generic Classes F Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. GenericMatrix

Example 9.4, cont.

F Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. TestIntegerMatrix Run TestRationalMatrix Run Rational Matrix IntegerMatrix

Linked List Arrays are useful for storing and managing a set of elements of the same type. However, since the length of an array is fixed once the array is created, you need to know the length of the array before you create it. A linked list can grow or shrink dynamically as needed. A linked list consists of nodes, as shown in Figure Each node contains an element and each node is linked to its next neighbor. Thus, a node can be defined as a class as follows:

Linked List Structure public class Node { Object element; Node next; public Node(Object o) { element = o; }

Create a Linked List with three Nodes Node n1 = new Node(new String(“Welcome to Java!”)); Node n2 = new Node(new JButton(“OK”)); Node n3 = new Node(new Rational(1, 2)); n1.next = n2; n2.next = n3; Node first = n1; Node last = n3;

Generic Linked List Add a new node

Generic Linked List Remove a new node

Example 9.5 Using Linked List F This example creates a linked list using GenericLinkedList. It then uses the add method to add strings to the list and uses the remove method to remove strings from the list. GenericLinkedList Run TestLinkedList

Generic Linked List GenericLinkedList list = new GenericLinkedList(); list.addLast(new String(“Welcome to Java”)); list.addLast(new JButton(“OK”)); list.addLast(new Rational(1, 2));