[CSUSB] >>
[CompSci] >>
/u/faculty/dick/cs320/sebesta/12
Notes on Object Oriented Programming and Sebesta Chapter 12
Learn definitions of words for talking about Objects
abstract
message
method
class
object
protocol
parent
derived
implements (as in ADTs/chapter 10)
inheritance
interface
field
final
Functional programming organizes your thinking one way:
data data
\ /
function
/ \
data data
Structured Programming tends to be like this
function
-----------------
| function |
| function |
| ----------- |
| | function | |
| | function | |
| ----------- |
| function |
-----------------
Data abstraction makes it possible to think like this:
operation operation
\ /
data
/ \
operation operation
However data abstraction alone does not take advantage of a simple
fact: different data often have similar operations.
+ -
\ /
int
/ \
* /
and
+ -
\ /
double
/ \
* /
These operations even have some common assumptions about them
0*x=0 for instance.
Example
In C six data types are numeric: int, double, char, long, short, and float.
If had to describe then using data abstraction only you
would end up writing the same things 6 different times.
When we teach this... we explain int and float. And then say that
long and short (and char) work the same way. Different implementations
of one common abstraction.
It would be nice to able to add a new arithmetic type that
has the same properties and functions.
Inheritance solves this problem.
We can inherit operations from previously defined classes.
+ -
\ /
Numeric_Data_Types
/ /_\ \
* | /
|
-------------------------------------
| | | | | |
| | | | | |
float char int double long short <-- all have +,-,*,/
Each is a special kind of Numeric_Data_Type.
When class A inherits form class B and object a is in class A
then it inherits all the properties of a B object as well. The
compiler can therefore allow you to do B operations on `a`.
Further if we have a pointer that points at a B we can also
apply B operations to it. Further we can allow B pointers to point
at A objects... but only allow operations listed in B to be done
to it. This will be safe as long as A only adds operations and attributes
to B.
But hat happens if A redeclares one of B's operations?
The compiler may not know that the pointer points at an A
at one point in the program and B at another....
The static type of the object is B but the actual type
is dynamic and may be an A.
Object-oriented programming also allows the use the dynamic class
of an object to determine the implementation at run time.
In Ada 83 and SIMULA the inheritance is static ( Set up by the compiler).
In Smalltalk and CLOS(Common LISP Object System)
inheritance is dynamic and so sorted out on the fly by an interpreter.
In Java inheritance is also dynamic but some is handled by the compiler
to save time.
In Ada 95 and C++ the inheritance is either static or dynamic as we need.
The inheritance structures: (What inherits from what)
Java, Ada 83 Static
Smalltalk, CLOS Dynamic
Ada 95, C++ Either Static or Dynamic
Java has two forms of inheritance in its class declarations: extension and
implementation:
http://www.csci.csusb.edu/dick/samples/java.syntax.html#Class_Declaration
One idea is that of extending an existing class:
class Number extends Object...
class Integer extends Number...
class Double extends Number...
See
http://www.csci.csusb.edu/dick/samples/java.syntax.html#Class_Extension
Here all the properties, attributes, fields, methods, etc of the extended
class also belong to the derived/extended class.
Java also distinguishes the inheritance of the complete structure of
an object from the implementation of an ADT or `interface`
http://www.csci.csusb.edu/dick/samples/java.syntax.html#Implements
12.2
You have a choice of Smalltalk or Java.... or even a bit of both in your
lab notes.
Sebesta talks about Smalltalk but covers the right concepts for Java.
However the syntax and semantics of Java are different to Smalltalk.
A later section has more on Java.
I have translated his SmallTalk into Java
http://www.csci.csusb.edu/dick/cs320/sebesta/12.java
If you want to learn Smalltalk try my
http://www.csci.csusb.edu/dick/cs320/smalltalk/
before Sebesta section 12.3, 12.4.
Bad news: We don't have the stuff in 12.4.11.
Bear this in mind:
In Smalltalk every thing you input is parsed and stored as an object.
EVERYTHING.
Including bits of code.
All objects are accessed and manipulated the same way.
Objects in C++
Inheritance
class new_class:mode parent_class
{
new_stuff
};
Compare with Java:
public class new_class extends parent_class{
{
new_stuff
}
DANGER: C++ defaults to static resolution of member functions.
Thus a pointer to an object will not behave like the object does.
The reserved word `virtual` fixes this problem.
LISP Objects
There is now a moderately standard object oriented LISP called CLOS. Our
LISP was specifically created to experiment with objects. See the directory
http://www.csci.csusb.edu/dick/cs320/lisp/
Also look for *.lsp files in
http://www.csci.csusb.edu/dick/cs320/stacks
http://www.csci.csusb.edu/dick/cs320/sieve
http://www.csci.csusb.edu/dick/cs320/stats
Java
Java's syntax is close to C most of the time
http://www.csci.csusb.edu/dick/samples/java.syntax.html
Java's semantics are also closer to C++ than Smalltalk.
For Java examples and notes see
http://csci.csusb.edu/public/faculty/dick/
http://www.csci.csusb.edu/dick/cs320/java/
Smalltalk vs C++ vs ???
Examples are in these directories
http://www.csci.csusb.edu/dick/cs320/stacks
http://www.csci.csusb.edu/dick/cs320/sieve
http://www.csci.csusb.edu/dick/cs320/stats
Also read the following rather rude messages from Usenet:
http://www.csci.csusb.edu/dick/cs320/c++/or.smalltalk
and http://www.csci.csusb.edu/dick/cs320/smalltalk/or.c++
Both Java and Smalltalk give you a lot of useful classes
to use and extend. This make application development easier in them
once you've mastered the libraries.
So why is C++ so successful?
(1) It grows out of and improves a popular language
(2) It is free
(3) It is available on all platforms.
(4) It runs faster
(5) It has better opportunities for bugs
(wouldn't you be bored if all your programs were simple?)
A possible strategy:
Prototype your classes in Smalltalk/Java
Translate into C++
Put all developed Classes into your personal library of C++ goodies.