Object Oriented Programming for the Java Programmer, November 2002
Objects, they're all around us...we own them, use them, break them, and fix them but when it comes to programming, objects become a mystery to many people. This is one topic that I for one will admit did not come easy and shouldn't be skipped over just because it's frustrating to learn. Object orientation is not a subject that you will read once & have it mastered so don't think you should. I am going to take this tutorial one slow step at a time because I don't want to confuse anyone or lead them down the wrong path. I will make every attempt to give real world examples of each of the concepts where it is possible. I am going to try to write this monster with both non-programmer examples and then again with Java code to show examples. I'm using Java because, in my opinion it is the ONLY pure object oriented language and shows all aspects of this difficult topic in action. You may still learn a lot from this without understanding the code completely but try to grasp the code wherever possible.
Starting at Square One
The first thing you must understand is that objects in programing are classes. Just like a real world object your classes have certain characteristics to them, called attributes and they can do certain things called methods. If you always think of attributes as nouns and your methods as verbs you will be much better off learning this. These files (your classes) are usually set up to contain all of the attributes and methods that relate to that class's specific function, hence they are technically containers. A well designed object will ONLY have attributes and methods that directly relate to that class. Using this theory as your credo, the objects you create become reusable modules that you may then use, over & over, in many different applications. Building a class with information that is proprietary to your application limits its re-usability so try to avoid it when possible.
But isn't a class an application? It can be, but a better way is to create your applications from many different classes. I like to explain this with Legos (you know those great toys EVERY kid has). Think of your classes as legos, each has it's own color, size, shape & purpose but they all work together to build whatever you want. Being well designed objects you can take ANY lego and put it into another project with no changes being required to the lego BUT the project you are building is enhanced by the addition of this particular lego. Thinking like this in programming is THE CORE of great object orientation. So now that we have filled up your head with new words and concepts, let's try to relate this to the "real world" to clear up some of the fog.
Let's start with a simple object and work through the basics one step at a time. Our real world example is an Animal class. First let's look think about what the real world animal
class would have. They have a height, weight, a lifespan, and a particular genus. Click Here to see
our class skeleton. As you can see it has four variables to hold the data about our object, very similar to how you would describe a real animal. Not very functional but it's a start. We
need to add methods to our new object, just like a real world animal our object can "do" things so let's add them now.
Click Here. We have added two methods, one to allow our object to eat
and another to make allow our object to
sleep
. Let's keep it simple for now so that's all we are going to add.
Now another VERY important concept we must grasp here is the instance of a class. Our class files are technically only templates. When we need to use the class we create an
instance of the class....basically a copy BUT you can have many instances all with their own information stored in them. This is called instantiation. Think of your class as the mold by which
we create objects. Now we need some way to create this object when we need it, this is called a constructor. Try to think of the constructor as the guy working the machine at the
animal factory. Every time you ask for another object he pops it out! Let's add a constructor to our code. Click Here.
Note that the constructor has the same name as our class file. To now create an instance of our object we would use this line of code: Animal anim = new Animal(); Simply put we are
creating an instance of our Animal class called a, the new keyword is the real magic here. This keyword calls our constructor (Hey, make me another of those Animals would ya?
,
and by the way call it anim so I know which one it is!), hence creating our instance of the class. But what do we have? An empty instance with nothing in the variables.....that's no good!
Technically I'm lying to you there because there are defaults for our class variables but that's another story that we will get to later, for now let's just say the object is null (contains nothing).
Let's create another constructor that does what we need...oh yes we can have as many constructors as we want, as long as the variables they are accepting are different variable types,
but that's another topic that we will also get to later. Anyway, here is our new constructor: Click Here. Now this
requires a bit of explanation but first let's see how we would now create our instance of the class, this time using our new constructor.
Animal anim = new Animal(4, 2, 10);
So now we have created an animal that has a height of 4, weight of 2 and has a lifespan of 10. If you look at the code in the new constructor you will see lines similar to this: this.xxx = xxx.
You will also notice that the incoming variables are called the same thing as our class variables. this, in java is a keyword that means this current object
so the incoming parameters are
assigned to the class variables for this instance. Also, using the same name for the incoming parameters and your class variables is perfectly legal in Java as the parameters are hidden
from the rest of the class OUTSIDE of the method. Although it is not required, I prefer to use the this.xxx for clarity. I know this can be VERY confusing for beginning java
programmers but these are important concepts that you will see over and over again. Well,....now we have an object that has something to it but how do we now use it? Move on to the
next section and we will explore further.
Theresa Judd, permanentcosmeticsbytheresa.com | More Testimonials >>