Held: Monday, 29 August 2005
Summary:
Today we continue our exploration of core course issues by visiting
the object-oriented paradigm.
We also attempt to define our first class of objects.
Related Pages:
Assignments
Notes:
- As you may have already observed, what I do in class does not always match what I've prepared on the outline. Live with it.
- Please scan the class site for tomorrow and bring questions.
Overview:
- Grounding Ourselves
- Object-Oriented Programming
- Exercise: Rational Numbers
- Before we delve too far into these issues, we should ground
ourselves somewhat by asking ourselves a few questions
(and I'll be asking these of the class).
- What is Computer Science?
- What is Computer Programming?
- How are they similar? How are they different?
- What is an algorithm?
- What is a computer program?
- We also want to ask ourselves some practical questions.
- What programming languages do we know?
- What CS or programming concepts are we least comfortable with?
- How comfortable are we with the workstations and Unix?
- Finally, I'd like you to reflect on the course.
- Why are you taking this course?
- What do you expect to get out of this class?
- Object-oriented languages have two very different motivators:
- The early object-oriented languages were designed to provide a better way to build simulations. In these languages, each program object simulated a real-world object.
- Most modern object-oriented languages use the tools of the early languages to provide an environment that better supports large-scale program design. In particular, good object-oriented languages support
- Information hiding: Limiting the portions of one part of a program that are immediately accessible by other parts of a program.
- Code reuse: Allowing programmers to reuse parts of old programs in new ways.
- As you might guess, object-oriented languages concern themselves with objects.
- You might have a sense of what a
real world
object is.
- In computerese, an object is a collection of information (sometimes called attributes or fields) and operations that the object can perform (called methods).
- We obtain information hiding by limiting the access one has to the fields of the object.
- We often classify methods as observers, which get information but don't change the underlying object, and mutators, which change the underlying object.
- For example, we might say that the textbook I used in the past
- has title
Java Structures
(attribute)
- is published in hardback (attribute)
- was written by Dwayne Bailey (attribute)
- contains text that you can extract (the text is an attribute, the extracting
of the text might be considered an observer method)
- permits you to rip pages out
- We often categorize objects into classes. A class specifies
common aspects of a set of objects. These aspects are often
generic attributes and specifications of methods (E.g., ``all objects in
this class have a color, a size, and can draw themselves''.)
- Almost every book has a title
- Almost every book has one or more authors
- Almost every book has pages
- Almost every book has text on various pages
- Most object-oriented languages support inheritance, in which
classes can be based upon other classes. For example, we might say that
library books are a subclass of books and inherit all the
attributes of books. Library books also extend books.
- Since books have titles, library books also have titles
- Since books have authors, library books also have authors
- Library books also have
loan records
: information on who has
the book.
- A subclass inherits the attributes and methods of its
parent class.
- The parent class is often called a superclass.
- Most object-oriented languages support polymorphism, in which
a member of a subclass can be used in place of a member of a superclass.
- If we know how to read a book, we know how to read a library book.
- Many object-oriented languages are event-driven. That is,
you can specify
when this event happens, call this method
of this object
.
- As mentioned above, a key aspect of object-orientation (and good
program design in any language) is information hiding.
In general, objects should know what other objects do, but
not how they do it.
- For example, you might know that a
Menu object can
draw menus on the screen, but it's not important to you how it
does it.
- This property is also called encapsulation.
- Polymorphism, inheritance, and encapsulation are three key aspects
of object-oriented programming.
- We describe operations (methods, procedures) very differently in object-oriented languages. In general, instead of saying
Apply this procedure to these values
, we say Tell this object to do this operation/method using these additional values
.
- For example, instead of saying
Print the value 2 to the screen
, we might say Screen, please print the value 2
or even 2, please print yourself to the screen
.
- Similarly, instead of saying
Add 2 and 3
, we might say 2, please add 3 to yourself and tell me the result
or possibly 2, please increment yourself by 3
.
- It takes a while to get used to this variant way of thinking about operations.
- When designing a new class, you should consider what methods are appropriate for the class.
- As I noted above, we tend to classify our methods in three ways:
- constructors allow us to build new objects;
- observers extract information from an object; and
- mutators change an object.
- These classifications are not perfect, since some operations can fit in more than one classification.
- We'll begin by attempting to list and classify the methods for a rational number (fraction) class.