Java Reference
In-Depth Information
declaring private methods universally. You can create private methods anywhere in any
class. These methods will be accessible only inside that class but not from outside.
Figure 1.10: A method and its variables
Compare Figure 1.9 and 1.10. In Figure 1.9 we have depicted a class and Figure 1.10
we have depicted a method. In procedural programming, a method is the top level entity
whereas in object oriented programming a class is the top level entity. You can see the
difference. Classes represent a better way of managing your source code. Managing code
inside a class is a much better way. Classes provide a better way to hide your code. Your
variables at the class level can be visible from outside but you can manage them trough get
and set methods. You can also treat methods much in the same way as you can treat your
variables. You can create both private as well as public methods. We have also been dis-
cussing about public variables and the problems associated with them. We have also seen
that this problem is taken care of in object oriented programming.
Now that we have learned about classes, let us build and learn how they work. Remember
we had made a method to do computation for chocolates! We will now build a class using
the same methods to compute chocolates.
<modifier> class my_class {
Pass_chocolates(a, b)
private int x;
x = a*b;
Return x;
Chocolate_computation () {
Search WWH ::

Custom Search