|Java Working With Class|
|Written by Ian Elliot|
Page 4 of 4
A Simple Example
To see how it all works, we need an example that not only demonstrates how the Java language works to let you create classes and objects, but something that demonstrates why you might go to the trouble of using it.
Consider for the moment that you need to perform some complicated calculations. You could just write a set of functions, one for each calculation, but in many cases it would be better to group them together so that they could be used as properties and methods of an object.
For example, suppose you needed to work with circles drawn on a graphic display. Then it makes sense to create a class that encapsulate all of the things you might want to do with a circle.
Start a new project called MathExample.
Add a new class called Circle.
The basic properties of a circle are its position x,y and its radius r - so let's add these as properties to the Circle class:
They are public and so accessible from outside of the class.
The data type double is a double precision number.
We also need to use the value of the constant Pi and there is no reason why we should make this available to the outside world so it can be private:
As well as storing the size and position of the circle it would be useful to have the ability to discover the circumference and so we add a public circumference method:
In the same way it would be nice to discover the area of the circle - so lets add an area method. But to do this we need to compute r squared. Let's add a private squared function that simply returns the square of any number:
You could argue that square was sufficiently useful to make public but in the spirit of hiding how the calculations are performed we can also chose to make it private.
Finally the area function is:
The complete list for the class is:
Now let's write a main function that makes use of the class:
This starts off by creating a circle object from the class.
Notice that we have the problem of finding a suitable name for the class and the object - they can't both be called Circle.
This is a standard problem of class based object oriented languages and there are many ways around it. One is to always start a class name with an uppercase letter and use lowercase for objects.
So in our example the class is Circle and the object is circle - remember Java is case sensitive. In practice this name problem isn't as big in real life programming as it is in examples because you generally have more specific names ready to use for instances of a class - bigcircle, bluecircle, offsetcircle and so on.
After creating the circle object we set its radius and position and then use its methods to get the circumference and area which are displayed in the output window.
Notice you can create any number of circles now you have the Circle class and each one is independent of the others and will compute its own area and circumference.
This is just one of the reasons object-oriented programming is a good idea - there are many more more sophisticated reasons for using it.
Before ending this chapter, I should add that our example doesn't do things in the best possible way.
There are better ways to do things. For example, instead of using public variables as properties it is better to use methods to acces private variables. That is instead of letting users of the class directly access x,y an r we would introduce accessor functions like getX and setX. This is so standard in Java that giving direct access to member variables is almost never encountered. However when you are learning Java you need to know how things actually work. We will return to get and set methods later.
There's still a lot to learn about classes and objects, including constructors, inheritance and more.
These ideas are the subject of later chapters.