Monday, January 11, 2010

Programming: Java: Tutorials: A Guide to Class Architecture

Java is a type of language known as OO or Object-Oriented. OO Programming differs from other types because it allows the programmer to write less code, which in turn is more efficient code.

As we know, in java a class is written in a .java file. Inside this file we denote the beginning of our class with a line similar to the one bellow.
public class Cat {
This line would create a public class named Cat. That would mean a class that any other class can access. We'll get more into that later.

Inside the class tags, if this is our main class, we have the main() method. This is the method which runs when the program starts.

public void main(String[] args) {
So, what have we done so far? We have made a .java file called Cat.java, which contains a public class name called Cat. Also we have a main method within our class.

So your thinking "So what?", that this is nothing new. Well, that may be but the class we have created has a major flaw. It's static!

Static vs. Non-Static

A Static class means class which just IS. I can't be recreated. It just runs. Values can be applied to it, and drawn from it, but you cannot make multiple instances of it.

What we need is a way to make each version of our class different. What we need is a Constructor!

Constructors

A constructor allows us to create multiple versions of our class in memory, each with different values.

The constructor is a sub-routine which is public, has no return statement (not even void), and has the same name call as the name of the class. In our case a constructor would look like this.

public Cat() {
This would be a "Default Constructor". The reason it gets this title is because it accepts no inputs from the class creating it. All it does is create a new instance of the Cat class using what ever values are already stored within it by default.

Also, like all other sub-routines we can have multiple versions of it, each with different parameters. Like so.

//Default Con.
public Cat() {
  s = 10;
  a = 5;
}

//Secondary Con.
public Cat(int Size) {
  s = size;
  a = 5;
}

//Thir-dary Con.
public Cat(String name) {
  s = 10;
  n = name;
}

//Fourth-dary Con.
public Cat(int Size, String name) {
  s = size;
  n = name;
}

See, by creating a constructor for each possible contingency we allow the Cat class to work under any circumstance.

Now from another class we could create multiple instances of the Cat Class, each with different values like so.

Cat c1 = new Cat();
Cat c2 = new Cat(5,"Dilbert");
Cat c3 = new Cat(7);
Cat c4 = new Cat("Dill");

or we could create an Array of Cat like so.

Cat[] cats = new Cat[4];
for (int i = 0; i < cats.size; i++) {
  cats[i] = new Cat((int)(i,"Cat: #" + i);
}

Sadly...

Sadly the powers that be at VHS aren't exactly "fond" of me posting how to's on my blog here so that's all I'm going to go over. This is the basics of the Class system, there isn't too much else to it accept for practice. Good luck! 

No comments: