Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.

Slides:



Advertisements
Similar presentations
Inheritance Inheritance Reserved word protected Reserved word super
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 1 Principles of Programming and Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction: The Walls
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
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.
Object Oriented Analysis By: Don Villanueva CS 524 Software Engineering I Fall I 2007 – Sheldon X. Liang, Ph. D.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
An Object-Oriented Approach to Programming Logic and Design
Data Structures and Abstractions with Java, 4e Frank Carrano
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Systems Analysis and Design in a Changing World, 3rd Edition
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Data Structures Using C++1 Chapter 1 Software Engineering Principles and C++ Classes.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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,
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 2 Principles of Programming and Software Engineering.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
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.
11 Systems Analysis and Design in a Changing World, Fifth Edition.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Appendix A: UML Java Software Structures: Designing and Using Data.
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
Data Abstraction: The Walls
Object-Oriented Analysis and Design
Inheritance and Polymorphism
Encapsulation and Java Interface
Principles of Programming and Software Engineering
About the Presentations
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,
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slides by Steve Armstrong LeTourneau University Longview, TX
Software Design Lecture : 14.
Defining Classes and Methods
Information Hiding and Encapsulation Section 4.2
Presentation transcript:

Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

Object Oriented Programming Encapsulation, Inheritance Polymorphism © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Encapsulation Information hiding Enclose data and methods within a class Hide implementation details Programmer receives only enough information to be able to use the class © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Encapsulation FIGURE P-1 An automobile’s controls are visible to the driver, but its inner workings are hidden © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Abstraction Focus on what instead of how  What needs to be done?  For the moment ignore how it will be done. Divide class into two parts  Client interface  Implementation © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Abstraction FIGURE P-2 An interface provides well-regulated communication between a hidden implementation and a client © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Specifying Methods Preconditions  What must be true before method executes  Implies responsibility for client Postconditions  Statement of what is true after method executes Use assertions  In comments or with assert statement © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Java Interfaces Program component that declares a number of public methods  Should include comments to inform programmer  Any data fields here should be public, final, static © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Listing 2-1 Interface Measurable

© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Listing 2-2 Interface NameInterface

Implementing an Interface FIGURE P-3 The files for an interface, a class that implements the interface, and the client © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Implementing an Interface A way for programmer to guarantee a class has certain methods Several classes can implement the same interface (each in its own way) A class can implement more than one interface © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Interface as a Data Type You can use a Java interface as you would a data type Indicates variable can invoke certain set of methods and only those methods. An interface type is a reference type An interface can be used to derive another interface by using inheritance © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Interface vs. Abstract Class Purpose of interface similar to that of abstract class  But an interface is not a class Use an abstract class …  If you want to provide a method definition  Or declare a private data field that your classes will have in common A class can implement several interfaces but can extend only one abstract class. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Named Constants Within an Interface An interface can contain named constants,  Public data fields that you initialize and declare as final. Options:  Define the constants in an interface that the classes implement  Define your constants in a separate class instead of an interface © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Choosing Classes Consider a registration system for your school … Issues: Who, what will use the system? What can each actor do with the system? Which scenarios involve common goals? © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Choosing Classes FIGURE P-4 A use case diagram for a registration system © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Identifying Classes FIGURE P-5 A description of a use case for adding a course © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

CRC Cards FIGURE P-6 A class-responsibility-collaboration (CRC) card © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Unified Modeling Language FIGURE P-7 A class representation that can be a part of a class diagram © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Unified Modeling Language FIGURE P-8 UML notation for the interface Measurable © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Unified Modeling Language FIGURE P-9 A class diagram showing the base class Student and two subclasses © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The Unified Modeling Language FIGURE P-10 Part of a UML class diagram with associations © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

Reusing Classes Not all programs designed and written “from scratch” Actually, most software created by combining  Already existing components with  New components Saves time and money Reused components are already tested © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

End Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.