Slides by Steve Armstrong LeTourneau University Longview, TX

Slides:



Advertisements
Similar presentations
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 1 Principles of Programming and Software Engineering.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 10 Classes Continued
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
An Introduction to Sorting Chapter 11 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Dictionaries Chapter 17 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
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.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Systems Analysis and Design in a Changing World, 3rd Edition
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Principles of Programming & Software Engineering
More Sophisticated Behavior
Unified Modeling Language (UML)
Object-Oriented Analysis and Design
Encapsulation and Java Interface
Data Abstraction: The Walls
Chapter 1 Data Abstraction: The Walls
Principles of Programming and Software Engineering
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Classes and Objects 2nd Lecture
Creating and Using Classes
Object Based Programming
Lecture 22 Inheritance Richard Gesick.
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 20 Object-Oriented Analysis and Design
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Iterators (partial) Chapter 8 Slides by Nadia Al-Ghreimil
Defining Classes and Methods
Stack Implementations
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Defining Classes and Methods
Chapter 22 Object-Oriented Systems Analysis and Design and UML
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

Slides by Steve Armstrong LeTourneau University Longview, TX Designing Classes Chapter 3 Slides by Steve Armstrong LeTourneau University Longview, TX ã 2007, Prentice Hall

Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface as a Data Type Generic Types Within an Interface Type Casts Within an Interface Implementation Extending an Interface Interfaces Versus Abstract Classes Named Constants Within an Interface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Chapter Contents Choosing Classes Reusing Classes Identifying Classes CRC Cards The Unified Modeling Language Reusing Classes Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Encapsulation 1 Hides the fine detail of the inner workings of the class The implementation is hidden Often called "information hiding" Part of the class is visible The necessary controls for the class are left visible The class interface is made visible The programmer is given only enough information to use the class Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Encapsulation Fig. 3-1 An automobile's controls are visible to the driver, but its inner workings are hidden. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Abstraction 3 A process that has the designer ask what instead of why What is it you want to do with the data What will be done to the data The designer does not consider how the class's methods will accomplish their goals The client interface is the what The implementation is the how Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Abstraction Fig. 3-2 An interface provides well-regulated communication between a hidden implementation and a client. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifying Methods 5 Specify what each method does Precondition Defines responsibility of client code Postcondition Specifies what will happen if the preconditions are met Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifying Methods Responsibility Where to place responsibility Precondition implies responsibility for guarantee of meeting certain conditions Where to place responsibility Client? or … Method? Best to comment this clearly before method's header Also good to have method check during debugging Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Specifying Methods 9 Assertion is statement of truth about aspect of a program's logic Like a boolean statement that should be true at a certain point Assertions can be written as comments to identify design logic // Assertion: intVal >= 0 Assert Statement assert someVal < 0; If false, program execution terminates Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Java Interface 11 A program component that contains Public constants Signatures for public methods Comments that describe them Begins like a class definition Use the word interface instead of class Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Writing an Interface 12 Consider geometric figures which have both a perimeter and an area We want classes of such objects to have such methods And we want standardization signatures View example of the interface Measurable Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Java Interface Example Recall class Name from previous chapter Consider an interface for the class Name View example listing Note Comments of method purpose, parameters, pre- and post-conditions Any data fields should be public, final, and static Interface methods cannot be final Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Implementing an Interface 15 A class that implements an interface must state so at start of definition with implements clause The class must implement every method declared in the interface Multiple classes can implement the same interface A class can implement more than one interface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Implementing an Interface Fig. 3-3 The files for an interface, a class that implements the interface, and the client. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

An Interface as a Data Type 17 An interface can be used as a data type or … Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Generic Types Within an Interface 19 Recall class OrderedPair from Chapter 2 Note the setPair method Consider an interface Pairable that declares this method A class that implements this interface could begin with the statement Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

The Interface Comparable 20 Method compareTo compares two objects, say x and y Returns signed integer Returns negative if x < y Returns zero if x == y Returns positive if x > y Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

The Interface Comparable Consider a class Circle Methods equals, compareTo Methods from Measurable View source code Note Implements Measurable interface Shown with two alternative versions of compareTo Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Extending an Interface 25 Use inheritance to derive an interface from another When an interface extends another It has all the methods of the inherited interface Also include some new methods Also possible to combine several interfaces into a new interface Not possible with classes Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Interfaces Versus Abstract Classes 27 Purpose of interface similar to purpose of abstract class But … an interface is not a base class It is not a class of any kind Use an abstract base class when You need a method or private data field that classes will have in common Otherwise use an interface Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Named Constants Within an Interface 30 An interface can contain named constants Public data fields initialized and declared as final Consider an interface with a collection of named constants Then derive variety of interfaces that can make use of these constants Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Choosing Classes 33 Look at a prospective system from a functional point of view Ask What or who will use the system What can each actor do with the system Which scenarios involve common goals Use a case diagram Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Fig. 3-4 A use case diagram for a registration system Choosing Classes Fig. 3-4 A use case diagram for a registration system Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Identifying Classes 34 Describe the system Nouns suggest classes Identify nouns and verbs Nouns suggest classes Students Transactions Customers Verbs suggest appropriate methods Print an object Post a transaction Bill the customer Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Fig. 3-5 A description of a use case for adding a course Identifying Classes Fig. 3-5 A description of a use case for adding a course Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

CRC Cards 35 Index cards – each card represents one class Write a descriptive name for class at top List the class's responsibilities The methods Indicate interactions The collaborations These are CRC cards "Class-Responsibility-Collaboration" Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Fig. 3-6 A class-responsibility-collaboration card CRC Cards Fig. 3-6 A class-responsibility-collaboration card Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Unified Modeling Language Used to illustrate a system's classes and relationships Provides a class diagram Class name Attributes Operations Fig. 3-7 A class representation that can be part of a class diagram. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Unified Modeling Language Fig. 3-8 UML notation for a base class Student and two derived classes Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Unified Modeling Language Fig. 3-9 A class diagram showing the base class Student and two derived classes Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Unified Modeling Language Fig. 3-10 Part of a UML class diagram with associations. Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

Reusing Classes Much software combines: When designing new classes Existing components New components When designing new classes Plan for reusability in the future Make objects as general as possible Avoid dependencies that restrict later use by another programmer Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X