05-10-2012, 04:14 PM
Object-Oriented Programming Concepts in Java
oop.ppt (Size: 630.5 KB / Downloads: 56)
INTRODUCTION
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 discussed this somewhat already and will do so more in Chapter
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
We will discuss this more in Chapter 8
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 this more in Chapter 9
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 ints
However, together they make up a Polygon
This is fundamental to object-oriented programming (OOP)
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
Encapsulation and Data Abstraction
Recall that we previously discussed 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
This concept is exemplified through objects
We can think of an object as a container with data and operations inside
We can see some of the data and some of the operations, but others are kept hidden from us
The ones we can see give us the functionality of the objects
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!
Instance Variables
Let’s look again at StringBuffer
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 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 and is actually inherited from AbstractStringBuilder
See source in StringBuffer.java and AbstractStringBuilder.java – cool!!!
Many 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?
In AbstractStringBuilder the value variable has no keyword modifier
This makes it private to the package
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
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 one or more classes that represent a CD (compact disc)
Write a simple driver program to test it