Intro. to OOP

advertisement
Computer Science II
810:062 Section 01
How is CS I different from CS II?
• When you teach Java there are a series of
decisions that have to be made…
How is CS I different from CS II?
• When you teach Java there are a series of
decisions that have to be made…
Applet
Core Java
IDE
Object Oriented
Event-driven
or
or
or
or
or
Applications
Textbook package
command line
“structured”
“Non-event driven”
How is CS I different from CS II?
• When you teach Java there are a series of
decisions that have to be made…
Applet
Core Java
IDE
Object Oriented
Event-driven
or
or
or
or
or
Applications
Textbook package
command line
“structured”
“Non-event driven”
How is CS I different from CS II?
• When you teach Java there are a series of
decisions that have to be made…
Applet
Core Java
IDE
Object Oriented
Event-driven
or
or
or
or
or
Applications
Textbook package
command line
“structured”
“Non-event driven”
How is CS I different from CS II?
• CS I focuses on learning to program in the
first place.
• CS II focuses on learning how to program
well using the paradigm of Object Oriented
Programming.
So what is OO programming?
• Object-oriented programming changes the
way software developers think about the
problem by redefining the problem.
• Object-oriented programming encourages
us to think about the computer as a
simulator of the world. Thus, a program is
a map of the world we want to imitate.
So what is OO programming?
• Instead of asking
– How can I write a program that is structured
like the machine on which it runs?
• We ask
– How can I write a program that is structured
like the part of the world it describes?
So what is OO programming?
The anatomy of an OO program
The world
consists of objects
that interact
to solve a problem.
So what is OO programming?
• “In Java, the main thing you do is write
class definitions.” (R. Morelli)
• A class definition encapsulates two (three
actually) things:
– Data (instance variables)
– Actions (constructors and methods).
So what is OO programming?
• But a class on its own is nearly worthless.
• The purpose of a class is to serve as a
template for creating individual objects, or
instances, of the class.
The anatomy of an OO program
• An object is an instance of a class
MemoPad p = new MemoPad();
• While a class describes a set of objects that
behave similarly, each instance of a class is
distinct and has its own state.
MemoPad p = new MemoPad();
MemoPad q = new MemoPad();
So what is OO programming?
• an object-oriented program is a set of
objects that collaborate to achieve a goal
• objects interact by sending each other
messages (invoking an object’s method).
Let’s consider a MemoPad example
Allows for the storage of a collection of
memos that are associated with keys
For example, you can insert an memo:
Identify the objects needed in the
MemoPadApp example
List the objects and their behaviors
What came first the chicken or
the egg?
Since objects create other objects
dynamically as needed, where
does the first object come from?
The anatomy of an OO program
• The world is a class that contains the main() function.
• main() is the “big bang” that creates one or more objects.
public class MemoPadApp {
public static void main( String [] args ) {
MemoPad p = new MemoPad();
p.show();
} // end main
} // end class MemoPadApp
So what is OO programming?
• Notice that “main” is a static method.
• We will discuss what static means in more
detail later this semester, but for now you
can think of this modifier as saying that
main is associated with the class
memoPadApp, and not with any instance of
the class.
• “main” is a class method.
So what is OO programming?
• an object-oriented program is a set of
objects that collaborate to achieve a goal
• objects interact by sending each other
messages (invoking an object’s method).
So how do objects collaborate?
• They send one another messages.
• In response to a message, an object executes
the method associated with the message that
it receives, both names and arguments.
• A big part of designing an object-oriented
system is deciding what messages each
object should respond to.
• The set of messages to which an object
responds is called its interface.
Designed an interface for the
memo database class
• the memo database class is used to hold the
actual memos being stored
• its interface is the set of messages to which
it should respond
Think about:
• What is the behavior of a memo database?
• What should it do in order to contribute to
the world of objects that implements the
behavior of a memo pad?
Interface for the memo database
public interface MemoDatabase {
} // end interface MemoDatabase
Interface for the memo database
public interface MemoDatabase {
public String find ( String key );
} // end interface MemoDatabase
Interface for the memo database
public interface MemoDatabase {
public String find ( String key );
public boolean remove ( String key );
public boolean insert (MemoAssociation m);
public boolean containsKey( String key );
} // end interface MemoDatabase
For next class
• Read chapter 1. Identify any concepts or
terms that are new to you so we can discuss
them.
• Re-acquaint yourself with the Java tools
available at home or in the lab. Download
the MemoPad code from the course Java
page, and play with it.
Download