Lecture 4 Creating A simple Class In the previous class I stated that one of the principle goals of object oriented design was the creation and use of classes In that class we focused on using the Java API and using existing classes provided by the Java language itself Today I want to focus on creating simple classes To create classes an object oriented programming language must support the following key language features Data Abstraction information hiding encapsulation inheritance and polymorphism Data Abstraction is the creation and use of User defined Abstract Data Type as Conceptual Models of real world problems Every program we create models some aspect of the real world an airline reservation system Patient monitoring system 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 We do this through the definition of Application specific Abstract Data Types which are implemented as classes Program Requirements Abstract Data Types Built in Objects int An Abstract Data Type represents a class of related objects which have a common set of properties a set of operations which process objects of the data type while preserving the properties of the objects A CLASS IS AN ABSTRACT DATA TYPE With Abstract Data Types we may create as many objects of a class as we need An Abstract Data Type is frequently implemented as a module which is an independent programming unit containing a set of type declarations representing the object and a set of functions which manipulate objects of the type This is a class definition that may be part of a larger construct called a package A package is a set of related classes Part of the creation of an ADT is the use of Encapsulation and Information hiding Information Hiding Is the practice of controlling access to the details of a class module or data structure 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 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 the rest of the program 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 In terms of defining classes this means that the definition of the data structure that contains attributes is grouped together with the implementation of methods Inheritance and Polymorphism are advanced concepts that we will cover more fully later in the semester Inheritance Abstract data types are obviously potentially reusable components However in most instances of reuse the features and capabilities of the existing type were not quite right for the new use The old type required some form of modification Such modifications could be difficult because they required the developer doing the modification to understand part if not all of the existing code Also real world objects have logical relationships to one another Some objects represent specializations of other categories of objects Bank accounts checking accounts or Balls and Tennis Balls If we use abstract data types only shared properties of the types would need to be implemented by duplicating the code writing a brand new module for the new type Inheritance provides solutions to these problems Inheritance is the definition of a new type that is ALMOST the same as some other previously defined type Properties of the new type that are identical to corresponding properties of the old type are automatically inherited and need not be specified again distinct properties of the new type are specified explicitly in the form of new program text This is one form of reuse by defining a type based on a previously existing type it allows the new type to inherit not only the attributes of the previous type but also the operations defined for the types rather than rewriting the type from scratch Polymorphism Polymorphism is the definition of operations that apply to more than one type Polymorphic means having many forms In object oriented languages polymorphism refers to the late binding of a method call to a specific method in an object Polymorphic operations are defined for classes or families of types that have common properties These common properties make it meaningful to define an operation applying to all sub types in the class For example it is possible to define an operation for a parent class
View Full Document
Unlocking...