lec.7 data abstraction

38
Lec.7 Data Abstraction Jiang (Jen) ZHENG June 1 th , 2005

Upload: leila-thornton

Post on 04-Jan-2016

55 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lec.7 Data Abstraction

Lec.7Data Abstraction

Jiang (Jen) ZHENG

June 1th, 2005

Page 2: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

2

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

Page 3: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

3

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

Page 4: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

4

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

Page 5: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

5

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+

Page 6: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

6

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.

Page 7: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

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

Page 8: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

8

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

Page 9: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

9

Objects and Data Abstraction

Objects enable us to combine the data and operations of a type together into a single entity

Pxpoints [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

dataEx: translate means translate the points

that make up P

Page 10: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

10

Objects and Data Abstraction

For multiple objects of the same class, the operations act on the object specifiedint [] moreXs = {8, 11, 8};int [] moreYs = {0, 2, 4};Polygon P2 = new Polygon(moreXs, moreYs, 3);

P

xpoints [0,4,4,0]ypoints [0,0,2,2]

npoints 4

addPoint()contains()translate()

P2

xpoints [8,11,8]]ypoints [0,2,4]

npoints 3

addPoint()contains()translate()

Page 11: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

11

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

Page 12: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

12

Encapsulation and Data Abstraction

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 OR how the methods are implemented

I know it has points but I don’t know how they are stored

Data Abstraction!

P

xpoints [0,4,4,0]ypoints [0,0,2,2]

npoints 4

addPoint()contains()translate()

Page 13: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

13

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?

Page 14: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

14

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

Page 15: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

15

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!

Page 16: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

16

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?

Page 17: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

17

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 classStringBuffer B = new StringBuffer(“this is “);

B.append(“really fun stuff!”);

System.out.println(B.toString());

Page 18: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

18

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)

Page 19: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

19

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

Page 20: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

20

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

Page 21: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

21

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 variablesB.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

Page 22: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

22

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!!!)

Page 23: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

23

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

Page 24: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

24

Developing Another Example

Remember the things we need for a class: Instance variables

… Constructors

… Accessors

… Mutators

Page 25: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

25

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

Page 26: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

26

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;

Page 27: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

27

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

Page 28: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

28

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

Page 29: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

29

Arrays of Objects Note that we have two levels of references here

See CDTest.java for another example

names0

1

2

3

4

Herb

Madge

Mort

Page 30: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

30

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

Page 31: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

31

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

Page 32: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

32

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

Page 33: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

33

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

Page 34: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

34

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

Page 35: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

35

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

Page 36: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

36

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

Page 37: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

37

Programming Style

Public members first Constants first Then Constructors Then Methods, can be alphabetically listed. Overloaded methods are together

Then Private members

Page 38: Lec.7 Data Abstraction

CS401/COE401 Summer 2005.Department of Computer Science.University of Pittsburgh.Lecture 7

38

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.