You are on page 1of 12

8966466.

doc FBE – Computer Science Dept

Mekelle University Faculty of Business & Economics

Computer Science Department

Comp 262: Internet Programming with Java

Handout 1 – Introduction to Java and OO Concepts

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.

1 A Brief History of 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?

Java technology consists of a programming language and a platform on which


programmes can be run.

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.

Figure 1 – compiling and interpreting of a Java program


Compilation only needs to happen one time, whereas interpretation happens every
time the program is executed. The source code for a Java program is saved to one or
more .java files. The compiler generates a .class file for each .java file. A .java file can
be opened and viewed in any text editor, but a .class file is bytecode so it cannot be
viewed in the same way. Figure 1 above illustrates the sequence of these steps.

Every Java interpreter is actually an implementation of something called the Java VM


(Virtual Machine). It is called a Virtual Machine because it is like a computer that
does not physically exist – the VM exists only within the memory of the computer.
The Java bytecodes are like a set of machine code instructions for the Java VM. A
web browser that is Java-enabled is an example of an interpreter.

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

As mentioned above, Java includes a programming language and a platform. A


platform refers to the hardware (PCs, servers, printers etc) and software (operating
systems, application software etc) in which a program is run. So, for example, the
platform we use here in the Computer Science department is a Microsoft Windows
platform that includes Windows 2000 Server, Windows 2000 Professional, Windows
XP and Office 2000 running on IBM-compatible PCs and servers.

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).

The API provides libraries of pre-written, ready-to-use components that provide


standard functionality. A library groups a number of related classes and interfaces
together into what is called a Java package. You can choose what packages you want
to use in your program.

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.

3 Object-Oriented Paradigm (OOP)


Java is based on the OOP (object-oriented paradigm3) – we say that it is an object-
oriented language. You should already be familiar with some OO concepts from your
study of C++. However, there are some differences between Java and C++ - we will
look at these in the next section.

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.

To summarise the OO approach:


• Emphasis on data, not procedure
• Programs are made up of objects
• The data structures reflect characteristics of the objects
• Methods that operate on the data of an object are tied into the data structure
• Data is hidden from external functions
• Objects can communicate with each other through methods
3
'Paradigm' means a set of ideas, a concept or hypothesis.

Page 3 of 12
8966466.doc FBE – Computer Science Dept

• New data and methods can be easily added when necessary


• Takes a bottom-up approach to program design

The basic concepts of OO are:


• Objects and classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic (late) binding
• Message communication

These are explained below.

3.1 Objects and Classes

Objects are the basic runtime entities in an OO program. An object:


• Is an identifiable thing or individual
• Is of significance to the system
• Has characteristic behaviour (i.e. functionality)
• Has states reflected in the attributes (data) held about the object

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 in this class:


Solomon Tekle Silas Bekele Yohannis Michaele
Kebele 16 Kebele 12 Kebele 05
Mekelle Mekelle Mekelle
birr1234 Birr2500 Birr1752
Figure 3 – a class, showing its data and methods, and some objects that belong to the class

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:

Message: Customer 'Solomon Tekle', Amend Address (Kebele 17, Mekelle)

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).

Methods can be public or private.


Public methods:
• Are triggered by receipt of a message from another object
• Have a public interface
• Can be defined at the class or object level
• Can be inherited (see Inheritance below)

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.

3.2 Data Abstraction and Encapsulation


Encapsulation means the wrapping up of data and methods into a single unit (a class).
It also means that the data inside a class is hidden from everything outside the class.
The data can only be accessed by invoking the methods of the class. The internal
workings of the methods are of no concern to other classes in the program.

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

Salary Date Retired


Department Benefits
Rank

Figure 4 – classes inheriting attributes


Inheritance supports the concept of a classification hierarchy. In Figure 4 above, an
Employee 'is a' Person and a Retiree 'is a' Person. Inheritance is generally used where
the relationship between classes can be described as 'is-a' or 'is-a-kind-of'.
The full definition of an Employee includes not only the attributes in the Employee
class but also those in the Person class – because it inherits them. So an Employee has
attributes:
Name, Address, Date of Birth, Salary, Department, Rank.

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

Salary Date Retired


Department Benefits
Rank

Promote ( )

Figure 5 – classes inheriting attributes and methods


It should be noted also that a class can inherit from more than one other class – but
this can cause conflicts in the naming of attributes and methods. It can also cause
confusion in terms of method and attribute inheritance, so it is better to avoid multiple
inheritance if possible.

3.3.1 Abstract and Concrete Classes

We also get the ideas of abstract and concrete classes.


Consider the diagram in Figure 4 again – if the only types of Person that exist were
Employees and Retirees, then the Person class would never have any objects. In this
case, the Person class is abstract – objects of this class can never be instantiated
(created).
If other types of Person besides Employees and Retirees do exist, then the Person
class is concrete – because objects of this class can be instantiated.

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.

3.3.2 Relationships Between Classes

There are three common types of relationships between classes:


• Inheritance – 'is-a'
• Dependence (or Association) – 'uses-a'
• Aggregation – 'has-a'.

We have already seen an example of the inheritance relationship, above.

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

Figure 6 – a class diagram using some UML notations


3.4 Polymorphism
The word polymorphism means the ability to take more than one form. In terms of the
OOP, this means that a particular operation may behave differently for different sub-
classes of the same class.

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

Calculate Area( ) Calculate Area( )

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.

This is the concept of polymorphism.

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.

3.5 Dynamic (late) binding


In the section above, on polymorphism, it was stated that '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'.

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.

3.6 Message communication


An OO program consists of a set of objects that communicate with each other. As we
have already seen, objects can communicate with each other by invoking methods.
So, put simply, the process of programming in an OO language involves these steps:

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

3. Establish communications between the objects

The passing of a message, or invoking of a method, between objects involves


specifying the name of the object, the name of the method and the information
(parameters) to be sent. For example:
Employee1.promote(' Manager')
This call is to an object named 'Employee1' – this is an instance of the Employee
class.
It is invoking the promote() method of the Employee class, passing 'Manager' as the
rank to promote Employee to.

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).

6 Java Development Tools


To get started with writing Java programs, you need to have the Java SDK installed on
your PC. This should be already installed on all PCs in the lab.
The current version of the Java itself is Java 2; the current version of the SDK is
1.4.1_03. The latest version of the SDK can be downloaded for free from the Sun web
site (http://java.sun.com).

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

• Networking Package (java.net) – classes for communicating with other


programs/PCs over networks and internet
• Applet Package (java.applet) – classes that are used to create Java applets.

You will see how these are used as we start writing Java programs.

Notes prepared by: FBE Computer Science Department.

Page 12 of 12

You might also like