Title: Introduction ObjectOriented Programming Behavioral Abstraction Compilation
1IntroductionObject-Oriented ProgrammingBehaviora
l AbstractionCompilation
Lecture 17
2IntroductionObject-Oriented Programming
3The Journey So Far
Procedural Programming
using Pseudocode
You are here
4The Destination
Procedural Programming
Object Oriented Programming
using Pseudocode
using Java
5Introduction
- We are going to introduce two concepts at the
same time - Object-Oriented Programming
- Programming in a real language
- Why?
- Pseudocode is a good preparation for learning any
language. - The Object Oriented paradigm is a significant
factor in modern programming - We feel that an introductory course should at a
minimum give students a good taste of real
programming - Many students have asked for exposure to a real
language - Excellent preparation for CS 1312
6Introduction
- We will not abandon the principles you have
learned so far - modularity
- abstraction
- the necessity to visually validate the logic
- Today we will give some basic ideas about
- Object Oriented Programming
- Working with a real language Java
7Object Oriented Programming
- A different programming style
- A different way of thinking
- Origins
- Problems with very large systems becoming
unmanageable (gt 100,000 lines of code) - Graphical User Interfaces (e.g. Macintosh,
Windows) required a different way of programming
due to complex interactions of windows, mouse
clicks, etc. - Xerox PARC Smalltalk
- C as an enhancement to C
- Java as a Web language
8To be more specific...
9The Scenario
- Recall the concept of a Queue
- Defined by a set of behaviors
- Enqueue to the end
- Dequeue from the front
- First in, first out
Items
10Where is the Queue?
- The logical idea of the queue consisted of
- Data stored in some structure (list, array, etc.)
- Modules to act on that data
- But where is the queue?
- Wed like some way of representing sucha
structure in our programs.
11Why?
- We would like to have reusable drop-in
programming components we could use to solve
problems. - We would like to encapsulate the data and the
behaviors in order to protect it from misuse. - We would like clear interfaces between different
parts of programs - We would like a programming system that was
extensible - We would like a programming language that would
be excellent for modeling real world objects
12Issues
- As is, there is no way to protect against
violating the specified behaviors.
Procedure Enqueue
Procedure Dequeue
The Queue Data
Sneak into Middle
13Issues
- If our queue could somehow be packaged we could
drop in a queue object whenever we needed it.
Queue
Acme Manufacturing
14Issues
- Wed like a way to put a wrapper around our
structure to protect against intrusion.
Sneak into Middle
15Issues
Contract The party of the first part hereinafter
known as the queue agrees to... The party of the
second part hereinafter known as the application
agrees to...
Clear lines of responsibility
16Issues
- I need a queue that can tell me how many items it
contains...
Queue
Enqueue
Dequeue
Acme Manufacturing
Magic Process
17Issues
Elevator
Signal Object
Elevator Object
Student Object
Car Object
BankAccount Object
Real World Objects!
18Questions?
19Abstraction in Object-Oriented Programming
20Procedural Abstraction
- Procedural Abstractions organize instructions.
Function Power Give me two numbers (base
exponent) Ill return to you baseexponent ???
Implementation ???
21Data Abstraction
- Data Abstractions organize data.
StudentType
Name (string) GPA (num) Letter Grade
(char) Student Number (num)
22Behavioral Abstraction
- Behavioral Abstractions combine procedural and
data abstractions.
23The Object-Oriented Paradigm
- Instances of behavioral abstractions are known as
objects. - Objects have a clear interface by which they send
and receive messages (communicate). - OO is a design and approach issue. Just because
a language offers object-oriented features
doesnt mean youre doing OO programming.
24Information Hiding
- Information Hiding means that the user has enough
information to use the interface, and no more
Example Stick shift We dont care about the
inner workings... We just want to get the car
going!
25Encapsulation
- Encapsulation is the grouping of related things
together within some structure
Item 1
Item 2
Item3
26Encapsulation via Algorithms
- Algorithms encapsulate modules, data, and
instructions.
Algorithm
Procedure
Instructions
Function
Data
27Encapsulating Instructions
- Modules encapsulate instructions.
Procedure/Function
Instructions
Instructions
Module call
Instructions
28Encapsulating Data
- Records allow us to do this with data
Record
Field 1
Field 2
Record
29Revisiting the Question Where is the Queue?
- Notice we still have no way of identifying the
idea were discussing - The Queue is still in the ether.
- Wed like to encapsulate the data (regardless of
its actual representation) with the behavior
(modules). - Once we do this, weve got a logical entity to
which we can point and say, there it is!
30Encapsulating Data with Methods
- Abstract data types (ADTs) allow us to
encapsulate logically related data and modules
Queue
Enqueue
Data
Dequeue
31Abstract Data Types
- An idea, a concept, an abstraction of the big
picture - It encapsulates the abstract behaviors and data
implied by the thing being modeled.
32Achieving Behavioral Abstraction
- Abstract data types (ADTs) are concepts.
- We require some way to implement these common
abstractions so we can write them once, verify
that they are correct, and reuse them. - This would save us from having to re-do work.
For example, every time we create a queue we did - List_Node definesa ...q_front isoftype
...q_tail isoftype ... - procedure Enqueue(...)
- procedure Dequeue(...)
- We need an algorithmic construct that will allow
us to bundle these things together the class.
33OO Summary
- Behavioral abstraction combines data abstraction
with procedural abstraction. - The object-oriented paradigm focuses on the
interaction and manipulation of objects. - An Abstract Data Type (ADT) allows us to think of
what were representing as a thing regardless of
its actual implementation.
34Questions?
35Compiling and the Java Virtual Machine (JVM)
36Working with a real language
- The syntax of Pseudocode is pretty loose
- visual validation encourages a permissive
approach - emphasized the importance of structure
- Compilers essentially require perfection.
- The procedure for writing your algorithms wont
change (much!) - Write a high level approach focusing on
algorithm. - Write the code concentrating on details.
- Check it by hand before compiling.
- Because we're introducing a real language at the
same time as we're introducing the object
oriented paradigm we will require you to just
write some code. - You may not understand it all immediately
- Eventually we'll explain!
37The First Part
- Initially well use Java to write procedural code
just to let you get the feel of a real language. - In fact, well start by programming some of the
same types of modules that we used in the
procedural pseudocode earlier in the semester. - Then, as we introduce the Object Oriented
paradigm well use Java as it was intended.
38About Java
39Introduction to Java
- What Java is
- A real professional programming language
- (which is good and bad...)
- Portable across any hardware platform that has a
JVM interpreter (more later) - An object-oriented language
- What Java is not
- The Ultimate Programming Language
- HTML or another web-content language
- Only useful for web applets
- Just Another Vacuous Acronym
40Real Languages
- Real languages require the programmer to write
code using a very specific set of rules, keywords
and syntax. - Then this code is transformed into actual
instructions that a specific machine can execute.
Often a complex process - A number of strategies have been invented to
accomplish this process - Assemblers
- Compilers
- Interpreters
- A traditional problem has been the necessity to
have different versions of a program for
different machines.
41A New Idea
- Java was specifically developed to be able to
write code once and run it anywhere - How is this magic accomplished?
- Using an intermediate language! Byte code.
- The Byte code is interpreted (executed) using a
special piece of software (a program) called the
Java Virtual Machine (JVM)
42Compilation
Source Code .java
Java compiler
Generic Byte Code .class
OS-specificJVM interpreter
OS-specific Object Code
Execute program
Need one of these for every different OS
43Structure of Java Programs
- Initially well write programs that fit in one
file. - Create the file with an editor (e.g. emacs)
- Compile with javac producing a .class file
- Execute by running java and sending it the .class
file - Our first (tiny) program will be roughly like a
cross between an algorithm and a procedure. - Lets take a look...
44Sample Application
We create a file (using an editor) called
HelloWorld.java
- public class HelloWorld
-
- public static void main(String argv)
-
- System.out.println(Hello World!)
-
We compile by typing (at the OS prompt) javac
HelloWorld.java Which produces HelloWorld.class Th
en we execute by typing java HelloWorld
Hello World!
45Demo
- gtjavac HelloWorld.java
- gtjava HelloWorld
- Hello World!
- gt
46Quick Trix
- The name of the file must match the name of the
class EXACTLY!!! - File Bubba.java
- Contains
- Everything must be EXACTLY correct!!!
Capitalization counts
class Bubba ...
47Eventually...
- Applications (normal computer programs)
- Each program consists of multiple files.
- Each file contains a class.
- Each class will contain modules which will
resemble procedures and functions. - THE MODULES WILL BE CALLED METHODS
- The .class file that you send to java must
contain a method named main - It will actually look like this
- public static void main(String argv ) ..
-
- the JVM will use the file naming convention to
find the other classes required by this main
program.
As opposed to Applets
48Some basic syntax issues
- Your TA is smarter than the java compiler
- Lines need to terminate with a
- Easier said than done
- Braces will be used to indicate "blocks" of code
- Which essentially act like a single line
public class HelloWorld public static void
main(String argv) System.out.println(He
llo World!)
49A look ahead...
- Pseudocode
- if some_boolean then
- a lt- a 1
- else
- a lt- a 2
- b lt- 7
- endif
- Java
- if(some_boolean)
- a a 1
- else
-
- a a 2
- b 7
-
Note placement
Note Indentation is used in both cases. Means
nothing to compiler. instead of lt- (more
later) Must have parens in Java
50Questions?
51(No Transcript)