Unformatted text preview:

Chapter 8 Creating Simple Classes One of the principle goals of object oriented design was the creation and use of classes to represent real world objects in the programs we are designing So far we have focused on using the Java API and using existing classes provided by the Java language itself But have yet to discuss what a class is and how it is composed To provide the ability to create classes an object oriented programming language must support the following key language features Data Abstraction information hiding encapsulation inheritance and polymorphism Lets begin by reviewing some terms introduced earlier in the semester 1 A Class A class represents a group of similar real world or abstract things or objects which share a set of common properties called attributes and a common set of behaviors which define how objects of the class can be use these actions are implemented as methods A class definition is a template containing both a variable declarations representing attributes b methods representing object behavior A class definition can be created to represent any entity in the solution of a problem space a passenger a ticket agent a queue or a ticket 2 An object or instance A technical term for a specific object of a class ONE specific occurrence of an individual object with a specific set of values for the data members of the class String name new String Camille 3 method objects a procedure body implementing an operation defined for a class of 4 message A procedure call a request to execute a method Objects interact by sending messages to each other A computation is characterized by the messages sent and received by objects in a program name length Math pow 2 4 When designing programs why do we need to design classes Every program we create models some aspect of the real world an airline reservation system Patient monitoring system checkout line at a grocery store There is a tremendous gap between the kinds of problems we are asked to solve using a computer and the BITS of information the computer can actually process Not every aspect of the data requirements of a problem can be adequately expressed directly using the built in data or primitive data types provided by any programming language Another related problem is how to capture the behavior of objects so that they are consistent with the requirements of the real world problem for example A check out line at the grocery store consists of a series of individuals each of which have a set of items that they wish to purchase These individuals enter a checkout line wait for service place their items on the checkout counter and pay for their purchase WE need a method of going from Program Requirements Built in Objects int We need to come up with a process for taking the built in data types we have available in any language and extending these type to create NEW application defined DATA TYPES that model objects and to capture their behavior This is the idea behind Data Abstraction Data Abstraction is the creation and use of User defined Abstract Data Type as Conceptual Models of real world problems We do this through the definition and implementation of classes Program Requirements Abstract Data Types Classes Built in Objects int An Abstract Data Type ADT represents a group of related objects which have a common set of properties a set of operations or behaviors A CLASS IS AN ABSTRACT DATA TYPE By defining classes we may create as many objects of a class as we need Information Hiding Is the practice of controlling access to the implementation details of a class or ADT This is based on the idea that we do not need to know implementation details to make effective use of an object For example we do not need to know how STRINGS are stored internally in memory to make use of them in our program We only need to know what they represent and what operations may be performed on them They are a sequence of characters that we may access individually change the case of search for the first occurrence of a character or substring etc We can declare objects using the types provided in the module and we make use of the operations provided in the module to manipulate the objects we ve created with out knowing the exact implementation details String name new String Camille Hayhurst name charAt 10 This is important because when ever a module is implemented which relies upon the detailed implementation of another it becomes vulnerable It causes the situation where changes in one portion of a program can have rippling and unexpected errors in other portions I don t want a change in the implementation of class String to crash my program which uses String variables Information Hiding is the separation of the behavior and use of an object from its implementation This means that the implementation of an object can be changed with out the change rippling through other programs that use the class Encapsulation Encapsulation is the concentration of all program text dependent on a particular design choice in one place Encapsulation is a feature that allows a compiler to enforce information hiding A class definition provides encapsulation Inheritance and Polymorphism are advanced concepts relative to object oriented design that we will cover more fully later in the semester Defining Classes Defining a class is a two step process 1 Determining what attributes all objects in the class share in common 2 Define the interface for the class The interface is the set of methods and possibly attributes that we will make available to users of the class Along with the interface we will need to define the set of attributes that define objects of the class As an example we will design a simple class to represent Accounts at a bank Our class account represents accounts that customers will open at a bank Although there are many potential data items that a real bank would collect about their customer accounts we will start with the following Class Account Data Members Account Balance Holder name Overdrawn true false Operations Open Credit Debit Check balance Print account information Some of the class members we define will be instance data members and methods that apply to declared instances of the class others will be class members which are static data members and methods that apply to the class as a whole and are usable whether or not ANY instances of the class are ever declared Once we create our class definition we can declare instances of the class which


View Full Document

WVU CS 110 - Creating Simple Classes

Loading Unlocking...
Login

Join to view Creating Simple Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Creating Simple Classes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?