![]() | Java Programming Getting started | Installation![]() |
NavigateGetting Started topic:() |
We conceptualize the world around us in terms of systems. Asystem is a web of interconnected objects working together in tandem. In thesystems 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:
Figure 1: A simple system interacting with its environment using input and output messages. |
![]() |
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 interconnectedsub-systems – where each sub-system may or may not be divided into further sub-systems. Each sub-system communicates with others usingfeedback messages – that is,inputs andoutputs.
Figure 2: Example of a complex system with multiple sub-systems and interactions |
![]() |
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 ofrepresenting real-life problems and objects into your programs.
Suppose a novelist, a painter and a programmer were asked toabstract (i.e.,represent) a real-life object in their work. Suppose, the real-life object that needs to be abstracted isan 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 calledobject-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.
In OOP, every abstraction of a real-life object is simply called anobject 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):
![]() | classAnimal{} |
The code above creates a space within your code where you can startdefining an object; this space is called aclass (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 calledAnimal
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.,class
). Such predefined words in the Java language are known askeywords and make up the grammar for the language (known asprogramming syntax).
Note: Class definitions have different names in different languages. They are sometimes calledtype definitions,object specifications ortemplates as well by Java Programming
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:
![]() | Animaldog=newAnimal(); |
The code above effectively creates an object calleddog
based on the class definition forAnimal
. In non-programmer parlance, the code above would translate into something akin to saying, "Create a new objectdog
of typeAnimal
." A single class definition enables you to create multiple objects as the code below indicates:
![]() | Animaldog=newAnimal();Animalcat=newAnimal();Animalcamel=newAnimal(); |
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).
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 simplyfields) 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) andlegs (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:
![]() | classAnimal{Stringname;intlegs;} |
In the code above you defined two object fields:
name
of typeString
; and,legs
of typeint
.These special pre-defined types are calleddata types. TheString
data type is used for fields that can holdtextual values like names, while theint
(integer) data type is used for fields that can holdnumeric valuesNote: Fields are called different things in different languages. They may be calledstate identifiers,properties ormember variables in other programming language syntax. Java uses the wordsfields andproperties in different contexts, as would be understood from upcoming sections. by Java Programming
Figure 3: In order to denote the Animal object as a system within the Java Environment,you present it as such. Note how fields are presented. |
![]() |
In order to demonstrate how fields work, we will go ahead and create objects from this amended version of our class definition as such:
![]() | Animalanimal1=newAnimal();Animalanimal2=newAnimal();animal1.name="dog";animal1.legs=4;animal2.name="duck";animal2.legs=2; |
You can access the fields of your created objects by using the.
(dot) ormembership operator. In the example above, we created two objects:animal1
andanimal2
of typeAnimal
. And since, we had established that eachAnimal
has two fields namelyname
andlegs
, 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 currentstate. So, for instance:
animal1
object is a"dog"
with4
legs to walk with; while,animal2
object is a"duck"
with2
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.
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 calledmethod.
In the case of theAnimal
, you require your virtual representation of an animal to be able to move through its environment. Let's say, as an analogy, you want yourAnimal
object to be able to walk in its environment. Thus, you need to add a method namedwalk
to our object. To do so, we need to write the following code:
![]() | classAnimal{Stringname;intlegs;voidwalk(){}} |
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 (orscope) within your object. So the first set of curly brackets defined a scope for your class definition called theclass-level scope. This new set of curly brackets alongside a method defines a scope for the further definition of your method called themethod-level scope.
In this instance, the name of our method iswalk
. 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 calledarguments.
A method therefore enables an object to:
In essence, methods are what makes an object behave more like a system.
Notice the keywordvoid
before the name of the method – this tells us that the methodwalk
returnsnothing. You can set a method to return any data type – it can be aString or anint as well.
Note: Methods are known by different names in different programming language. They might be calledfunctions,procedures,routines orbehaviors. by Java Programming
Figure 4: The Animal object can now be denoted as having an interaction behavior within the Java Environmentas illustrated here. Note the difference between the presentation of fields and methods. |
![]() |
By now, we thoroughly understand that any object can interact with its environment and in turn be influenced by it. In our example, theAnimal
object exposed certain fields –name
andlegs
, and a method –walk()
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.
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 asaccess modifiers.
Let's modify our example to demonstrate this effect:
![]() | classAnimal{publicStringname;publicintlegs;publicvoidwalk(){}} |
By declaring all fields and methodspublic
, we have ensured that they can be used outside the scope of theAnimal
class. This means that any other object (other thanAnimal
) has access to these member elements. However, to restrict access to certain member elements of a class, we can always use theprivate
access modifier (as demonstrated below).
![]() | classAnimal{privateStringname;privateintlegs;publicvoidwalk(){}} |
In this example, the fieldsname
andlegs
can only be accessed within the scope of theAnimal
class. No object outside the scope of this class can access these two fields. However, since thewalk()
method still haspublic
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).
![]() | publicclassAnimal{privateStringname;privateintlegs;publicvoidwalk(){}} |
The following list of keywords show the valid access modifiers that can be used with a Java program:
keyword | description |
---|---|
public | Opens access to a certain field or method to be used outside the scope of the class. |
private | Restricts access to a certain field or method to only be used within the scope of the class. |
protected | Access to certain field or methods is reserved for classes that inherit the current class. More on this would be discussed in the section oninheritance. |
![]() | Java Programming Getting started | Installation![]() |