Object-Oriented Programming (in a pure OOPL)

  1. "There is more to OOP than syntax." Paul Luker paper

    The language is less important than the methodology used to analyze the problem and design the code

    but:

  2. "A pure OOPL is better than a hybrid OOPL"

    A pure OOPL, in which all data are objects is superior to a hybrid OOPL. The advantages almost invariably outweigh the disadvantages.

So, does the language really matter?

Yes, of course

But, a pure OOPL will do you little good if your analysis and design are poor. (You can write FORTRAN code in Smalltalk)

Many people say they are doing object-oriented programming since they are programming with C++. It is easy to use C++ and not be really using the OO technology.

The "OO world view"

To some extent, you can acquire this perspective through programming, but iff you use a pure OOPL.

If you have an OO design, it is natural to realize that design in an OOPL. (This is why I have the pre-requisite...so the techniques are already there!)

Goals of the course:

  1. to provide experience and good OOP practice and procedures
  2. to demonstrate the effectiveness of the Smalltalk software development environment (sorry - not in the current class)
  3. to experiment with the way cool additional features that Java provides

This course is 15 weeks short. We have a lot to cover. Have patience that we will get to everything.

Course Organization (first few weeks):

Java demos are cool and do exciting things, and we can often get the code and adapt BUT I want you to see the organization of these programs - how does one think in OO

Once we have a solid grounding into OO techniques, then we will see how Java provides tools to use these software design aspects over larger systems

(i.e., "the network is the computer")

Object-Oriented Methodology - an overview

OOA - analysis, modeling with an OO perspective

OOD - designing software based on objects

OOP - coding using objects

OO encourages iteration in the life cycle

There are various reasons for this - the main ones are

  1. close correspondence between A/D/P - manipulation of objects
  2. continuity of objects - the nature of objects leads to high
  3. maintainability and flexibility

For smaller projects, the code is the model - as the code develops, so does the model.

For larger projects, the code may still be applied early for rapid prototyping. (Smalltalk, LISP, Java are especially good for this (why? because they are robust, no need to worry about freeing or corrupting memory.))

Again remember:

"The network is the computer" - Bill Joy

Characteristics of an OOPL

Bertrand Meyer's seven steps

  1. The language must support modules

  2. Those modules must support data abstraction

    (i.e., support abstract data types)

    In order to do this, the modules must support:

    1. i) information hiding - the data is hidden from view
    2. ii) encapsulation - the only operations allowed on the data are defined in the module
    e.g.

    Dictionaries
    Data

    (hidden)

    Methods

    add(key.val)
    remove(key)
    valueof(key)
    print(key)

    Operations (interface) are called methods

    invoking a method is sending a message

    The implementation details of the Dictionary can be changed without impacting any users of the Dictionary. This is the fundamental principle on which advantages of OO depend. Violate this principle at your peril!

    (Different languages have different levels of enforcement.)

    These lead to the property of continuity.

    ADT's are supported by:

    1 Simula class
    2 LISP class
    3 Smalltalk class
    4 Modula-2 module
    5 Ada package
    6 Turbo Pascal 4&5 unit
    7 C++class
    8 Turbo Pascal 5.5+ class
    9 Java class

  3. The language must support automatic memory management. When objects are created memory allocation should be painless. Similarly, when objects are no longer needed, memory should be deallocated easily.

    (Different languages take different approaches.

    C++ has static and dynamic (de)allocation.

    Smalltalk, LISP, Java have dynamic allocation and garbage collection.

    Much easier and safer. Some overhead.)

  4. All data are defined by ADT modules, which are treated as types. I.E., the module is a type definition - this is what is usually meant by a class. e.g. class Dictionary .... ;

    Dictionary webster, oxford;

  5. It should be possible to define new classes as extensions or restrictions of existing classes. This is inheritence.

  6. There should be support for polymorphism and dynamic binding. Polymorphism is where the same name is used (deliberately!) for different, but related, methods in different objects.

    For example, we will have a number of data structure classes, all of which would support a method such as PrintAll. Polymorphism enables all objects to be treated in the same way, without having to worry about the 'type' of object. If I have a variable aStructure, then if PrintAll is (polymorphically) redefined in all structures, I can send that message to aStructure, whatever object it represents.

    Dynamic binding is where the binding of a method to a message call takes place at runtime. Clearly, if polymorphism is to be effective, dynamic binding is essential.

  7. Multiple Inheritence, which enables a new class to be created from more than one existing class. This is a problematic feature (supported in C++, Flavors, some others, not Smalltalk or Java).

The Nature of the Object-Oriented Programming Language

All OOPLs support 1-6 to varying degrees (some support 7.) The degree of support for some of these features depends on the nature of the language. Two things in particular are important: purity and typing.

Pure vs Hybrid

In a pure OOPL all data are defined by classes. In a hybrid (such as Simula, C++, and Java) some data are pre-defined types (e.g., int double []) while others are created from classes.

The pure OOPL can therefore treat all data in a uniform way (and maximize the application of polymorphism)

e.g., In Smalltalk (pure) all numbers are defined by classes. Adding a new number type (such as Complex) enables Complex to be treated in the same way as the existing numbers.

In C++ (hybrid) Complex can only be added as a class.

int, double , etc. are not objects.

Java: boolean character int double float long

All have object "wrappers" because Java's utility classes require the use of objects. Note convention Long, long

Aside: Arrays in Java are a mystery to me. What are they? Look like classes but not in hierarchy... (pg. 231 says is a class)

Strongly vs Weakly Typed

If a language is strongly typed, then the compiler will want to bind objects and methods statically. This undercuts the effectiveness of polymorphism. (C++ has to provide a workaround for this(virtual functions).) Also in a strongly typed language, overloading is necessary. (Briefly, overloading, which is related to polymorphism, is where a method is given different meanings determined by argument type.)

Smalltalk and Java are weakly typed (effectively untyped). These problems do not arise.


No smalltalk in this course anymore. Below not used

Learning Smalltalk

Remember, Smalltalk is different.

Functionality comes from pre-defined classes. Learning Smalltalk requires learning a substantial part of the class library. The best way to learn that is by focusing on subtrees of the hierarchy and walking through the code using the

*Class Hierarchy Browser*

The basic syntax does not take long to learn.

The existing classes can be used to construct new classes in several ways:

  1. as examples: use existing classes as models for completely new classes
  2. as components of new classes (aggregation) (the component is hidden within the new class (apartof)) body engine car electrical mechanical
  3. as superclasses for new classes (using inheritance)
    • Computer
    • Mainframe
    • Mini
    • PC
    Intel

    Smalltalk: Getting Started - The Tutorial

    Go through Tutorial - will discuss next few classes

    This environment has many many tools. It takes a while to become accustomed. Play with them a lot to familiarize yourself

    1. off-site can create own working image if sys ads let you have the space. Here, we will need to save individual classes

    2. note especially, Tools and Browse (also iconified)

    3. Workspaces

    4. Browse the Class Library
      1. System Browser
        categories and protocol views are organizational
        categories for classes, protocols for methods
      2. Class Hierarchy Browser
        Choose: Browse, Choose: Class Named nameofclass

    5. File Browser

    6. Help (Online Documentation)

      Documentation with running examples is online (Cookbook)

    Note the use of Sample Applications. Note the sentence: "This is where you can examine the example code in context."

    Here is an interesting and important concept about object-oriented programming.

    Remember the main idea is re-use? In object-oriented environments you are given the code and are expected to adapt it to your needs. The idea is to no longer keep having everyone re-invent the wheel. Remember that one of the great things about humans is that we make tools to make our lives easier. Objects in object-oriented programming are our new tools.

    Learn the environment. Go through Chapter 2 in the tutorial a couple of times. Become familiar with the tools of the environment (Workspace, Browsers, Help) and the tools for programming (the classes).