Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.

Slides:



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

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Systems Analysis and Design 8th Edition
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Object Oriented Programming & Software Engineering.
UML – Class Diagrams.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
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.
1 CS1001 Lecture Overview Object Oriented Design Object Oriented Design.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
OO Analysis and Design CMPS OOA/OOD Cursory explanation of OOP emphasizes ▫ Syntax  classes, inheritance, message passing, virtual, static Most.
Systems Analysis and Design in a Changing World, Fifth Edition
Writing Classes (Chapter 4)
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
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
Systems Analysis and Design 8 th Edition Chapter 6 Object Modeling.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
ITEC324 Principle of CS III Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
CSE 219 Computer Science III UML. UML Diagrams UML - Unified Modeling Language UML diagrams are used to design object-oriented software systems –represent.
Coming up: Inheritance
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
UML Course Instructor: Rizwana Noor. Overview  Modeling  What is UML?  Why UML?  UML Diagrams  Use Case  Components  Relationships  Notations.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSCI 383 Object-Oriented Programming & Design Lecture 7 Martin van Bommel.
Object Oriented Programming and Data Abstraction Earl Huff Rowan University.
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.
Chapter 2 (Horstmann’s Book) – Part 1 The Object-Oriented Design Process Hwajung Lee.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
11 Systems Analysis and Design in a Changing World, Fifth Edition.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
The Movement To Objects
Object-Oriented Analysis and Design
Inheritance and Polymorphism
Encapsulation and Java Interface
Chapter 11 Object-Oriented Design
Data Abstraction: The Walls
About the Presentations
Creating and Using Classes
Object Based Programming
Object Oriented Analysis and Design
Slides by Steve Armstrong LeTourneau University Longview, TX
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
ITEC324 Principle of CS III
ITEC324 Principle of CS III
Presentation transcript:

Designing Classes Chapter 3

2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface as a Data Type Type Casts Within an Interface Implementation Extending an Interface Named Constants Within an Interface Interfaces Versus Abstract Classes Choosing Classes Identifying Classes CRC Cards Reusing Classes

3 Encapsulation 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

4 Encapsulation Fig. 3-1 An automobile's controls are visible to the driver, but its inner workings are hidden.

5 Abstraction 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

6 Abstraction Fig. 3-2 An interface provides well-regulated communication between a hidden implementation and a client.

7 Specifying Methods Specify what each method does Precondition Defines responsibility of client code Postcondition Specifies what will happen if the preconditions are met Assertions can be written as comments to identify design logic // Assertion: intVal >= 0

8 Java Interface 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 public interface someClass { public int someMethod(); }

9 Java Interface Example public interface NameInterface {/** Task: Sets the first and last names. firstName a string that is the desired first name lastName a string that is the desired last name */ public void setName(String firstName, String lastName); /** Task: Gets the full name. a string containing the first and last names */ public String getName(); public void setFirst(String firstName); public String getFirst(); public void setLast(String lastName); public String getLast(); public void giveLastNameTo(NameInterface child); public String toString(); } // end NameInterface

10 Implementing an Interface A class that implements an interface must state so at start of definition public class myClass implements someInterface 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 An interface can be used as a data type public void someMethod (someInterface x)

11 Implementing an Interface Fig. 3-3 The files for an interface, a class that implements the interface, and the client.

12 Type Casts Within an Interface Implementation In any class that implements an interface A parameter may be of type Object A type cast would probably be needed within that method public class Pet implements Comparable {private String name; private int age; // in years private double weight; // in pounds /** Task: Compares the weight of two pets. */ public int compareTo(Object other) {Pet otherPet = (Pet)other; return weight - otherPet.weight; } // end compareTo } // end Pet

13 Extending an Interface 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

14 Named Constants Within an Interface 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

15 Interfaces Versus Abstract Classes 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

16 Choosing Classes 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

17 Choosing Classes Fig. 3-4 A use case diagram for a registration system

18 Identifying Classes Describe the system Identify nouns and verbs Nouns suggest classes Students Transactions Customers Verbs suggest appropriate methods Print an object Post a transaction Bill the customer

19 Identifying Classes Fig. 3-5 A description of a use case for adding a course

20 CRC Cards 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"

21 CRC Cards Fig. 3-6 A class-responsibility-collaboration card

22 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.

23 Unified Modeling Language Fig. 3-8 UML notation for a base class Student and two derived classes

24 Unified Modeling Language Fig. 3-9 Part of a UML class diagram with associations.

25 Reusing Classes Much software combines: 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