CSE5910 : Multimedia Programming in Java

Lecture : Overview of Object Oriented Programming Concepts

In the previous lecture:

In this lecture:



Object Oriented Programming

A method for analysing needs, designing solutions and programming software

Build systems from co-operating collections of "objects"

Objects communicate by passing "messages" that other objects "react" to

Objects are organized into "classes". All objects in a class have the same behaviour (but may store different data)

Classes are organized into "abstraction hierarchies"

Why use it? ...because an OO approach decomposes the problem into manageable and "intuitive" components

The components of this model are Abstraction, Encapsulation, Modularity, Hierarchy, Typing and Genericity


Abstraction

To allow us to understand our complex surroundings and react to them in a timely manner we "chunk" things by identifying significant commonalities (whilst ignoring trivial differences)...

We can then make "classes" of objects depending on what is significant to us at a particular time.

unclassified things
the same things classified as organisms, mammals, humans
the same things again classified as organisms, mammals, dangerous mammals
unclassified "things"
organisms, mammals, humans
organisms, mammals, dangerous mammals

The selection of significant attributes (to allow us to classify the items that possess them) is abstraction.

We can abstract by appearance, structure, purpose, functionality, privilege etc.

When OO programming we specify classes that will form the elements of our programs. When the program is executed it makes software objects that are instances of these classes and allows them to interact with one another (by passing messages) to solve the problems that we set.

 

Abstraction Hierachies & Inheritance

abstraction hierachy
  • Abstractions that we make often form a hierarchy

  • The relationship between levels when going up the hierarchy is known as "is a"

  • Two different "child" classes may share the same "parent" class

  • We say that a member of a class "is an instance of" that class

 

A child in an abstraction hierachy inherits the behaviours and properties of its parent.

A child may also have additional unique behaviours and properties of its own that its parent does not have.

For example (in the diagram above), an Animal (a child of Alive) inherits all the properties of being Alive (the parent of Animal). A Human inherits all the properties of being an Animal and therefore also all the properties of being Alive.

A Human has some extra properties that allow us to classify it as a Human that are not necessarily true of all things that are Animals or all things that are Alive. For instance normal Humans always have two legs. Other Animals (like mice) may have a different number of legs. Animals can move around by themselves. Other things that are Alive (like trees) may not be able to move around by themselves.

When OO programming we aim to build hierachies of the classes of objects that our programs use. This way we build an intuitive system for the classification of our software objects and we can re-use the properties and behaviours of parent classes in their children. This helps make the code easy to understand, easy to debug and simplifies the task of writing it well in the first place since we can re-use common elements.


Encapsulation

Hiding things inside barriers (like walls, your skin, plastic boxes, envelopes) to keep them (i) neatly together and (ii) away from disruptive external influences is encapsulation.

Encapsulated systems can have "public interfaces" that allow meddling in controlled ways from outside.

To stay safe, don't show the outside world your sensitive inner workings if it doesn't need to know about them!

Your car's engine is inside the bonnet so that you don't get your fingers burnt on the hot metal or stuck in the fan. The car's body also keeps out rain and mud so that you don't get dirty and wet while you are driving it. The engine keeps oil on the cylinders and contains the regular explosions of burning fuel so that their power can be harnessed. You can still control the car by sitting inside and operating the steering wheel, brake, clutch and accelerator. The car's body also encapsulates all the parts so that it can move in one piece!
Your skin keeps out bacteria and dirt. It encapsulates your organs, lots of water, vitamins and minerals and keeps them all in the correct place with respect to one another so that you work properly. People can still interact with one another and the world by touching, smelling and tasting or by visual and aural signals. Food can enter and waste products can leave the body through well-made entry and exit points.
Your computer is in a case so that you don't fiddle with the wiring and circuit boards, so that you don't get an electric shock and so that dirt and dust doesn't get inside to stop it working properly. But you can still use it by operating the keyboard, mouse and buttons. The case also encapsulates all the parts so that you can move it around.

When OO programming encapsulation allows us to specify complex classes so that our software objects' behaviours can be controlled from outside in well-defined ways, without the worry that something from outside the object will muddle up its inside workings. Encapsulation also ensures that all the parts that the software objects need to correctly do their job are kept neatly together.


Typing

In computer programming typing is the enforcement of class distinctions.

E.g. Some languages insist that when doing assignments of a variable to a value, the data-types on either side of the assignment operator must be compatible.

integer myInteger = 1; // OK in most languages because left & right sides are both int's

myInteger = "Hello"; // Error in most languages because left & right types don't match

E.g. Some languages insist that parameters to functions match the type declared in the function header.

integer myTripleFunction (integer myLocalInteger)
{return myLocalInteger * 3; }

myFunction (myInteger); // OK

myFunction ("Hello"); // Probably not OK

Whether or not a given expression is acceptable in a given programming language depends on:

The rules for that language;

How strongly the rules are adhered to:

 

Research in your study time the meaning of the terms static typing and dynamic typing.


Genericity

Abstraction of structure independently of type is genericity (pronounced jen-err-iss-it-ee).

E.g. Compare these two functions to return the maximum of two numbers.

integer maxInteger (integer param1, integer param2)
{
if (param1 > param2)
{ return param1; }
return param2;
}
float maxFloat (float param1, float param2)
{
     if (param1 > param2)
     { return param1; }
     
     return param2;
}

It seems silly to have to write the two functions but if a language doesn't allow you to pass an integer to a function that requires a floating point parameter (or vice versa) then this is exactly what you must do.

Some languages allow you to to write a single generic function that will serve the same purpose for both integers and floating point values (or any other types you like for that matter). In general, such a function might look a little like this.

TYPE max (TYPE param1, TYPE param2)
{
if (param1 > param2)
{ return param1; }
return param2;
}

In this case we have maintained the common structure of the "max" functions and abstracted away the differences in their type information to produce a general function that can serve the purposes of both maxInteger() and maxFloat(). In this case we are depending on the overloading of the ">" operator to work correctly for integers and floating point variables alike.

The word TYPE is set up by the programmer to represent different data types. The way this is achieved is not important right now but if you are interested you can investigate C++ templates as one example. Not all languages provide this facility but it is important that you understand the concept as it is a component of programming you will come across.



Lecture summary:

Object Oriented Programming involves breaking problems up into manageable components. These objects solve problems by sending messages to one another. Objects are of types defined by a class hierachy that specifies their relationships to one

Abstraction, Encapsulation, Modularity, Hierarchy, Typing and Genericity



CSE5910 Courseware | CSE5910 Lecture Notes