Greg's Blog

helping me remember what I figure out

Java 101

| Comments

This will provide a brief overview of the tools and documentation needed to get into Java programming and how to define classes (looking at the differences between applets and java applications) before delving into OO concepts a little. So for starters you’ll need to get a hold of the following items, which are available for download:

  1. Get the SDK, at time of writing the version was 1.4
  2. Get the full documentation
  3. Get the tutorial
  4. Get a good editor, TextPad is useful or use the IDE: Sun Studio One [now bundled with the SDK]

Easy beginnings: comments in java

Listed below are three ways of commenting your code:

// single line
/* */ multi line
/** */ documentation, creates the documentation using the javadoc tool

All pretty self explanatory!

Next step: Defining an class

Listed below are the two types of class definitions I found:

class YourClassName {

}

Where YourClassName is the same as your file name in this case it would be: YourClassName.java. The other one I came across is as follows

public class YourClassName {

}

There are of course a few other components to a class declaration, however a point worth noting is that if you omit the public component from a class, this class will only be accessible to other classes in it’s package. Whereas using the public statement indicates that this class is accessible by other classes, regardless of their package membership.

abstract, final, extends and implements form the other components of a class declaration.

The “main” Method in Java Applications

Every Java application must have a “main” method and it takes the following signature (or structure)

public static void main(String[] args) {

}

When an application is executed this the method that is first called. What is a method? A method is a function, i.e. it carries out an action.

What is a constructor? And what’s the difference? You can recognize a constructor because it has the same name as the class and has no return type. You can also have multiple constructors, in which case they still have the same name, but take a different number or type of arguments. If you fail to specify a constructor in your class, the Java automatically provides a no argument constructor or “default constructor”.

Applets, what’s the difference

Things are slightly different when working with applets. As opposed to applications where a lost of system classes are readily made available, for starters to draw an applet you need to import a few new classes, like such:

import java.applet.Applet;
import java.awt.Graphics;

Furthermore when creating your class you will make a few more changes:

public class YourClassName extends Applet {

}

By using the “extends” statement you are in fact making “YourClassName” a sub class of “Applet”, which also means that “YourClassName” inherits all of the behaviour and hence functionality of “Applet”.

The equivalent of “main” for applets is “init”, “start” or “paint”. Every applet, must implement one or more of these methods. For example:

public void paint(Graphics g) {

}

g is an object of Graphics passed to the “paint()” method this sets the onscreen drawing context . One argument that can be passed is the Graphics drawString() method. For example:

g.drawString(“Hello World”, 50,25);

This method takes a string as the first argument, then the x and y co-ordinates for the display. There are two more methods available to Applets: “stop” and “destroy”.

Enough already, a quick overview of OO concepts

What Is an Object?
“An object is a software bundle of related variables and methods.”

Software objects are often used to model real-world objects you find in everyday life. Examples of objects are things you can see in everyday life: such as a bike. All objects share two characteristics: state and behaviour. Using our bike example, a bike’s state might be current gear, current speed or two wheels. And it’s behaviour could be braking or accelerating. A software object stores it’s state in one or more “variables” and implements behaviour by way of “methods”.

A handy way to model this is by using a donut. Where the methods (behaviour) are displayed in the ring and variables (state) is kept inside the hole. Such variables are known as instance variables in objects as they map the state for a particular object. MyBike might have a completely different state to YourBike. The same applies to methods within objects, they are formerly known as instance methods because they add behaviour to a particular instance of an object, not all objects.

The analogy of a donut is good for another reason as it also shows that variables are hidden to the outside world by methods, this is also known as “Encapsulation”. However it can also occur that an object may wish to expose these values and/or hide some of the methods.

What Is a Message?
“Software objects interact and communicate with each other using messages.”

Most objects form part of a greater whole, as such these individual objects may need to interact with other ones. For example a bike is useless and unless you interact with it, i.e. use it and pedal. This kind of interaction in the software world is achieved by way of sending messages from one object to another, or simply put one object calls a method in another object. In some cases calling another method may really on additional information to be passed, these additional bits of information are known as parameters. For example: The object YourBicycle may wish to change to a lower gear, so it would call a method changeGear with a parameter to determine whether to change up or down and in this case the parameter would have a value of lowerGear.

What Is a Class?
“A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.”

Using our real world example: bicycles exist by the millions and all share common traits (two wheels, breaks), this would be known as a “class”. Whereas your bike would be just one instance of this class or also known as an “object” because it’s state or behaviour at any given time can be different to another bike’s.

On top of the objects separate instance variables a class can define class variables, these are variables that are shared by all instances of this class. For example a class variable could be the number of gears a bike has. Just as objects have instance methods, classes have “class methods”.

What Is Inheritance?
“A class inherits state and behaviour from its superclass. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs.”

A good example for inheritance is this: Do you know what a “Penny farthing” is? If i told you it was a bicycle it would greatly help to understand that it has two wheels, handle bars and pedals. Essentially inheritance allows you to pass common traits from a superclass (bicycle) to a subclass (penny farthing). Penny farthing inherits all of the traits of the bicycle class and is able to add additional states and behaviour. The tandem bike is a classic example where it inherits a lot of state and behaviour from a bike, but also adds new state (two seats for example).

Subclasses can also override states and behaviour, for example a mountain bike may override the bike superclass by specifying more gears than the original.

Inheritance is not limited to one layer, it can be as deeply nested as needed, this is known as either the inheritance tree or hierarchy. Generally speaking as you navigate deeper down the tree you keep adding more specialised behaviour and state.

What Is an Interface?
“An interface is a contract in the form of a collection of method and constant declarations. When a class implements an interface, it promises to implement all of the methods declared in that interface.”

Back to the bicycle analogy. A bike class only defines what it can do, not how it interacts with the rest of the world. An unrelated item could be an inventory management tool in a store. The two evidently share a relationship but it’s not in terms of what they can each do. In order for the inventory program to be able to track the bike it needs to have a price and tracking number. Where they come from is what matters to the inventory program. So in order for the bike to be in the system it agrees to implement the state and behaviour for the inventory. It interfaces with the application by delivering the price and track number.

And this completes part 1. Part 2 will look at the language itself in more detail, covering variables, operators, expressions, statements, blocks, control flow statements and branching statements.