Posted by: Ibrahim Faisal | May 4, 2008

Some questions on OO

What is Object Oriented Programming (OOP)?

Traditional programming languages (C, COBOL, FORTRAN…) follow a
structured programming model. In this model, data is separately stored
and procedural code processes data.

For example, in C, data is stored in structures and processing is done
by functions.

When used on large projects, this type of programming revealed several
weaknesses:
1. Not modular: Any of the data is accessible from any piece of code.
This made it difficult to demarcate the areas for different teams
working on a large project.
2. Not easy to fix: When we go back and make a change in one part of the
program, it is difficult to foresee and contain its impact from other
parts of the program.
3. Not enough code reuse: Even though function and subroutine libraries
were built to promote reuse of code, it was obvious that a way was
needed to reuse larger chunks of code.

OOP came in to solve some of the above problems. OOP bundles data and
the processes which act on that data into a single unit called an
object. Objects have properties to store data and methods to process
that data, all bundled into a single unit.

Think of structured programming as a centralized library where any
employee can get and replace books in any section. OO is like a
decentralized library where one employee is in charge of ‘fiction’,
another is in charge of ‘arts’ and so on. If employees in charge of one
section want a book from another, they can’t just go and grab it, they
have to ask.

What are the benefits of OOP?

Encapsulation: Objects hold together well demarcated units of
functionality. They hold all data and processing pertaining to that
functionality together. Objects are accessed through their public
interfaces by sending messages. No need to read all the implementation
code to check usability.

Code reuse: In structured programming only processing can be reused. In
OOP, inheritance allows us to reuse processing and data structures
bundled together.

Quality: Whenever we reuse code, quality improves. This is because
writing new code has potential for additional bugs. Reusing existing,
tested code minimizes the chances of additional bugs.

Maintainability: Implementation details are isolated from the interface.
Implementation code can be changed without any fear of breaking other
objects that access it. Because of inheritance, changes made to an
ancestor spread to all its descendants automatically. Because related
code and data is held together, it is easier for the maintenance people
to find what they are looking for in one place.

What are the origins of OOP?

The SIMULA programming language was the first OO language. It had all
the key ingredients of OOP, such as, objects, classes, inheritance, and
dynamic binding. It was created primarily to model or simulate real
world objects. It was designed and built by Ole-Johan Dahl and Kristen
Nygaard at the Norwegian Computing Centre in Oslo between 1962 and 1967 .

What are popular OO Languages and how did they originate?

At present, SmallTalk, C++ and Java are the OO languages in use by large
number of programmers.

Smalltalk: It originated in the 1970’s at the Xerox Research Center in
Palo Alto, California, USA. Alan Kay was the primary visionary who
borrowed ideas from Simula and LOGO as the basis of what was to be the
programming language of a portable, computerized notebook called the
Dynabook (at a time when computers were the size of a room!). After
several iterations, Smalltalk-80 was eventually created, in the early
1980’s.

C++: AT&T Bell Labs, New Jersey, USA is where C had been developed in
the 1970’s. C is flexible yet powerful, and it had been used to create
some of the most important software products of that decade. However, in
order to overcome some limitations of C while writing very large
programs, Bjarne Stroustrup, a computer scientist there, developed “C
with Classes” in 1980. According to him, his objective was to combine
C’s strengths as a systems programming language with Simula’s facilities
for program organizations. It was later renamed “C++” in 1983.

Java: In 1990, Sun Microsystems began a project to develop software for
use in consumer electronic devices such as, Personal Digital
Assistants(PDAs), VCRs and toasters. James Gosling began writing
software using C++ , but he soon realized that he needed a language that
is hardware independent (allowing the manufacturer to change chips) and
less susceptible to crashes. Gosling came up with a new language called
Oak. Oak retained the familiar syntax of C++, but dropped some of the
error-prone features of C++ such as pointers. Removal of unused objects
from memory (garbage collection) was added directly into the language.
In 1995 Oak was renamed Java, for marketing purposes.

Other OO languages are: Eiffel, Objective C, Ada 95,…etc.

Delphi (from Inprise) is a 4-GL tool which is based on Object Pascal,
which is a OO language. PowerBuilder (from Sybase) is a 4-GL tool which
has a lot of OO functionality.

What is an object?

An object is a piece of code which combines in a single unit both data
and the processes that operate on that data. Such a unit is called an
object. Other objects interact with it by sending messages to it and,
possibly, getting back results. Property or attribute is the term used
to refer to data stored in an object. Method or member function is the
term used to refer to the processing code.

What is a class?

A class is also a piece of code which is like a recipe. Just as you can
bake several cakes from the same recipe, you can create several
instances of an object from its class.

For example, you may create an object called cake from ‘my favorite cake
recipe’. The ‘my favorite cake recipe’ class would have data about
ingredients, gadgets… and so on. It will also have methods for mixing,
baking…etc.

What is an instance?

The terms instance and object mean the same thing.

What is inheritance?

Inheritance allows you take advantage of the commonality of objects in
modeling and constructing large software programs. Inheritance also
makes possible code and structural reuse.

Let us say your analysis of a software project shows that you will need
three similar objects. In OOP, you will create an ancestor with what is
common among those three objects. You will then inherit from this and
make some small changes to create the three descendants.

Supposing you already had a ‘my favorite cake recipe’ class, which you
know gives good results. If you wanted a different topping, will you
start from scratch? No, you will bake the cake following your tested ‘my
favorite cake recipe’ and as a last step finish it with the needed
topping. Thus using a known class to create variations is called
inheritance.

What is encapsulation?

When you write the code for an object, you want to make sure that other
objects can call and send messages to it. So, you will want to make sure
its interface is accessible to other objects. However you will also want
to make sure that its implementation details are not accessible to other
objects. This gives you a free hand to make changes to the
implementation details later without worrying about breaking the code in
other objects that call it. This process is known as encapsulation. OO
languages help you do this by letting you declare properties and methods
as private, protected or public.

For example, when you go to the supermarket to buy a replacement
electrical plug for a home appliance, for a given rating your main
concern is that it should fit the wall socket. With the same size, shape
and spacing of the plugs, the manufacturer is free to make changes or
improvements inside. You need not know nor care what those details are.

What is polymorphism?

Polymorphism (“poly” means “many” and “morph” means “form”) is the
ability of an object to to take different forms. To be more precise, it
is the ability of a reference to an object to refer to different objects
at runtime. Without this feature programs will have large switch (or
case) statements to handle variety of objects. Also, when more such
objects are added, this switch statement has to be extended to
accommodate them. Thus polymorphism lets you write generic code that
will work not only for a range of existing objects, but also for objects
to be added later.

This is typically accomplished through inheritance of classes. (Java
makes use of inheritance of interfaces to provide polymorphism.) A name
may denote objects of many different classes that are related by some
common ancestor. Thus any object denoted by this name is able to respond
to some common set of operations in different ways. Polymorphism is
often considered the most powerful feature of an OOP language.

To take a general example, let us say that a bank sends a letter
offering loans at low interest rates with the address “The current
resident” at a given address. Whoever is occupying that address at the
moment could receive that letter and respond to it. One resident may
apply for a car loan, while another may need a business loan… and so on.

To take a programming example, let us say that you are writing a program
that deals with polygons. Your triange class can have its own
triangleArea method, your rectangle class can have its own rectangleArea
method and your pentagon class can have its own pentagonArea method. If
you want to let the user pick a shape and get the area, you need to have
a switch (or case) statement. This is the non-polymorphic way. With
polymorphism, you will have a single method ‘area’ in each of these
classes, however, with their own implementation code. The object will be
switched at runtime, depending on user selection, so that the
appropriate method is called.

What are properties or attributes?

Data stored by each object is known as its property or attribute.

In ‘my favorite cake recipe’ class, the quantity of baking soda, for
example, is an attribute or property.

What are methods or member functions?

Processing code in an object is known as its method or member function.
Some people also call this behaviour.

In ‘my favorite cake recipe’ class, the mixing procedure is a method or
member function.

What is multiple inheritance?

Multiple inheritance occurs when a class inherits from more than one parent.

For example, if you need a watch-cum-calculator, you can inherit from a
watch and a calculator so that you get both functionalities.

If inheritance is good, multiple inheritance should be great, right?

Not necessarily. With single inheritance, whenever you refer to any
attribute or method name in a descendant, it can be simply looked-up
from the lowest ancestor. In a multiple inheritance hierarchy, since
several distinct parents can declare an attribute or method of the same
name, which to choose becomes a problem. This is known as name collision.

C++ declares an error if a conflict arises, but a class qualifier can be
used to explicitly clarify. Smalltalk does not support multiple
inheritance. Java does not permit multiple inheritance of classes, but
provides multiple inheritance of interfaces.

What is typing?

Each programming language provides some primitive types such as integer
and boolean. Once you define a class, it is treated as a user-defined
type by OO programming languages. You can then use it just like a
primitive type to declare a variable, store it in an array, pass to a
method… and so on. This is what is referred to as typing.

I read that Java is a strongly typed language. What does this mean?

In a strongly typed language, type checking is done at compile time.
This is very good for stability (there are less chances of errors at
runtime) and improves reliability. This is also known as static typing.
C++ and Java are examples of languages with static typing.

What is dynamic typing?

Dynamic typing allows for type checking of objects at run-time. This
makes the language more powerful by allowing better flexibility.
Smalltalk supports dynamic typing.

What is dynamic binding?

In C++, a class can have a virtual function and its descendants can
override that function by their own individual implementation code.
Wherever the ancestor object is used, you are allowed to substitute it
with its descendant object at runtime. This is known as dynamic binding,
late binding or loose coupling. This is how C++ provides polymorphism,
even though it has static typing.

What is abstraction?

Abstraction is most often used as a complexity mastering technique. When
you are doing OOA, you need to do some abstraction in order to find a
pattern of common base objects. In other words, you need to look at the
highlights without getting bogged down with the details.

*What is specialization?*

To create a subclass from a parent class is specialization.

If you have a base class ‘polygon’ and if you inherit from it to create
a descendant called ‘triangle’, that is specialization.

What is generalization?

To pull together common parts of derived classes into a common base (or
parent) is generalization.

If you find that you need three classes ‘car’, ‘truck’ and ‘bus’ and
decide that it will be better to have a common base class called
‘automobile’, then that is generalization.

What is overriding?

Overriding is the term used in C++, for example, for redefining a method
in a derived class, thus providing specialized behavior. Whenever a
method is invoked on an object of the base class, the derived class
method is executed overriding the base class method, if any.

The difference between overloaded functions and polymorphic (overridden)
functions is that with overloaded functions, the correct function to
call is determined at compile-time; with polymorphic functions the
correct function to call is determined at run-time.

What is an interface?

An object may have many attributes and methods. However most of these,
namely the implementation code, should not be accessible to other
objects. Only a few methods should be accessible to other objects, so
that they can send a message and, possibly, get a return value. These
methods are known as the interface.

What is garbage collection?

In structured programming, you had data and your functions processed
them. But in OOP, you have to create objects when needed and you have to
destroy them when done. If not done properly, this could lead to
dangling pointers, orphaned objects and memory leaks. Such problems are
hardest to debug.

Some of the OO languages, such as Java, have a built-in mechanism in the
run-time system to automatically destroy objects which are no longer
used and reclaim memory. This is known as garbage collection.

What is Object Oriented Analysis (OOA)?

In order to make the best use of an OOP’s features , you must start
early. At the time of analysis itself you must start looking for
potential objects with well-defined outlines. You must also look for
patterns to identify sets of objects with similar attributes and
methods. This is known as OOA.

What is Object Oriented Design (OOD)?

Here you will identify ways to organize your program into base classes,
inheritance structure, clearly defined interfaces… and so on. This is
known as OOD.

What are Use Cases?

Use Cases are a technique for formalizing the capture of what the
requirements of a system are and how it works. The reason for formally
capturing the scenarios is to make them available for review by both the
users and the developers. What Use Cases do is capture in an
implementation-independent manner what the needs and expectations of the
users are.

Even though it is not exclusive to OO, it is a popular technique for
documenting the user requirements before starting on the design of a system.

What is UML?

The Unified Modeling Language (UML) is emerging as the
industry-standard language for specifying, visualizing, constructing,
and documenting pieces of software systems. It simplifies the complex
process of software design, making a “blueprint” for construction. The
UML definition was led by Rational Software’s industry-leading
methodologists: Grady Booch, Ivar Jacobson, and Jim Rumbaugh.

Again, UML is not exclusive to OO, but combines several popular
modelling techniques, including Use Cases.

I am moving from C to C++. What should I do to get the benefits of OO?

OOP is more than learning a new language; it requires a new way of
thinking. When we move to OO, we must no longer think in terms of data
structures and function/subroutine libraries – we must think in terms of
objects. This is the hardest part.

Try to start early, at the time of the analysis/design itself. This will
give you an opportunity to visualize similar objects, base classes and
so on. However, if you are already into programming, look for
opportunities to specialize and generalize. If you are creating a new
class, see if you can inherit from an existing class and modify slightly
to get what you want. If you find yourself writing similar code over
again, see if you can combine them into a common base class.

What are Object Oriented Database Management Systems (OODBMS)?

OODBMS are databases that support objects and classes. They are
different from relational databases because they allow entire objects to
be stored and also support methods and inheritance. OODBMSs claim to
provide synergy when used with object-oriented programs.


Responses

  1. Many many thanks for such a wonderful and important posting.

  2. Thanks.


Leave a response

Your response:

Categories