©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 4 - 1 Chapter 4 Defining Your Own Classes.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Information Hiding Chapter 5: Classes and Objects in Depth.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1 Animated.
Classes, Encapsulation, Methods and Constructors
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introduction to methods Chapter 5: Classes and Objects in Depth.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter Chapter 4 Defining Your Own Classes Part 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Develop Instantiable classes (continue). Exam 1 – section 02 Avg = 131 (87%). Max: 148.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 4 Defining Your Own Classes Part 1 Animated Version.
BCS 2143 Object Oriented Programming Defining Your Own Classes (Part 1)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Object- Oriented Programming (CS243) Dr Walid M. Aly lec4 1 Dr Walid M. Aly 1 Lecture 4 Object- Oriented Programming (CS243) Group home page:
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Classes and Objects. Object Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 4 Defining Your Own Classes Part 1 Animated Version.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Classes and Objects. How can one design a program?  Top-down structured design: uses algorithmic decomposition where each module denotes a major step.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Chapter 4 : Defining Your Own Classes Part 1 - Objectives After you have read and studied this chapter, you should be able to Define a class with multiple.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Defining Your Own Classes
Chapter 5: Classes and Objects in Depth
Chapter 5: Classes and Objects in Depth
Chapter 5: Classes and Objects in Depth
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object-Oriented Design Abstraction allows a programmer to concentrate on the important features of an object Interfaces define how classes interact Encapsulation makes objects self-contained Hiding information protects an objects data from meddling Generality allows a class to be used for several related tasks Extensibility allows the re-use of a class through inheritance

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Define Your Own Classes? Using just the standard classes will not meet all of our needs. We need to be able to define our own classes customized for our applications. Learning how to define our own classes is the first step toward mastering the skills necessary in building large programs.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Designing a Class What role does the class play in the program? –What data does it need? –What behavior is required? –What interface does it present to other classes? –What information does it keep to itself? –How do we want to create the object?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Example: Bicycle Class A class to be used in a program to register bicycles on campus Data – name of owner –what else? Methods –need to be able to give a value to owner name –need to be able to find out what the owner name is –Any others?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Class Diagram for Bicycle

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter High-Level Class Diagrams Librarian is a main class –main method creates Student and Library card objects LibraryCard has a Student Student has two Strings

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Template for Class Definition

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Data Members Data members (instance variables) are basically variables that are declared in the class body –Variables declared inside a method are local to that method Instance variables are used to store the information needed to describe a particular object Instance variables are usually private Class data is also declared in the body of the class –The reserved word static indicates class data –Constants are usually class data use the modifier final to declare a constant

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Syntax for Data Member Declaration ; private String ownerName ; Modifiers Data Type Name Declaration needs a name and a type –Usually use modifier private for data members –We'll see other modifiers later

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Instance Methods Instance methods define the behavior of an object –Instance methods use the object's data –Qualify instance methods with the name of an object Methods can be either public or private –The interface a class presents to the outside world is defined by its public methods Class methods are independent of any particular object –Qualify class methods with the name of the class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Method Declaration ( ){ } public void setOwnerName ( String name ) { ownerName = name; } Statements Modifier Return Type Method Name Parameter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Parameters Parameters describe the information needed by the method –Think of a parameter as a placeholder for the information that needs to be sent to the method when it is called. The parameter list consists of zero or more parameter declarations –parameters are separated by commas A parameter declaration consists of a type followed by a name

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Calling a method with Parameters When we call a method that has parameters, we need to provide a "value" for each parameter –An argument is a value we pass to a method. public class Account {... public void add(double amt) { balance = balance + amt; }... } public class Sample { public static void main(String[] arg) { Account acct = new Account();... acct.add(400);... }... } argument parameter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Calling Methods The number or arguments and the parameters must be the same Arguments and parameters are paired left to right The matched pair must be assignment- compatible (e.g. you cannot pass a double argument to a int parameter)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Creating Objects We use new to create an object Bicycle bike = new Bicycle(); Bicycle() is an example of a constructor –A constructor is a special method that is executed when a new instance of the class is created. –A constructor has no return type –A constructor can only be called with new Every class has at least one constructor –The default constructor has no parameters It is useful to be able to provide an object's data when it is created –Define a constructor with parameters to do this

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Constructor public ( ){ } public Bicycle( String name ) { ownerName = name; } Modifier Class Name Parameter

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Programs with Multiple classes BicycleRegistrationBicycle BicycleRegistration uses Bicycle –instances are created in the main method Once a class is defined, it can be used in more than one program SecondMainBicycleAccount

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Information Hiding and Visibility Modifiers The modifiers public and private designate the accessibility of data members and methods. If a class component (data member or method) is declared private, client classes cannot access it. If a class component is declared public, client classes can access it. Internal details of a class are declared private and hidden from the clients. This is information hiding.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Accessibility Example ClientService … Service obj = new Service(); obj.memberOne = 10; obj.memberTwo = 20; obj.doOne(); obj.doTwo(); … public class Service { public int memberOne; private int memberTwo; public void doOne() { … } private void doTwo() { … }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Guideline for Visibility Modifiers Guidelines in determining the visibility of data members and methods: –Declare the class and instance variables private. –Declare the class and instance methods private if they are used only by the other methods in the same class. –Declare the class constants public if you want to make their values directly readable by the client programs. If the class constants are used for internal purposes only, then declare them private.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter UML Notation for Visibility public – plus symbol (+) private – minus symbol (-)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Diagrams Once the Bicycle class is defined, we can create multiple instances. Bicycle bike1, bike2; bike1 = new Bicycle( ); bike1.setOwnerName("Adam Smith"); bike2 = new Bicycle( ); bike2.setOwnerName("Ben Jones");

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Local Variables Local variables are variables declared in the body of a method –They are used for storing intermediate results that don't need to be saved.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Local, Parameter & Data Member An identifier appearing inside a method can be a local variable, a parameter, or a data member. The rules are –If there’s a matching local variable declaration or a parameter, then the identifier refers to the local variable or the parameter. –Otherwise, if there’s a matching data member declaration, then the identifier refers to the data member. –Otherwise, it is an error because there’s no matching declaration.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sample Matching

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Calling Methods of the Same Class So far, we have been calling a method of another class (object). It is possible to call method of a class from another method of the same class. –in this case, we simply refer to a method without dot notation

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Passing Objects to a Method Method parameters can be either objects or primitive values When a primitive value is passed to a method, its value is copied into the parameter variable When we pass an object, we are actually passing the reference (location) of the object –the method has access to the original object, not a copy

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Passing a Student Object

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sharing an Object We pass the same Student object to card1 and card2 Since we are actually passing a reference to the same object, it results in the owner of two LibraryCard objects pointing to the same Student object

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object-Oriented Design Divide and Conquer is one approach to solving large problems –Break the problem up into smaller tasks –Keep sub-dividing until each task becomes small enough to handle In object-oriented design, identify the objects in the problem –Look for nouns in the problem description –For each object identify data needed to describe the object operations needed to allow the object to do its job