|
Home
<<Table of Contents
<<Introduction to Classes and Methods
Java Packages>>
|
MORE ON THE CLASS
The subject of classes is much too large to have only one chapter. This second chapter is your guide to some of the more advanced concepts behind the class and Object-Oriented Programming (OOP).
- Object-Oriented Programming
- Modifiers
- Scope
- Derived Classes
- Abstract Classes
- Overriding Methods
|
| |
Object-Oriented Programming
Those who have programmed in BASIC have probably heard the term "Spaghetti Code." When all code must have only one module, parts of code can only be repeated using "GOTO" and "GOSUB" statements that render code almost totally unreadable. One of the goals of Java is to eliminate spaghetti code and be totally object-oriented. As a matter of fact, you can not use "GOTO" in Java, but it is a keyword as if the developers of Java wanted to stop it from ever being put into the language. Part of OOP is the concept of the class containing variables and methods which surround and protect the variables of the class. Other classes can change these variables through the class' methods. For example: If you have a "Goblin" class you can give it the variable "Health." Another class could assign this variable a value greater then the Goblin's maximum health thus screwing up you game. You can prevent this by forcing other classes to change the "Health" variable with one of the goblin's methods
(e.g. The class would call a method and pass the new health value to the method which would change the health variable). This allows the method to prevent the "Health" variable from being assigned an invalid value. You don't have to follow this school of thought in your programming, but no discussion of Java would be complete without the ideas of OOP. Plus, it leads us into our discussion of concepts designed to made OOP easier.
|
| |
Modifiers
How do we prevent another class from accessing the hypothetical "Health" variable in our goblin. Answer: We make the "Health" variable a private variable. This is done with the "private" modifier. Modifiers are placed before variable method, and class declarations to change the way the variables, methods and classes act. The three modifiers that affect what can access a variable are public, private, and protected. The "public" modifier is the same one you saw in front of all the previous method declarations. Here is a public variable declaration.
Now, other classes may not access the variable; only methods within the goblin class may access. Any attempt by another class to change or read the variable would result in an error when the progam compiled. The "public" modifier does the opposite. It allows all classes to access the variable. The "protected" modifier allows some classes to access the variable. This becomes important later.
Along with variables, methods can also be declared public or private. You probably noticed all methods in previous chapters were declared public. Here is a private declaration.
Now, only methods within the goblin class can call the "Attack" method.
|
| |
Scope
You can now control how other classes can access the variables inside your class, but how do methods inside of your class access variables? Every class has two types of variables - local and global variables. "Local" and "Global" are the scope of the variables. The scope is determined by where the variable is declared. If a variable is declared within a method, it is a local variable; the variable can not be accessed from outside the method. Once the method ends, the variable dies and it starts at the default value when the method is run again. When a variable is declared at the beginning of a class outside of any method, it is global and can be accessed by any method. It can also be accessed by another class if it is public. Here is an example.
public class Goblin {
private int Health = 100;
public void setHealth(int H) {
Health = H;
}
public int getHealth() {
return Health;
}
}
|
The Goblin class now has a Health variable which can be accessed by any method in the class. However, it cannot be changed by other classes unless it is changed indirectly with the getHealth method. This may seem silly, but it is useful if you want to have a restriction on the health parameter.
|
| |
Derived Classes
"Deriving" classes lets you make a classes similar to another class but change the class. This is done using the "extends" keyword. For example:
|
public class Goblin extends Monster {
|
The Goblin class now "inherits" all public and protected variables and methods of the Monster class. That brings us to the "protected" keyword. We already know what "public" and "private" mean. Protected variable can only be accessed by the class containing th variable (as with all variables) and children of the class (such as Goblin in our example). Deriving classes is most useful for creating similar but different classes. Just like our first example, you can create a generic "Monster" class and derive several different more specific monsters (like "Goblin") from it. If the parent class has a constructor, you can call the constructor with the super keyword. Like in this example which demostrates some of the functions of derived classes.
public class Monster {
private int Health;
Monster(int initHealth) {
Health = initHealth;
}
public void setHealth(int H) {
Health = H;
}
public int getHealth() {
return Health;
}
}
public class Goblin extends Monster {
Goblin(int initHealth) {
super(initHealth);
}
public AttackSelf(int Power) {
setHealth(getHealth() - Power);
}
}
|
The goblin has all of the variables and methods as the Monster class, but it has a new Goblin-specific method. As a matter of fact, any variable or parameter of type "Monster" can be filled with a "Goblin" instead. Because Goblins are not too bright, I gave this class a function to attack itself. This gives me another chance to explain the OOP concept to you. You can see that with these function the "Health" variable can reach a negative value. If this happens the goblin should be dead. You can set the "SetHealth" function to kill the goblin if its health becomes lower then zero.
|
| |
Abstract Classes
Now that we've discussed child classes, we should probably discuss parent classes. You may derive a class from any class you want, even a class derived from another class, but some classes are designed specifically to be a parent class. These classes are declared "abstract" with the abstract keyword. You may create a variable of the type of an abstract class, but you may not use the "new" operator on an abstract class. You may only fill a variable of an abstract class with a child of the abstract class. In our previous example, it would be a good idea to declare the "monster" class as abstract because it is designed as a template for other child classes. In this case, if you had a variable declared as type "Monster" you could only fill it with a "Goblin" class.
|
| |
Overriding Methods
One final note on deriving classes. It later becomes important to change the functionality of the parent class. This can be done by overridding the methods of the parent class. If, in our previous example, the "Monster" class had a "Attack" method, you could declare a method of the same name and parameters in your "Goblin" class. This way, the goblin could attack in a way that is not possible in the monster class. The goblin class would act as if the parent's "Attack" method had never existed.
|
|
Home
<<Table of Contents
<<Intoduction to Classes and Methods
Java Packages>>
|
You may have noticed that in your first programming endeavor, "Hello World" (in the first chapter "Introduction to Java") the class "HelloWorld" was derived from the "Applet" class. This is what allows the user's interactions with the applet to control your program. In short, it allows event-driven programming (yet another buzzword) which is the subject of our next chapter creatively entitled "Event-Driven Programming."
|