This chapter explains the basic concepts behind object-oriented programming, design and also
the development.
A D V E R T I S E M E N T
What is Object?
The real-world objects(any living or non- living thing) share two characteristics:
they do have state and behaviour. For example, dogs have a state ( like name, color, etc)
and behaviour ( like barking, fetching etc). Also consider for example a
Bicycles have a state such as current gear, current wheels, number of gears etc and
and behaviour such as braking, accelerating etc.
After the real-world objects Software objects have been modelled in that, they too,have
states and behaviours.
A software object maintains states in the variables and implements behaviour by methods.
What is Encapsulation?
Packing up an object's variables within its methods is called encapsulation.
Encapsulating the related variables and methods into neat software bundle seems simple
but is a powerful idea that provides two benefits to the software developer:
Modular programming-- The source code for any of the object can be written and maintained
independently from those of the source code written for other objects. Also, an object can be
easily used passing around in the system. For example you can give your bicycle to someone else
and it will still work for them too.
Information hiding-- An object has a public interface by which other objects can
communicate with it. But on the other hand objects can maintain private information and methods
that cannot be communicated to others.
What are Messages for?
Using "messages" software objects do interact and communicate with each other. When an object say
A wants another object say B to perform one of its methods, then object A can send
message to object B.
There are three components comprising a message:
Following three components are enough for the receiving object to perform
the desired method. No other information or context is required. Thus, objects in
different processes or even on different machines can communicate to each other
through the use of messages.
the object to whom the message is addressed (bicycle)
the name of the method to perform (change gears)
any parameters needed by the method.
The Benefit of Messages
Since everything that an object can do is expressed through its methods, message
passing supports all possible interactions between objects.
To send and receive messages back and forth objects don't need to be in the same
process or even on the same machine.
What are Classes?
In object-oriented software, it's also possible to have many objects of the same
kind that share characteristics: rectangles, employee records, video clips and so on.
Like the bicycle manufacturers, you can take advantage of the fact that objects of
the same kind share certain characteristics and you can create a blueprint for those
objects. Software blueprints for objects are called classes.
The Benefit of Classes
Objects provide the benefit of modularity and information hiding. Classes provide the
benefit of reusability. Bicycle manufacturers reuse the same blueprint over and over
again to build lots of bicycles. Software programmers use the same class over and
over to again create many objects.
What is Inheritance?
Generally speaking, objects are defined in terms of classes. You know a lot about an
object by knowing its class. Even if you don't know what a penny-farthing is, if I
told you it was a bicycle, you would know that it had two wheels, handle bars and
pedals.
Object-oriented systems take this a step further and allow classes to be defined in
terms of other classes. For example, mountain bikes, race bikes and tandems are all
different kinds of bicycles. In object-oriented terminology, mountain bikes, race
bikes and tandems are all subclasses of the bicycle class. Similarly, the bicycle
class is the superclass of mountain bikes, race bikes and tandems.
The Benefit of Inheritance
Subclasses provide specialized behaviours from the basis of common elements provided by
the superclass. Through the use of inheritance, programmers can reuse the code in the
superclass many times.
Programmers can implement superclasses that define "generic" behaviours (called abstract
classes). The essence of the superclass is defined and may be partially implemented but much of
the class is left undefined and unimplemented. Other programmers fill in the details with
specialized subclasses.