Professional Documents
Culture Documents
Overview
This handout introduces Java as a technology. It looks at OO (Object-oriented)
programming concepts as Java is an OO language and also at the differences between
C++ and Java.
Java was originally developed by Sun Microsystems, a US company, in 1991. Its first
name was Oak and it was designed for the development of software for consumer
electronic devices such as televisions and video recorders. As such, one of the main
goals for the language was that it be simple, portable and highly reliable.
The team based the new language on the existing C and C++ languages, but they
removed features of C and C++ that were seen as being the sources of problems.
In 1993, the WWW (World Wide Web) was starting to be used, transforming the
previously text-based internet into a more graphic-rich environment. The team
developing the Oak language came up with the idea of creating small programs, called
applets, that would be able to run on any computer that was connected to the internet.
In 1995, Oak was renamed Java due to some legal issues – the name does not have
any particular meaning. Java was supported by many IT companies, including the big
internet browser developers, Netscape and Microsoft.
Since then, Java has become one of the main languages used for internet
programming and also as a general-purpose object-oriented language for stand-alone
applications.
2 What is Java?
The Java language is object-oriented and programs written in Java are portable – that
is, they can be run on many different platforms (e.g. Windows, Mac1, Linux, Solaris2).
Java is different from other programming languages because a Java program is
compiled and interpreted – in many other languages, the program is either compiled
or interpreted.
1
Mac, short for Macintosh, is an operating system owned by the Apple Corporation. The Mac OS has a
GUI that looks like Windows but has a different underlying architecture. It is a competitor to Windows.
The Mac OS has to be run on Apple Mac computers, which, again, have a different underlying
architecture to PCs like IBM, Acer etc.
2
Solaris is a platform from Sun Microsystems. Like Windows, Solaris has server and workstation
operating systems.
Page 1 of 12
8966466.doc FBE – Computer Science Dept
The Java compiler translates the program into an intermediate language called Java
bytecodes. Java bytecodes are platform-independent – this means they can be run on
different operating systems (e.g. Windows and Mac). There are different Java
interpreters for different platforms. The interpreter parses and runs each of the Java
bytecode instructions.
The big benefit of the Java way of doing things is that the program code can be
written once, compiled and then run on many different platforms – see Figure 2 below
for an illustration of this concept. There is one Java VM, but many different
implementations of it – i.e. many different interpreters for the Java bytecodes.
Figure 2 – a Java program can be compiled once and then run on different platforms
Page 2 of 12
8966466.doc FBE – Computer Science Dept
The Java platform is a bit different to other platforms, in that it consists of software
only – that means it is hardware-independent. This is what makes it possible to run
compiled Java programs on different platforms.
The Java platform consists of:
• the Java VM
• the Java API (Application Programming Interface).
In order to write Java programs, you need to have the Java SDK (Software
Development Kit) installed on your PC. This includes the API and the VM, as well as
compilers and debuggers for Java.
The OO paradigm aims to eliminate some of the flaws of the procedural approach to
programming. In OO, data is critical – data is not allowed to move freely around a
system, but it is tied closely to the functions that operate on it. Data is also protected
from unintentional modification by other functions.
In OO programming, a system or problem is decomposed into entities called objects.
An object has data members and functions – in Java, functions are known as methods.
Data is protected because the data belonging to an object can only be accessed and
modified by the methods of that object.
Different objects can 'talk' to each other through their methods – in this way, Object A
cannot directly modify data belonging to Object B – but it can call a method of Object
B that in turn modifies the data.
Page 3 of 12
8966466.doc FBE – Computer Science Dept
A class is the definition of a set of objects which are all similar in terms of their
attributes, associations and functionality e.g. customer class defines all customer
objects.
A class model is a static view of objects which exist in the system users' world.
A class consists of data and methods. For example, Figure 3 below shows a class
named 'Customer'. It has 3 attributes and 2 methods (functions).
Customer Class
Name
Address Attributes (data)
Current Balance
Amend Address ( ) Methods
Adjust Balance ( )
Objects interact with each other by sending messages between them. For example, to
amend the address for the Customer object that represents 'Solomon Tekle', a call
Page 4 of 12
8966466.doc FBE – Computer Science Dept
needs to be made to the method 'Amend Address'. This is like sending a message to
the 'Solomon Tekle' object, telling it to change the address to a specified new value:
Sending this message is known as invoking or calling the method 'Amend Address'.
The Customer class definition tells the program what the format of the Amend
Address method is – including the method name, what values it takes (the
parameters) and what it returns (what data-type to expect for the response).
Private methods:
• Are triggered by methods within an object
• Are internal to a class
• Can be changed without affecting any methods outside the class
A class is a bit like a data-type – once a class has been defined, any number of objects
belonging to that class can be created. The objects then are a bit like variables – each
object is associated with the class type from which it was created.
Abstraction refers to the way in which a class represents only the essential features of
the set of objects it models – a class does not include background or extra details that
are not relevant to the system.
So a class is like a list of abstract attributes e.g. size, weight, cost, and the methods
that operate on those attributes e.g. change the cost, increase the weight.
3.3 Inheritance
Inheritance is the way in which objects of one class get the properties of objects of
another class. This includes the data and the methods.
For example, take a system in which there are various types of 'person' objects – they
have some attributes in common, such as Name, Address and Date of Birth. The
person that is an Employee is part of the class 'Person', but has some extra attributes
such as Salaray, Department and Rank.
If we also have retired employees, they have further attributes such as Date Retired,
Benefits.
Page 5 of 12
8966466.doc FBE – Computer Science Dept
The simple class diagram below shows 3 classes – Person, Employee and Retiree. The
arrow shows that Employee and Retiree are both part of the Person class. In other
words, they are derived from the Person class, and they inherit the attributes of the
Person class. The notation shown here (using the arrow to denote inheritance) is
commonly used in class diagrams.
Person
Name
Address
Date of Birth
Employee Retiree
Employee and Retiree are sub-classes of Person – a sub-class only needs to define
those attributes that are unique to it, that do not apply to the super-class (or parent
class).
Inheritance provides the idea of reusability – the Person class can be reused without
making changes to it, because another class can be made to inherit from it.
The methods of a parent class are also inherited by its sub-classes, and new methods
can be defined in the sub-classes. For example, if we add some methods to the
Person/Employee/Retiree diagram – the Address of a person can be changed, so we
need an Amend Address method. This method is inherited by the Employee and
Retiree classes.
An Employee can also be promoted, so it needs a Promote method (note that this
method could change the Rank and Salary attributes – so a method can change more
than one data item).
Person
Name
Address Page 6 of 12
Date of Birth
Amend Address ( )
8966466.doc FBE – Computer Science Dept
Employee Retiree
Promote ( )
An abstract class is convenient to define characteristics that are shared by two or more
classes. If the inheritance concept did not exist, the Employee and Retiree classes
would have to explicitly include the attributes that are common to them – leading to
some code duplication.
Page 7 of 12
8966466.doc FBE – Computer Science Dept
There are different ways to denote these relationships on class diagrams, and Java as
an OO language has ways to model these in code. We will not go into the coding yet,
but you should be aware that it is possible to model these in your programs.
A commonly used notation for drawing class diagrams is UML (Unified Modeling
Language). This provides standard ways of drawing models and diagrams for all
stages of system development, and includes the OO approach. The diagram below
shows a class diagram – it shows that:
A Project uses Persons
A Project has Stages
A Retiree is a Person
An Employee is a Person
An Employee is associated with a Department
The numbers indicate the cardinality of the relationships (with * indicating many) –
much like Entity-Relationship diagrams4.
It is good practice to draw at least a simple class diagram before you begin to write an
OO program – as it helps to decide what classes need to be defined.
Dependence /Association
1
Project Person Department
0..* 0..* Inheritance 1
Aggregation (is-a)
* (has-a) *
Retiree Employee
Stage
Consider a class hierarchy for different shapes. A Shape class encapsulates the
common features of shapes – such as drawing a shape and calculating the area of a
shape (or, in other words, each Shape object can draw itself and can calculate its
area). So Shape would be an abstract class – an object must be a square or a circle or a
triangle and so on – it cannot be just an undefined shape. This is depicted in Figure 7
below.
We know that the way to calculate the area for a circle is different to that for a square.
To reflect this, each sub-class needs to have a different calculation in the Calculate
Area method.
4
You have studied E-R diagrams in your Systems Analysis and Design course. This course does not
cover UML in detail as it is a very comprehensive modeling language.
Page 8 of 12
8966466.doc FBE – Computer Science Dept
Shape
Calculate Area( )
Circle Square
Figure 7 – Polymorphism – the Circle and Square classes have their own physical
implementations of the Calculate Area () method.
This means that the physical implementation of the Calculate Area method will be
different in each sub-class. At runtime, objects of the class Circle would invoke the
Calculate Area method from the Circle class while objects of the class Square would
invoke the Calculate Area method from the Square sub-class. The method will still
have the same name in each sub-class, as it is inherited from the parent class.
If Shape is defined as an abstract class, and if it does not make sense to calculate the
area for a shape unless it has a specific implementation (Circle, Square), then the
Calculate Area method in the Shape class can have nothing in it – it would then be an
abstract method. However, sometimes, the parent class may have one implementation
of the method and the sub-classes have different implementations.
This idea is called dynamic binding or late binding. This means that the code
associated with a given procedure call is not known until the time of the call at
runtime.
For the Calculate Area method, the code that is executed depends on the class of the
object for which it is called.
1. Create classes – a class defines a set of objects that have common characteristics
2. Create objects from the class definitions
Page 9 of 12
8966466.doc FBE – Computer Science Dept
4 The Benefits of OO
• The concept of inheritance and the data-oriented approach allow alot of reuse
of existing classes and helps to eliminate redundant code.
• Programs can be built using working modules that already know how to
communicate with each other. This means that programs do not always have to
be written from scratch – thus saving development time and increasing
productivity.
• Encapsulation (hidingof data) helps with the building of more secure programs
– as data cannot be unintentionally changed by other parts of the program.
• There is a close link between objects in the real-world system and objects in
the program (recall E-R diagrams – consider how these could be easily
mapped to OO classes). Therefore the structure of the system is more likely to
be meaningful to users.
• The work for a project can be divided by class/object – making it easier to
split work up between a team of developers.
• OO systems can be easily upgraded from small to larger systems.
• The message passing technique for communication between objects makes it
easy to describe the interface of an OO system for other systems that need to
communicate with it.
• Software complexity can be easily managed.
Of course, many of the above benefits depend on the system being well-designed –
the programmer needs to ensure that the OO concepts discussed in section 3 above are
incorporated into the design.
5 Differences to C++
Java is a true OO language, while C++ is C with an object-oriented extension (C is
not an OO language).
The following is a list of some major C++ features that were intentionally omitted
from Java or significantly modified. You may not have covered all of these C++
features in your C++ programming course, but you should be familiar with some of
them. And you may find that Java makes your life as a programmer easier!
There are, of course, other differences between Java and C++, which you will
discover as we go through the course.
• Java does not support operator overloading (not covered in C++ course).
• Java does not have template classes (not covered in C++ course).
Page 10 of 12
8966466.doc FBE – Computer Science Dept
• Java does not directly support multiple inheritance of classes, but it does allow
this to be achieved using a feature called an interface.
• Java does not support global variables – every variable and method is declared
within a call and forms part of that class (this relates to the scope of variables,
which was not covered in great detail in the C++ course).
• Java does not use pointers (but similar functionality is achieved using implicit
references to objects).
• Java has replaced the destructor function with a finalize() function.
• There are no header files in Java (but there is a way to import all the classes
from another package or library).
The SDK provides various tools for working with Java code, as well as the Java VM
and the API.
Some of the tools are:
• Javac (Java compiler)
• Java (Java interpreter i.e. to run a Java program)
• Javadoc (for creating HTML-format documentation from Java source code)
• Jdb (Java Debugger)
With a simple editor such as NotePad and a command line from which to run the
above programs, you can start writing Java programs – but not before you know the
syntax for creating a class.
To make this easier, we will use a development environment for Java that
automatically creates the basic code structure for a class. This is called BlueJ, and it
should be installed on all the lab PCs.
As you get more proficient with writing Java, you may wish to switch to a more
conventional editor. One that we have available is TextPad.
Both BlueJ and TextPad have menu options for compiling and running Java programs
– this is somewhat easier, at least at the beginning, than carrying out these tasks from
the command line.
The Java API consists of many built-in classes and methods that you can use in your
code. This reflects the fact that the Java technology is built using Java itself.
Some of the most commonly used packages are the following:
• Language support package (java.lang) – classes required for implementing
basic features of Java.
• Utilities package (java.util) – classes that provide various utility functions
such as date and time functions.
• Input/Output package (java.io) – classes required for manipulation of
input/output to/from programs
Page 11 of 12
8966466.doc FBE – Computer Science Dept
You will see how these are used as we start writing Java programs.
Page 12 of 12