You are on page 1of 9

Chapter One – Introduction to Java and

OO Concepts
Basics of programming
Computer is a device that performs tasks based on given set of step-by-step instructions
(procedures)
These set of procedures (step-by-step instructions) are called algorithms also called computer
programs or soft wares.
The way in which we tell/give the set of procedures or programs to a computer are called
programming languages. Examples are: C++, java, C#...

In order to write a program you need to go through several stages. These are
1. Type the text of the program into the computer
2. Translate the program text into a form the computer can use
3. Run the translated program
Programming paradigms
There are different types of programming languages based on the philosophy they use. There are
two widely known programing paradigms. These are:
1. Structural Programming Paradigm, and
2. Object-Oriented Programming Paradigm.
In Structural Programming, the program structure is based on operations not on data. The
following are programing techniques that follow the structural approach.
• Unstructured Programming
Usually, people start learning programming by writing small and simple programs
consisting only of one main program. Here “ main program” stands for a sequence of
commands or statements which modify data which is global throughout the whole program (Fig.
1.1).

Figure 1.1: Unstructured programming. The main program directly operates on global data.
This programming technique provides tremendous disadvantages once the program gets
sufficiently large. For example, if the same statement sequence is needed at different locations
within the program, the sequence must be copied. This has lead to the idea to extract these
sequences, name them and offering a technique to call and return from these procedures.
• Procedural Programming
With procedural programming you are able to combine returning sequences of
statements into one single place. A procedure call is used to invoke the procedure. After the

Page 1 of 9
sequence is processed, flow of control proceeds right after the position where the call was made
(Fig. 1.2).

Figure 1.2: Execution of procedures. After processing flow of controls proceed where the call
was made.
With introducing parameters as well as procedures of procedures (sub procedures)
programs can now be written more structured and error free. For example, if a procedure is
correct, every time it is used it produces correct results. Consequently, in cases of errors you can
narrow your search to those places which are not proven to be correct. Now a program can be
viewed as a sequence of procedure calls. The main program is responsible to pass data to the
individual calls, the data is processed by the procedures and, once the program has finished, the
resulting data is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a
tree, as shown in Fig. 1.3 for a program with no sub procedures.

Figure 1.3: Procedural programming. The main program coordinates calls to procedures and
hands over appropriate data as parameters.
To sum up: Now we have a single program which is divided into small pieces called
procedures. To enable usage of general procedures or groups of procedures also in other
programs, they must be separately available. For that reason, modular programming allows
grouping of procedures into modules.
• Modular Programming
With modular programming procedures of a common functionality are grouped together
into separate modules. A program therefore no longer consists of only one single part. It is now
divided into several smaller parts which interact through procedure calls and which form the
whole program (Fig. 1.4).

Page 1 of 9
Figure 1.4: Modular programming. The main program coordinates calls to procedures in
separate modules and hands over appropriate data as parameters.
Each module can have its own data. This allows each module to manage an internal state
which is modified by calls to procedures of this module. However, there is only one state per
module and each module exists at most once in the whole program.
Problems with Modular Programming
The previous section shows, that you already program with some object-oriented
concepts in mind. However, there are some problems with modular programming, which
outlined below.
- Explicit Creation and Destruction
Every time you want to use a module, you explicitly have to declare a handle and
perform a call to a procedure to create a module instance. After the use of the module you must
explicitly call a procedure to destroy the module instance, with the handle of the module you
want to be destroyed.
- Decoupled Data and Operations
Decoupling of data and operations leads usually to a structure based on the operations
rather than the data: Modules group common operations together. You then use these operations
by providing explicitly the data to them on which they should operate. The resulting module
structure is therefore oriented on the operations rather than the actual data. One could say that
the defined operations specify the data to be used. In object-orientation, structure is organized
by the data. You choose the data representations which best fit your requirements. Consequently,
your programs get structured by the data rather than operations. Thus, it is exactly the other
way around: Data specifies valid operations.
- Missing Type Safety
Let us consider a list example, using a special type ANY to allow the list to carry any
data we like. This implies, that the compiler cannot guarantee for type safety. Consider the
following example which the compiler cannot check for correctness:
PROCEDURE foo() BEGIN
SomeDataType data1;
SomeOtherType data2;

Page 2 of 9
list_handle_t myList;
myList <- list_create();
list_append(myList, data1);
list_append(myList, data2); /* Oops */
...
list_destroy(myList);
END
It is your responsibility to ensure that your list is used consistently. A possible solution is
to additionally add information about the type to each list element. However, this implies more
overhead and does not prevent you from knowing what you are doing. What we would like to
have is a mechanism which allows us to specify on which data type the list should be defined.
The overall function of the list is always the same, whether we store apples, numbers, cars or
even lists. Therefore it would be nice to declare a new list with something like:
list_handle_t<Apple> list1; /* a list of apples */
list_handle_t<Car> list2; /* a list of cars */
The corresponding list routines should then automatically return the correct data types.
The compiler should be able to check for type consistency.
• Object – Oriented Programming
Object-oriented programming solves some of the problems mentioned with the other
programming techniques. In contrast to the other programming techniques, we now have a web
of interacting objects, each house-keeping its own state (Fig. 1.5).

Figure 1.5:Object-oriented programming. Objects of the program interact by sending messages


to each other.

Programming languages have traditionally divided the world into two parts--data and
operations on data. Data is static and immutable, except as the operations may change it. The
procedures and functions that operate on data have no lasting state of their own; they're useful
only in their ability to affect data.

Object-oriented programming groups operations and data into modular units called
objects and lets you combine objects into structured networks to form a complete program. In an

Page 3 of 9
object-oriented programming language, objects and object interactions are the basic elements
of design.

Every object has both state (data) and behaviour (operations on data). In that, they're
not much different from ordinary physical objects. It's this resemblance to real things that gives
objects much of their power and appeal. They can not only model components of real systems,
but equally as well fulfil assigned roles as components in software systems.

Each object is responsible to initialize and destroy itself correctly. Consequently, there is
no longer the need to explicitly call a creation or termination procedure.

Object oriented programming(OOP): is a programming paradigm/modelling approach that


represents concepts as "objects" that have.

o data fields (attributes that describe the object) and


o Associated procedures known as methods/functions that operate or manage the
data.

Examples of Object Oriented Programming languages include: C++, Java, C#...


Object-Oriented Paradigm (OOP)
In OO, data is tied closely to the functions that operate 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.
In OO approach:
 Emphasis is on data, not procedure
 Programs are made up of objects
 Data is hidden from external functions, functions not part of the object.
 Objects can communicate with each other through their methods
 New data and methods can be easily added when necessary...
The basic concepts of OO are:
 Objects and classes

Page 4 of 9
 Data abstraction and encapsulation
 Inheritance
 Polymorphism
Objects and Classes
Objects are the basic runtime entities in an OO program. An object is an identifiable thing or
individual containing
 characteristic behaviour (i.e. functionality) and
 states reflected in the attributes (data) held about the object
A class is:
A class is the definition of a set of objects or a class is the design of an object. A class models a
static view of objects which exist in the system users' world.
All objects created by a given class will have similar structure in terms of their attributes,
associations and functions/methods. E.g. customer class defines all customer objects. So, all
customers have the same attributes and methods.
NB: In general,
o A class is a user defined a data-type – once a class has been defined, any number of
objects belonging to that class can be created.
o An object is then is a variable in which its data-type is the user defined class.
Example, a class named ‘Customer’ has 3-attributes and 2-methods (functions).

Class
Customer
Name
Address Attributes (data)

Current Balance
Amend Address ( ) Methods
Adjust Balance ( )

Examples of Objects created using this class could be:

Solomon Tekle Lome keza Yohannis Michaele


Kebele 16 Kebele 12 Kebele 05
DB AA DB
birr1234 Birr2500 Birr1752

Fig. 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 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, AA)
Page 5 of 9
Sending this message is known as invoking or calling the method 'Amend Address'.
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.
Inheritance
Inheritance is the way in which objects of one class get the properties of objects of another class.
This includes the data/ attributes and the methods.
Polymorphism
The word polymorphism means the ability to take more than one form/structure. A particular
operation/method may behave differently for different number of inputs.

The Benefits of OO
 Reuse of existing classes and eliminate redundant code using inheritance.
 Encapsulation (hiding of 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. Therefore the structure of the system is more likely to be meaningful to users.
 The work for a given 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....

Features of Java programming language


 Object Oriented: In Java, everything is an Object.
 Compiled and interpreted: programming languages are either compiled or interpreted.
But java is both compiled and interpreted. The Java compiler translates the program into
an intermediate language called Java byte-codes. Java byte-codes are platform-
independent – this means they can be run on different operating systems (e.g. Windows,
Linux, Mac...). There are different Java interpreters for different platforms. These
interpreters parse and run each of the Java byte-code instructions to each machine code

Page 6 of 9
in which the machine can understand. The interpreters are usually called 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 byte-codes are like a
set of machine code instructions for the Java VM.

 Platform independent & Portable: when Java is compiled, it is not compiled into
platform specific machine, rather into platform independent byte code. Then the
interpreter interprets the byte-code into platform dependent format also called machine
code.
 etc…

Byte-code Machine-code

Fig. 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. The source code “.java” file is opened and viewed in
any text editor, but the byte-code file which is “.class” file cannot be viewed in the same way.
The big benefit of the Java is that the program code can be written and compiled once, and then
run on many different platforms.

Fig. a Java program can be compiled once and then run on different platforms

Page 7 of 9
NB: A platform refers to the
 Hardware (PCs, servers, printers etc) and
 Software (operating systems, application software etc) in which a program is run.
JAVA development Environment
Java environment includes a many number of development tools and hundreds of classes &
methods.
 The development tools are part of the system known as java development kit (JDK). And
 The set of classes and methods are part of the Java Standard Library(API), also known as
Application Programming Interface(API).
Java Development Kit (JDK)
 Comes with a collection of tools that are used for developing and running java programs.
They include
o javac --- java compiler
o java --- java interpreter
o jdb --- java debugger etc...
So, to develop java programs, we need to install jdk. And then we can use our own text editor to
write java source code. Or instead of using our own text editor, another option we can use is
installing other tools or APIs like Net-beans and Eclipse to easily code, compile and run java
programs.
Main Java API packages include:
The Java API consists of many built-in classes and methods that we can use in our code. These
classes and methods/functions are again grouped into a form packages. Package is a collection
of related classes together. 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
 Networking Package (java.net) – classes used for communicating with other
programs/PCs over networks and internet
 Applet Package (java.applet) – classes that are used to create Java applets.

Page 8 of 9

You might also like