Java Programming/Getting started

Understanding systems
We conceptualize the world around us in terms of systems. A system is a web of interconnected objects working together in tandem. In the systems theory, a system is set out as a single entity within a world surrounded by an environment. A system interacts with its surrounding environment using messages of two distinct types:
 * inputs: messages received from the surrounding environment; and,
 * outputs: messages given back to the surrounding environment.

Life is a complicated mess of interconnected objects sending signals and messages. See the illustration below in figure 2 demonstrating a complex system for an economic ecosphere for a single company. Imagine what this system diagram would be like if you were to add a few more companies and their sub-systems. Computer software systems in general are a complex web of further interconnected sub-systems – where each sub-system may or may not be divided into further sub-systems. Each sub-system communicates with others using feedback messages – that is, inputs and outputs.

The process of abstraction
Programming is essentially thinking of solutions to problems in real life as a system. With any programming language, you need to know how to address real-life problems into something that could be accurately represented within a computer system. In order to begin programming with the Java programming language (or in fact, with any programming language), a programmer must first understand the basics of abstraction.

Abstraction is the process of representing real-life problems and objects into your programs.

Suppose a novelist, a painter and a programmer were asked to abstract (i.e., represent) a real-life object in their work. Suppose, the real-life object that needs to be abstracted is an animal. Abstraction for a novelist would include writing the description of the animal whilst the painter would draw a picture of the animal – but what about a computer programmer?

The Java programming language uses a programming paradigm called object-oriented programming (OOP), which shows you exactly what a programmer needs to be doing. According to OOP, every object or problem in real-life can be translated into a virtual object within your computer system.

Thinking in objects
In OOP, every abstraction of a real-life object is simply called an object within your code. An object is essentially the most basic representation of a real-life object as part of a computer system. With Java being an object-oriented language, everything within Java is represented as an object. To demonstrate this effect, if you were to define an abstraction of an animal in your code, you would write the following lines of code (as you would for any other abstraction):

The code above creates a space within your code where you can start defining an object; this space is called a class (or type) definition. All objects need to be defined using a class definition in order for them to be used in your program. Notice the curly brackets – anything you write within these brackets would serve as a definition or specification for your object. In the case of the example above, we created a class definition called  for objects that could serve as an abstract representation of any animal in real-life. The way that a Java environment evaluates this code to be a class definition is by looking at the prefix word we used to begin our class definition (i.e., ). Such predefined words in the Java language are known as keywords and make up the grammar for the language (known as programming syntax).

Understanding class definitions and types
Aristotle was perhaps the first person to think of abstract types or typologies of objects. He started calling them classes – e.g., classes of birds, classes of mammals. Class definitions therefore serve the purpose well in defining the common characteristics or types of objects you would be creating. Upon declaring a class definition, you can create objects based on that definition. In order to do so however, you need to write a special syntax that goes like this:

The code above effectively creates an object called  based on the class definition for. In non-programmer parlance, the code above would translate into something akin to saying, "Create a new object  of type  ." A single class definition enables you to create multiple objects as the code below indicates:

Basically, you just have to write the code for your class or type definition once, and then use it to create countless numbers of objects based on that specification. Although you might not grasp the importance of doing so, this little exercise saves you a lot of time (a luxury that was not readily available to programmers in the pre-Java days).

Expanding your class definitions
Although each object you create from a class definition is essentially the same, there has to be a way of differentiating those objects in your code. Object fields (or simply fields) are what makes your objects unique from other objects. Let's take our present abstraction for instance. An animal could be a dog, cat, camel or a duck but since this abstraction is of a very generic kind, you need to define fields that are common to all of these animals and yet makes the animals stand apart. For instance, you can have two fields: name (a common name given to any one of these animals) and legs (the number of limbs any one of these animals would require to walk). As you start defining your objects, they start to look like this:

In the code above you defined two object fields:
 * a field called  of type  ; and,
 * a field called  of type.

These special pre-defined types are called data types. The  data type is used for fields that can hold textual values like names, while the   (integer) data type is used for fields that can hold numeric values

In order to demonstrate how fields work, we will go ahead and create objects from this amended version of our class definition as such:

You can access the fields of your created objects by using the  (dot) or membership operator. In the example above, we created two objects:  and   of type. And since, we had established that each  has two fields namely   and , we accessed and modified these fields for each of our objects using the membership operator to set the two apart. By declaring different values for different objects, we can manipulate their current state. So, for instance:
 * the  object is a   with   legs to walk with; while,
 * the  object is a   with   legs to walk with.

What sets the two objects apart is their current state. Both the objects have different states and thus stand out as two different objects even though they were created from the same template or class definition.

Adding behavior to objects
At this point, your objects do nothing more than declare a bunch of fields. Being a system, your objects should have the ability to interact with their environment and other systems as well. To add this capability for interaction, you need to add interactive behavior to your object class definitions as well. Such behavior is added to class definitions using a programming construct called method.

In the case of the, you require your virtual representation of an animal to be able to move through its environment. Let's say, as an analogy, you want your  object to be able to walk in its environment. Thus, you need to add a method named  to our object. To do so, we need to write the following code:

As you write this code, one thing becomes immediately apparent. Just like the class description, a method has curly brackets as well. Generally, curly brackets are used to define an area (or scope) within your object. So the first set of curly brackets defined a scope for your class definition called the class-level scope. This new set of curly brackets alongside a method defines a scope for the further definition of your method called the method-level scope.

In this instance, the name of our method is. Notice however that the name of our method also features a set of round brackets as well. More than just being visual identifiers for methods, these round brackets are used to provide our methods with additional input information called arguments.

A method therefore enables an object to:
 * 1) Accept input: Receive some argument(s);
 * 2) Process information: work on the received argument(s) within its curly brackets; and,
 * 3) Generate ouput: occasionally, return something back.

In essence, methods are what makes an object behave more like a system.

Notice the keyword  before the name of the method – this tells us that the method   returns nothing. You can set a method to return any data type – it can be a String or an int as well.

The process of encapsulation
By now, we thoroughly understand that any object can interact with its environment and in turn be influenced by it. In our example, the  object exposed certain fields –   and , and a method –   to be used by the environment to manipulate the object. This form of exposure is implicit. Using the Java programming language, a programmer has the power to define the level of access other objects and the environment have on a certain object.

Using access modifiers
Alongside declaring and defining objects, their fields and methods, a programmer also has the ability to define the levels of access on those elements. This is done using keywords known as access modifiers.

Let's modify our example to demonstrate this effect:

By declaring all fields and methods, we have ensured that they can be used outside the scope of the   class. This means that any other object (other than ) has access to these member elements. However, to restrict access to certain member elements of a class, we can always use the  access modifier (as demonstrated below).

In this example, the fields  and   can only be accessed within the scope of the   class. No object outside the scope of this class can access these two fields. However, since the  method still has   access, it can be manipulated by actors and objects outside the scope of this class. Access modifiers are not just limited to fields or methods, they can be used for class definitions as well (as is demonstrated below).

The following list of keywords show the valid access modifiers that can be used with a Java program: