Java Reference
In-Depth Information
Object assignments
Once you create a class then you need to use objects based on that class in your code. When
you create just variables or methods with a return type, they can hold some scalar data after
assignments. It means that they can hold just one value of any data type. In contrast a class
contains a structure with many values of different data types. For example if you have cre-
ated a class with 2 class variables of different data types and one method with a return type
then this class will have a structure and can contain 3 values after assignment. Off course
this assignment will come after you create objects.
Figure 1.11: Variables and objects
Let us revisit procedural programming again. In procedural programming we have variables
and using variables we create methods. Methods in turn do some computation. Many of such
methods together do some computation which form a complete software product. When it
comes to object oriented programming, instead of variables, we have objects which we use
inside methods to do some computation. Many of such methods in turn can form a complete
software product. Please refer Figure 1.11 to understand this concept.
Now you may ask why you need to go this long way of creating classes then build objects
from these classes and then use objects to build methods which turn will help in building
the complete software product? Indeed it is a long process but it has some advantages. The
first one is we can avoid using public methods completely which are dangerous. The other
advantage is that we build a structure inside a class which becomes available to their objects.
This promotes reuse. As we have seen previously that defining space in procedural program-
ming is rather limited which is addressed in object oriented programming through providing
a larger space. This helps in having flexibility to write our code.
Search WWH ::




Custom Search