Lecture 7
So far we have developed programs mostly by modifying existing ones, or using a template. Now I start explaining the
principles of object-oriented programming in Java and we learn how we create our own classes and objects.
By learning how to define new classes and work with
user-defined objects, we move into object oriented
programming. It is a different, more advanced programming style,
suitable for designing, developing and maintaining larger
programming projects than the rather elementary exercises typically done
in introductory programming classes.
- Object oriented programming comes with a distinct programming
philosophy. The way you think about your program is
different.
- You design your program from the top, thinking about
what you want to do and organizing the code into
functional units that act on objects.
- You may want to think of
objects as being highly structured data with some prescribed
functionality. For example, in Java you have seen Button's, Fonts etc.
- In C, you have seen so far only one
type of non-elementary data structure, the array. An array is
only one of the many possible ways of grouping data. One of its
characteristics, for instance, is that it is suitable for the use
of iterative for loops to access all of its elements.
- In object
oriented programming you group the data by functional criteria and at
the same time specify the methods that are relevant for
manipulating the objects in that class. For instance, a Button
can be created (constructed) with a specific label; it can be
added on to the applet, etc.
I will introduce object oriented programming with a
simple example, then we will work out a more complex program.
Simple Example
- New type of object: myPoint (a sort of point...).
class myPoint{
{
...
}
- Information grouped together in a single object of type
point: the x and y coordinates of a point on the screen.
class myPoint{
{
int x;
int y;
...
}
- Operations done with an object of type point:
- Create a
new point (from a pair of integers giving the coordinates).
- Draw a point on the screen (for instance, as a small
black spot).
- Print the coordinates of the point on the Java
console.
In Object oriented programming, we refer to these operations as
methods applied on objects of type myPoint, rather
than operations or functions or procedures.
public class myPoint{
{
int x;
int y;
myPoint(int a, int b) // constructor
{
...
}
public void Print() // print method
{
...
}
public void Draw() // draw method
{
...
}
// and perhaps other methods
...
}// end class myPoint
- Applying methods on an object from this new class.
- Creating a new object with new:
myPoint p1; // declaration of a variable of type myPoint
...
// creating a new object of type myPoint
p1 = new myPoint(20,30);
...
- To access the values of the class variables of an existing
object:
p1.x
p1.y
- To Print an object of type myPoint:
p1.Print(); // "apply" the method Print to the object p1
- To Draw an object of type myPoint:
p1.Draw(); // "apply" the method Draw to the object p1
How to work with this new class in Java.
Here's an example, incorporating
the new class into a complete Java program.
In-class exercise
Do
this exercise.