Introduction to Classes and Methods

Home | Graphical Version | Table of Contents | Next Chapter, "More on the Class" | Previous Chapter, "Loops"



One of the main goals behind Java was to achieve an entirely object-oriented programming language. The concept behind object-oriented programmiong (OOP) is part of Java in the form of classes and methods. This chapter is a guide to understanding the use of objects in Java and their implementation.
  1. What are Classes?
  2. Using Classes
  3. What are Methods?
  4. Parameters
  5. Casting
  6. Overloading
  7. Return Values



What are Classes?

Good question. A class is a container which holds variables and methods. Through the manipulation of methods, you can change the values of the variables and perform tasks. For example, if you were creating a computer game, you would have a class for each sprite, or if you were making a paint program, each brush might be a class.

Using Classes

In Java, you generally give every class its own file. You must name every file after the class it contains to please the very fussy compiler. Here is a simple class declaration.

public Classname {

You can replace Classname with the name of your class. This, actually looks very similar to one of the beginning statements of the Hello World program (see the first chapter: "
Introduction to Java"). In fact, the hello world program actually is a class. The one noteable difference is the "extends Applet" part which makes the class an applet. Once you create a class, you've wasted your time unless you execute the code in the class. One way to do this is to embed you class into an web page with the applet tag, but that is only for applets. You can also have your class declared as a variable in another class. Here is an example of a class named "Sprite" declared as a variable,

Sprite MySprite;

Now, you have a cotainer for a Sprite class, but it is empty. This is like having an integer variable with no number in it. You would put a number in it with a statement like "MyInt = 5;" and this is similar for classes. To fill a variable with a new class, you must use the "new" operator. Here is an example.

MySprite = new Sprite();

This puts a new sprite class in the variable "MySprite." When you have a class in a variable, you can call the methods of the class

What are Methods

Another brilliant question. I'd be glad you asked that if you actually had asked it instead of just reading it, but I will answer the question anyway. A method accomplishes a task within the class. For example, you may have a class with a variable "temperature;" if you wanted to convert this variable from farenheit to celcius, you could have a method. You could also have a method to draw a sprite to the screen. Here is an example method declaration.

public void Draw () {
	code
}
In this lovely declaration, Draw is the name of the method, and the word "public" serves the same function as it did in the class declaration. The braces ({}), as with the braces in classes, show where the method begins and ends. Some other parts of the method decleration have gone unexplained so far, but they are explained later. If you wanted to draw your sprite, you would just use this code.

Draw();

However, this will only work if it is called inside of your sprite class. Otherwise, the computer will have no idea where to find the sprite class. This is where variables filled with classes come into play. Let us say you have a variable named MySprite with a new Sprite class in it; you can call a method with this variable. To do this, you use the dot operator (a period) on the variable. Here is an example.

MySprite.Draw();

This is an example of how you can control the flow of your program with classes and methods. Other parts of the class and method will allow you to gain even more control over your program's flow.

Parameters

Parameters allow you to give a method data to operate with. For example, let us say you are making a video game, and you have a class "Goblin." You could outfit this goblin with several important methods such as "attack" or "jump." Now, you don't want your goblin to always attack with exactly the same power and always jump exactly the same height. You can set power or height with parameters. Here is an example.

public void Attack(int Power) {

The "Power" is a parameter that must be passed to the method when it is called. Here is an example.

Attack(5);

This will call the "Attack" method and give the "Power" parameter a value of five. If you wanted your goblin to attack again with a power of only four, you would just use the same command, but substitute "4." You may have noticed that "int" has popped up again. When declaring methods, all parameters must have a data type. Passing a parameter that does not fit into the data type of the parameter causes an error. The statement "Attack(5.6);" would cause an error because 5.6 is not an integer.
You can also have subs with multiple parameters. Here is an example.

public void Attack(int Power, float Angle) {

To attack, you now must call the method like this.

Atack(5, 2.3);

You may use any numbers you want for the parameters, as long as they are within the specifications for their data-types.
Once you declare a sub with parameters, you must use those parameters in code. You use the parameters in code just like you use normal variables. Because you declare the variables in the method declaration, you don't need to declare them inside your method. Here is an example method that uses parameters.

public void Add(int a, int b) {
	int c;
	c = a + b;
}
You would call your method like this.

Add(5, 6);

This method will then create a variable named "c" and put in it the sum of "a" and "b." In this case, it creates "c" and makes it equal to 5 + 6.

Casting

If you try to pass a parameter of a type different from the one in the method declaration, Java will cause an error. In this case, you need to change the type of your parameter. You can do this with casting. A cast is a data-type in paranthesis. Placing it before a number or variable will change it to that data-type. Here is an example using the "add" method we developed earlier.

double Num1 = 2.3;
double Num2 = 3.8;
Add ((int)Num1, (int)Num2);

The two variables (Num1 and Num2) will be converted to integers. In the process, they will be rounded down to whole numbers.

Overloading

If you prefer not to always specify the angle of attack for your scary goblin, you can overload your attack method. Isn't that a mean thing to do? No, overloading is having two methods with the same name. Each method, however, must have different parameters. Java calls the method that uses the parameters you specify. For example:

public class Goblin {
	public void Attack(int Power) {
	     code
	}
	public void Attack(int Power, double Angle) {
	     code
	}
}
If you only specify one parameter, Java calls the first method, but if you specify two parameters, Java calls the second method. However, if you specify one floating point parameter, such as "6.3," the compiler will give you an error.

Return Values

Before, we made a usefull "add" method, but there was no way to find out the results of our addition. That brings us to return values and, finally, the "void" in front of every method. The "void" means that the method does not return anything. If you want your method to return something, you must specify the data type of the return value where you would normally put "void." Here is an example.

public int add(int a, int b) {
	return a + b;
}
The "return" statement tells the method what to return. Now the method acts as a value of the specified type such as "5." You can set other variables to be equal to the return value like this.

int MyVar;
MyVar = add(5, 8);

Now, "MyVar" eqauls the sum of 5 and 8, or 13. You may also use a statement like this.

MyVar = add(5, 8) * 6;

This calls the method add, returns 13 (the sum of 5 and 8), multiplies it by 6 and sets MyVar equal to the result 78.




Now that you have an understanding of object-oriented programming, you can learn more about the class in the next chapter, but first, you might want to go grab another snack.

Home | Top | Table of Contents | Next Chapter, "More on the Class" | Previous Chapter, "Loops"