Java Reference
In-Depth Information
Object-oriented Programming
Object-oriented programming is often used to model representations of objects in the real
world. There are three main concepts in OOP: encapsulation , polymorphism , and inher-
itance . I'm going to use my juicer to illustrate how each of these concepts can be applied
in a programming environment, since the juicer can be considered an object. It's a wonder-
ful machine that makes fresh juice for me every morning. The juicer has properties such as
speed and capacity, and also has methods or actions it can perform, such as juicing, switch-
ing on, and switching off.
When I use my juicer, I put the fruit into the machine, press the “on” button and out comes
the juice. I haven't a clue how it does it—only that it makes a very loud noise! This demon-
strates the concept of encapsulation : the inner workings are kept hidden inside the object
and only the essential functionalities are exposed to the end user, such as the “on” button. In
OOP, this involves keeping all the programming logic inside an object and making methods
available to implement the functionality, without the outside world needing to know how it's
My juicer uses the same plug socket as other appliances in my kitchen. I can also place vari-
ous types of fruit into it and it still juices them. These two examples demonstrate the concept
of polymorphism : the same process can be used for alternative objects. In OOP, this means
that different objects can have the same method, but they implement it in their own way.
I'd really like the next model up from my juicer, as it can deal with more types of fruit and
it's a bit quieter. Even though it has these extra features, I'm sure that inside it uses many
of the same parts that my juicer has. This demonstrates the concept of inheritance : taking
the features of one object and then adding some new features. In OOP, this means that we
can take an object that already exists and inherit all its properties and methods. We can then
improve on its functionality by adding new properties and methods.
Search WWH ::

Custom Search