JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

Slides:



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

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Fields, Constructors, Methods
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
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.
JAVA Quality. Objectives Understand how to verify the correctness of a program’s behavior. Be able to write unit tests for program behavior Understand.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Enhancing classes Visibility modifiers and encapsulation revisited
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Ranga Rodrigo. Class is central to object oriented programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
© Calvin College, What is object-oriented programming? My guess is that object-oriented programming will be in the 1980’s what structured programming.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
1 More About Classes – Instance Methods Chap.6 Study Sections 6.1 – 6.4 Representing More Complex Objects.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Classes and Objects: Encapsulation
Chapter 4: Writing Classes
Classes and Objects 2nd Lecture
Classes and Objects: Encapsulation
Classes and Objects Encapsulation
Encapsulation & Visibility Modifiers
Corresponds with Chapter 7
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
CIS 199 Final Review.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Dr. R Z Khan Handout-3 Classes
CMSC 202 Encapsulation Version 9/10.
Building Java Programs
Presentation transcript:

JAVA Classes

Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class Constructors Accessors Mutators Other Be able to distinguish between static and instance variables and methods Be able to create and call static methods Be able to create and call instance methods

3 Classes Classes encapsulate object types. In object-centered design we Reuse old classes where possible Build new classes when necessary Each new class that we design and build should have a coherent set of: Knowledge Responsibilities

Ice Cream Orders Suppose you were writing a program for use at an ice cream shop. The program needs to help track what orders are given and when they have been fulfilled. What are the necessary… Data? Number of scoops Flavor Order fulfilled (true or false) Operations? Create new order Change fulfilled status Compute cost

5 Classes and Objects Classes describe sets of similar objects by specifying their: Attributes Behaviors Each object has its own copies of the attribute values.

6 Using Classes When you use a class object: The calling program doesn’t know: How to initialize the object’s data How to implement the object’s methods The object itself is responsible for these things. The calling program accesses them by calling predefined methods.

7 /** DRIVER PROGRAM *... appropriate documentation... */ package c09classes.icecream; public class IceCreamConsole{ //Create a default order IceCreamOrder firstOrder = new IceCreamOrder(); System.out.println(firstOrder); //Change the number of scoops firstOrder.setScoops(3); System.out.println(firstOrder); //Print out how much the order costs System.out.println(“Collect: “ + firstOrder.getCost()); }

8 Designing Classes When we want to work with objects not supported by existing types, we must design a new class. The key design issues here are: What classes do we need? What goes in them (and what doesn’t)?

9 Information Hiding When we design a class we distinguish: the external interface to a class; the internal implementation of the class. The principle of information hiding dictates that a class designer: provide public views of those things that a class user really needs to know; hide all other details by making them private.

10 Design using Perspectives Use an external perspective to specify the public interface to a class. Use an internal perspective to specify the internals of the class design.

11 Implementing Classes Implementing the class attributes Implementing the class methods: Constructors Default-value constructor Explicit-value constructor Accessors Mutators Other Methods Copy Constructors

12 Class Attributes IceCreamOrder objects will certainly have to encapsulate their own: # of scoops; flavor; fulfilled status These will be stored as instance variables, which means that each IceCreamOrder object will have its own versions of these values.

13 Implementing Class Attributes class IceCreamOrder{ private int myScoops; // number of scoops private String myFlavor; // flavor of ice cream private boolean myStatus; // order completion // other class stuff here… }

14 Default-Value Constructor External View (in the driver): IceCreamOrder order1 = new IceCreamOrder(); Internal View (in the class): /** * Construct a new IceCreamOrder with default values */ public IceCreamOrder() { myScoops = 1; myFlavor = “Vanilla”; myStatus = false; }

15 Constructors as Methods Constructors are like methods except that: They have no return type They are given the same name as the class They are invoked with new: IceCreamOrder order = new IceCreamOrder(); Constructors initialize instance variables within the limits set by the invariants. Constructor methods are often overloaded.

16 Class Invariants Objects must maintain the integrity of their internal data. /** * Indicate whether or not the number of scoops is valid. scoops the value to check true if valid, false otherwise */ private boolean isValidScoops(int scoops) { if (scoops < 1) { return false; } return true; }

17 Explicit-Value Constructor External View: IceCreamOrder order1 = new IceCreamOrder(2, “Superman”, false); Internal View: /** Construct a new IceCreamOrder scoops the number of scoops flavor the ice cream flavor status the order status */ public IceCreamOrder(int scoops, String flavor, boolean status){ if(isValidScoops (scoops)){ myScoops = scoops; } else{ System.err.println(“Invalid number of scoops: “ + scoops); System.exit(-1); } myFlavor = flavor; myStatus = status; }

18 Accessor Methods my number of scoops */ public int getScoops() { return myScoops; } my flavor */ public String getFlavor() { return myFlavor; } my order status */ public boolean getStatus() { return myStatus; }

19 /** Change my order completion status */ public void setStatus(boolean status) { myStatus = status; } /** Set a new number of scoops for the order scoops non-negative scoops value */ public void setScoops(int scoops) { if (isValidScoops(scoops)){ myScoops = scoops; } else{ System.err.println(“Could not change number of scoops to “ + scoops); System.exit(-1); } Mutator Methods

20 /** * Print out the value of the order */ public String toString() { String result = myScoops + “ scoop(s) of “ + myFlavor + “ :”; if (myStatus){ result += “ Fulfilled”; } else{ result += “ Unfulfilled”; } return result; } Other Methods

21 Copy Constructors ● We can’t “copy” objects as follows: IceCreamOrder order1 = new IceCreamOrder(); IceCreamOrder order2 = order1; This only copies the reference. ● Thus, we must write copy constructors: IceCreamOrder order1 = new IceCreamOrder(); IceCreamOrder order2 = order1.copy();

22 Copying Objects /** a copy of myself */ public IceCreamOrder copy() { return new IceCreamOrder(myScoops, myFlavor, myStatus); }

23 Instance versus Class Members Java allows data and methods to be associated with either: A particular object, or The class as a whole. Instance members are defined for each object of a class. Class members are marked as static and are shared by all objects of a class. Static methods only reference static data. Static methods can be called without creating an instance of the class.

24 Referencing Members Instance members are referenced using the object identifier. objectIdentifier. memberIdentifier Class members are referenced using the class identifier. ClassIdentifier. memberIdentifier Examples: instance: keyboard.readDouble() class: Math.PI

Class Data Instance data is unique for each object of a class Class data is shared between all objects of a class Marked as static private static final double PRICE_PER_SCOOP = 1.50; public double getCost() { return myScoops * PRICE_PER_SCOOP; }

26 /** * Compares one order to another other whether or not the order is bigger than * some other order */ public boolean isBigger(IceCreamOrder other){ return myScoops > other.getScoops(); } Object Interaction ● Sometimes objects must interact with each other.