380 likes | 623 Views
Lec.7 Data Abstraction. Jiang (Jen) ZHENG June 1 th , 2005. Outline. Intro. To Object-Oriented Programming Data Abstraction and Encapsulation Inheritance Polymorphism Data Abstraction Polygon Example Functional Abstraction -> Data Abstraction StringBuffer Example Elements of a Class
E N D
Lec.7Data Abstraction Jiang (Jen) ZHENG June 1th, 2005
Outline • Intro. To Object-Oriented Programming • Data Abstraction and Encapsulation • Inheritance • Polymorphism • Data Abstraction • Polygon Example • Functional Abstraction -> Data Abstraction • StringBuffer Example • Elements of a Class • Instance Variables • Class Methods vs. Instance Methods • Constructors, Accessors and Mutators • Classes vs. objects • Static variables • Arrays of Objects • Composition • Accessibility of Variables and methods • Programming Style CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Intro. to Object-Oriented Programming (OOP) • Object-Oriented Programming consists of 3 primary ideas: • Data Abstraction and Encapsulation • Operations on the data are considered to be part of the data type • We can understand and use a data type without knowing all of its implementation details • Neither how the data is represented nor how the operations are implemented • We just need to know the interface (or method headers) – how to “communicate” with the object • Compare to functional abstraction with methods • We will discuss this next in Chapter 6 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Intro. to OOP • Inheritance • Properties of a data type can be passed down to a sub-type – we can build new types from old ones • We can build class hierarchies with many levels of inheritance • Polymorphism • Operations used with a variable are based on the class of the object being accessed, not the class of the variable • Parent type and sub-type objects can be accessed in a consistent way • We will discuss both of these later in Chapter 7 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • Up to this point we have concentrated on primitive types • Each variable represents a single, simple data value • Any operations that we perform on the data are external to that data X + Y X 10 + Y 5 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • Consider the data • In many applications, data is more complicated than just a simple value • Ex: A Polygon – a sequence of connected points • The data here are actually: • int [] xpoints – an array of x-coordinates • int [] ypoints – an array of y-coordinates • int npoints – the number of points actually in the Polygon • Note that individually the data are just integers • However, together they make up a Polygon • This is the fundamental of OO programming. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • Consider the operations • Now consider operations that a Polygon can do • Note how that is stated – we are seeing what a Polygon CAN DO rather than WHAT CAN BE DONE to it • This is another fundamental idea of OOP – objects are ACTIVE rather than PASSIVE • Ex: • void addPoint(int x, int y) – add a new point to Polygon • boolean contains(double x, double y) – is point (x,y) within the boundaries of the Polygon • void translate(int deltaX, int deltaY) – move all points in the Polygon by deltaX and deltaY CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • These operations are actually (logically) PART of the Polygon itself int [] theXs = {0, 4, 4};int [] theYs = {0, 0, 2};int num = 3;Polygon P = new Polygon(theXs, theYs, num);P.addPoint(0, 2);if (P.contains(2, 1)) System.out.println(“Inside P”);else System.out.println(“Outside P”);P.translate(2, 3); • We are not passing the Polygon as an argument, we are calling the methods FROM the Polygon CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • Objects enable us to combine the data and operations of a type together into a single entity P xpoints [0,4,4,0] ypoints [0,0,2,2] npoints 4 addPoint() contains() translate() Thus, the operations are always implicitly acting on the object’s data Ex: translate means translate the points that make up P CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • For multiple objects of the same class, the operations act on the object specified int [] moreXs = {8, 11, 8};int [] moreYs = {0, 2, 4};Polygon P2 = new Polygon(moreXs, moreYs, 3); P P2 xpoints [0,4,4,0] ypoints [0,0,2,2] npoints 4 addPoint() contains() translate() xpoints [8,11,8]] ypoints [0,2,4] npoints 3 addPoint() contains() translate() CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Encapsulation and Data Abstraction • Recall that previously we discussed functional abstraction (Lec4 Slide 13) • We do not need to know all of the implementation details of the methods in order to use them • We can extend this idea to data as well: • Data Abstraction • We do not need to know the implementation details of a data type in order to use it • This includes the methods AND the actual data representation of the object CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
As long as we know the method names, params and how to use them, we don’t need to know how the actual data is stored Note that I can use a Polygon without knowing how the data is stored ORhow the methods are implemented I know it has points but I don’t know how they are stored Data Abstraction! Encapsulation and Data Abstraction P xpoints [0,4,4,0] ypoints [0,0,2,2] npoints 4 addPoint() contains() translate() CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • Let’s look at another example – StringBuffer • Also discussed in text • A StringBuffer in Java is an object that can store a variable length string • Compare with String (Recall the String methods we know …) • We can add characters to or remove them from any position in the StringBuffer • To use a StringBuffer in our programs, what do we need to know? CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Objects and Data Abstraction • We need to know the general type of data being stored • Strings of characters • We need to know the operations that can be done • Method names, parameters and return values, as well as an idea of what each method is supposed to do • What DON’T we need to know • How the characters are actually stored in memory • How any of the methods are actually implemented • This is how data abstraction works • We can use any number of classes with just basic information about the data and methods CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Instance Variables • Let’s look at StringBuffer in more detail • Instance Variables • These are the data values within an object • Used to store the object’s information • As we said previously, when using data abstraction we don’t need to know explicitly what these are in order to use a class • For example, look at the API (application program interface)for StringBuffer • Note that the instance variables are not even shown there • In actuality it is a variable-length array with a counter to keep track of how many locations are being used • See source in StringBuffer.java – cool! CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Instance Variables • Note that the instance variables are declared with the keyword private • This means that they cannot be directly accessed outside the class itself • Instance variables are typically declared to be private, based on the data abstraction that we discussed earlier • Recall that we do not need to know how the data is represented in order to use the type • Therefore why even allow us to see it? CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Class Methods vs. Instance Methods • Recall that methods we discussed before were called class methods (or static methods) • These were not associated with any object • Now, however we WILL associate methods with objects (as shown with Polygon) • These methods are called instance methods because they are associated with individual instances (or objects) of a class StringBuffer B = new StringBuffer(“this is “); B.append(“really fun stuff!”); System.out.println(B.toString()); CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Class Methods vs. Instance Methods • Class methods have no implicit data to act on • All data must be passed into them using arguments • Class methods are called using: ClassName.methodName(param list) • Instance methods have implicit data associated with an Object • Other data can be passed as arguments, but there is always an underlying object to act upon • Instance methods are called using: VariableName.methodName(param list) CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Constructors, Accessors and Mutators • Instance methods can be categorized by what they are designed to do: • Constructors • These are special instance methods that are called when an object is first created • They are the only methods that do not have a return value (not even void) • They are typically used to initialize the instance variables of an object StringBuffer B = new StringBuffer(“hello there”); B = new StringBuffer(); // default constructor B = new StringBuffer(10); // capacity 10 CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Constructors, Accessors and Mutators • Accessors • These methods are used to access the object in some way without changing it • Usually used to get information from it • No special syntax – categorized simply by their effect StringBuffer B = new StringBuffer(“hello there”); char c = B.charAt(4); // c == ‘o’ String S = B.substring(3, 9); // S == “lo the” // note that end index is NOT inclusive int n = B.length(); // n == 11 • These methods give us information about the StringBuffer without revealing the implementation details CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Constructors, Accessors and Mutators • Mutators • Used to change the object in some way • Since the instance variables are usually private, we use mutators to change the object in a specified way without needing to know the instance variables B.setCharAt(0, ‘j’); // B == “jello there” B.delete(5,6); // B == “jello here” B.insert(6, “is “); // B == “jello is here”; • These methods change the contents or properties of the StringBuffer object • We use accessors and mutators to indirectly access the data, since we don’t have direct access – see ex8.java CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Simple Class Example • We can use these ideas to write our own classes • Let’s look at a VERY simple example: • IntCircle • Instance variable: private int radius • Cannot directly access it from outside the class • Constructor: take an int argument and initialize a new circle with the given radius • Accessors: public double area(); public double circumference(); public String toString(); • Mutator: public void setRadius(int newRadius); • See IntCircle.java and ex9.java (note COMMENTS!!!) CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
More on Classes and Objects • Classes • Define the nature and properties of objects • Objects • Instances of classes • Let’s learn more about these by developing another example together • Goal: • Write a class that represents a CD (compact disc) • Write a simple driver program to test it CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Developing Another Example • Remember the things we need for a class: • Instance variables • … • Constructors • … • Accessors • … • Mutators • … CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Static (class) Variables • Earlier we discussed static methods • These are associated with a class, not an object • Analogous to static methods are static variables • Declared with classes using the keyword static • Associated with the class, not with individual objects • One variable exists regardless of how many or how few objects exist • Accessible to all objects and through the class itself CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
final • The keyword final indicates that this variable can’t be changed once it has been initialized • Ex: In class Math, public static final double PI = 3.14159265358979323846; CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Arrays of Objects • We have seen already how to create and use Java arrays of primitive types: int [] data; // declare variable (reference) data = new int[20]; // create array object … data[4] = 77; // index array to access locations • How does it differ if we want arrays of objects? • The first two steps are the same • Declare variable • Create array object CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Arrays of Objects • However, remember that objects are accessed by reference types • Thus, when we create the array, we have an array of references, with no objects yet • We need to create the objects to store in the array separately • For example: String [] names; names = new String[5]; names[1] = new String(“Herb”); names[3] = new String(“Madge”); names[4] = new String(“Mort”); • names[0] and names[2] are still null CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Arrays of Objects • Note that we have two levels of references here • See CDTest.java for another example names Herb Madge Mort CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Accessing Variables within Classes • We have discussed instance variables • Variables defined within classes that are part of objects • We have access to these within any of the methods in a class • However, keep in mind that if an instance variable is itself a reference type, we can only access the portions of it that are declared to be public CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Composition • If we use a reference type as an instance variable within a new class, we are using composition to build the new class • We are “composing” the new class from pieces that already exist, putting them together in an appropriate way • Our use of these classes is limited to the functionality provided as public • We are building new classes using “off the shelf” components, so we may have to compromise based on what the “off the shelf” components can do CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Accessing Variables within Classes • Within methods in classes, we may also have parameters and method variables • Our access to these variables depends on their class types • If they are of the SAME CLASS as the class we are in, we have full access to them • For example, a method for class Foo also has a parameter of class Foo • If they are of a DIFFERENT CLASS than the class we are in, we have the normal restricted access based on the public methods CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
this • We know we can access any instance variables within instance methods • We can also call instance methods from within other instance methods • However, in both cases we are accessing the current object from within • Sometimes we need to access the object from OUTSIDE, even though we are inside it CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
this • this is a special pseudo instance variable that refers to the current object • It enables us to access the entire object from the inside CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
private Methods • Let us see Fraction that some methods are declared to be private • This allows access to them within Fraction but not from outside • The idea is that these are “internal” methods – needed to implement the class but not part of the interface with the outside world • i.e. Should not be called directly from outside the class • Access declaration guidelines: • When creating a new class, consider the access needs of the user CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Access Guidelines • Instance variables should not typically need to be accessed from outside the class, and thus should be declared to be private • Instance methods may or may not need to be accessed from the outside • Is so, make them public • If not, make them private • The idea is that the programmer is controlling how much of the inner details of the class that the user should have access to CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Programming Style • Public members first • Constants first • Then Constructors • Then Methods, can be alphabetically listed. • Overloaded methods are together • Then Private members CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7
Next Topic • Reading Assignment: Chapter 7 You don’t need to know the details but you should know what are those when I talk about it. CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7