Q. What is an Object?
Ans:- Objects are software bundles of data and
related procedures.
Software objects
are often used to model real-world objects you find in everyday life. As the
name implies, objects are the key concept in understanding
object-oriented technology.
You can look
around you now and see many examples of real-world objects: your dog, your
desk, your television set or your bicycle.
These real-world
objects share two characteristics: they all have state and they all
have behavior. For example, dogs have state (name, color, breed,
hunger) and, behavior (barking and
fetching). Bicycles
have state
(current gear, current pedal cadence, two wheels, number of gears) and behavior
(braking, accelerating, slowing down and changing gears).
Software objects
are modeled after real-world objects in that, they too, have state and
behavior. A software object maintains its state in variables and implements its
behavior with methods.
You can represent
real-world objects in programs using software objects, consider an example, you
may represent dogs or bicycle as software objects in an animation program.
Q. Explain the Term Encapsulation.
Ans:- Everything that
the software object knows (state) and can do (behavior) is expressed by the
variables and methods within that object. A software object that model your
real-world bicycle will have variables that indicates the bicycle's current
state: its speed is 25 kmph, its pedal cadence is 900 rpm, and its current gear
is the 3rd gear. These variables and methods are formally known as instance
variables and instance methods, to distinguish them from class variables and
class methods. The software bicycle will also have methods to brake, change the
pedal cadence and change gear(The bike would not have a method for changing the
speed of the bicycle as the bike’s speed is really just a side-effect of what
gear it’s in, how fast the rider is pedaling and how steep the hill is.)
Anything that an
object does not know or cannot do is excluded from the object. For example,
your bicycle(probably) doesn’t have a name, and it can’t run, bark of fetch.
Thus there are no variables or methods for those states and behaviors. object's
variables make up the center or nucleus of the object and the methods surround
and hide the object's nucleus from other objects in the program. Packaging an
object's variables within the protective custody of its methods is called encapsulation. Typically, encapsulation is used to hide
unimportant implementation details from other objects. When you want to change
gears on your bicycle, you don't need to know how the gear mechanism works, you
just need to know which lever to move. Thus, the implementation can change at
any time without changing other parts of the program.
The Benefits of
Encapsulation
Encapsulating
related variables and methods into a neat software bundle is a simple yet
powerful idea that provides two primary benefits to software developers:
·
Modularity--the source
code for an object can be written and maintained independently of the source
code for other objects. Also, an object can be easily passed around in the
system. You can give your bicycle to someone else and it will still work.
·
Information hiding—Through
information hiding, the complicity of the system can be reduced by not exposing
unnecessary data. For example, you don’t need to understand the gear mechanism
on your bike in order to use it.
Q. What Are Messages?
Ans:- Software objects
interact and communicate with each other via messages. A single object
alone is not very useful and usually appears as a single component of a larger
program or application that contains many other objects. It is through the
interaction of these objects that programmers achieve higher order
functionality and more complex behavior. Your bicycle hanging from a hook in
the garage is just a bunch of titanium alloy and rubber; by itself the bicycle
is incapable of any activity. The bicycle is useful only when another object (you)
interact with it (starts pedaling).
Software objects
interact and communicate with each other by sending messages to each
other. When object A wants object B to perform one of its methods, object A
sends a message to object B.
Sometimes the
receiving object needs more information so that it knows exactly what to
do--for example, when you want to change gears on your bicycle, you have to
indicate which gear you want. This information is passed along with the message
as parameters.
Three components
comprise a message:
·
the
object to whom the message is addressed (bicycle)
·
the
name of the method to be performed (changing gears) any parameters needed by
the method (to a higher gear)
These three components provide enough
information for the receiving object to perform the desired method. No other
information or context is required.
The Benefits of Messages
·
Everything
an object can do is expressed through its methods, so message passing supports
all possible interactions between objects.
·
Objects
don't need to be in the same process or even on the same machine to send and
receive messages back and forth to each other.
What are Classes?
A class
is a blueprint or prototype that defines the variables and the methods common
to all objects of a certain kind.
In the
real world, you often have many objects of the same kind. For example, your
bicycle is really just one of the many bicycles in the world. Using
object-oriented terminology, we say that your bicycle object is an instance
of the class of objects known as bicycles. All bicycles have some state
(current gear, current cadence, two wheels) and behavior (changing gears,
braking) in common. However, each bicycle's state is independent of and can be
different from other bicycles. When
producing bicycles, manufacturers take advantage of the fact that bicycles
share characteristics and they build many bicycles from the same blueprint--it
would be very inefficient to produce a new blueprint for every individual
bicycle manufactured.
Like the bicycle manufacturers,
you can take advantage of the fact that objects of the same kind are similar
and you can create a blueprint for those objects. Software
"blueprints" for objects are called classes. For example,
you could create the bicycle class that would declare several variables to
contain the current gear, the current cadence, etc. It would also declare and
provide implementations for the methods that allow the rider to change gears,
brake and change the pedaling cadence.
The values for
the variables are provided by each instance of the class. So, after you've
created the bicycle class, you must instantiate it (create an instance
of it) before you can use it. When you create an instance of a class, the
variables declared by the class are allocated space in the memory. You can use
the instance's methods to assign values to the variables. Instances of the same
class share method implementations.
Q. What do you mean by the Term "Object"?
Ans:- You have
probably noticed that the illustrations of objects and classes look very
similar to one another. And indeed, the differentiation between classes and
objects is often the source of some confusion. In the real world it’s obvious
that classes are not themselves the objects that they describe. A blueprint of
a bicycle is not a bicycle. However, it’s a little difficult to differentiate
classes and objects in software. This is partially because software objects are
merely electronic models of real-world objects or abstract concepts in the
first place. Then also occurs because many people use the term “object”
inconsistently and use it to refer to both classes and instances.
The main
difference between a class and an object is that objects are tangible, but a
class is always intangible. You can’t see a class but you can always see an
object. In Figure 1.5 and Figure 1.6, a class’s methods and variables are not
shaded because they don’t exist yet. You must create an instance from the class
before you can call the methods and before the variables can have any values. In
comparison, an object’s methods and variables are shaded indicating that the
object actually exists and you can use it. You can send the object a message
and it will respond by performing the method and perhaps modifying the values
of the variables.
The Benefits 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 again to create many objects.
No comments:
Post a Comment