Friday, September 20, 2013

Building a Simple Java Program Part 4: Making the Model

Lets overview what we have accomplished so far in my past blogs:
  1. Installed Java and Eclipse
  2. Created a Java Environment by creating a project and packages
  3. Created a Car class to serve as an object
In the last blog, we created a Car class.  This is a special class that defines a car.  We said that a standard car has four doors, four wheels, and calls it self "Car".  What we are going to do next is how we can organize Java code by creating instances of an objects.  We can look at it this way.  When a model is a car, it should have the same properties as a car.  We can either extend those properties to the model or inheritance them within an object.  We are going to do both.

First we will create some two Java files.  One is called BMW and the other we will call Series3.  The BMW class will extend the car properties and the Series3 will inheritance an object though the BMW class.  Lets start by creating the files.  In Eclipse, create two class files, one called BMW and one called Series3.  Look at the last blog for instruction on how to do this.  When you create the create the BMW file, don't select the main(String[] args) option.  However, select it for the Series3.  


Look at the above for reference.  In the BMW code, we are going to add a string variable called company, import Car, and extend Cars to BMW.  Before we move on, let me explain something.  Look at the code below.  Import and extends is new.  Import is how we tell a class where another is.  In this case, we tell class that Car is loaded in package test.example.car. You can also use test.example.car.*, but since we are only using one class, we will only import on that one.  Next is extends.  This is away to add the Car code to BMW.  It is like we wrote all the Car code in BMW.  This is a great way to reuse code.  Something you need to understand is extending code isn't flexible as we are just adding code.  All inherent code will follow suit.  Examine the completed code below before you move on.

package test.example.models;

import test.example.car.Car;

public class BMW extends Car{
public static String company = "BMW";
}

You are welcome to add some variable that are unique to a BMW.  Just remember the rules about variables and that you CANNOT have the same named variables in BMW as in Car.  Next, we are going to change the code in Series3.  Remember when we added a Main Method to this class through the class wizard?  This code will be the central point for putting it all together.  It's out execute code.  Lets examine the code and change it to this:

package test.example.models;

public class Series3 {

public static void main(String[] args) {
BMW series = new BMW();
System.out.println("I am a " + series.company + " Series 3, I have " + series.door + " doors and " + series.wheels + " wheels.");
}
}

Did you notice that we didn't need to import BMW?  This is because Series3 is in the same package.  Look at the code:

BMW series = new BMW();

Whats going on here?  This is how we create an object in Java.  We are saying that the variable "series" is going to hold an object called BMW, and to create an the object as a fresh, new object. Note that if the class has static methods, the next time the object is created, it'll basically be a duplicate of the first object.  Any changes done in one changes the other.  See the parentheses?  That is for constructors.  We can put stuff there to extend what BMW does.  Don't worry about that now as this is something to cover in a different lesson. 

Now that we have created a object called series, we can now access any variable that exist within it.  Note that Java can take different types of variables and convert them to strings so we can display them.  You can see this after the println.  If you notice, we can call on an object variables by object.variable.  So series.company will gives us "BMW".  Try it.  Save BMW and Series3 and press the green arrow in Eclipse to run the program.  You get this in Eclipse:

I am a BMW Series 3, I have 4 doors and 4 wheels.

Next blog, we are going to do an overview of what just went on and shine some light on a couple of things.

Next

No comments:

Post a Comment