You are on page 1of 8



Chapter Objectives: Upon completion of this chapter, you would have learnt: Introduction to Objected-Oriented Language Concept of Object-Oriented Programming Benefits of Object-Oriented Programming Introduction to Java Programming Resources necessary to create a Java Program Structure of a Java Program Different Types of Access Specifiers



1.1 Object-Oriented Programming Language

Objected-oriented programming is an extension of procedural programming. It involves creating program components as objects that are similar to concrete objects in the real world that are manipulated to achieve a desired result. When writing object-oriented programs, it involves both creating objects and creating applications that use those objects.

The objected-oriented approach is a new approach to develop software, which can be widely used in business application. With this approach, data and the program(or procedure) can be packaged into a single unit called an object. An object is an item that can contain both data, which are known as attributes or variables, and procedures, which contains activities that read or manipulate the data.

Object-oriented programming is a conceptual approach to designing programs. Languages like C+ and Java have intrinsic features, which apply this approach.

1.2 Object-Oriented Programming Principle

A program developed in an objected-oriented approach is based on the thre basic concepts, which are encapsulation, inheritance and polymorphism.

Encapsulation is a mechanism that binds together attributes and methods into a single object, and keeps both safe from interference or misuse, that is, the details of the object are encapsulated or hidden, from the user. The user knows the method that can be requested of the object but does not know the specifics of how the method is performed. Encapsulation hides the details of the object, it sometimes is called information hiding.

An object may be part of a large category of objects called a class. Every object in a class shares similar methods and attributes as the original object. Each class can have one or more lower levels called subclasses. The higher-level class is called a superclass. Each subclass inherits the methods and attributes of the objects in its superclass. This concept of low levels inheriting methods and attributes of higher levels is called inheritance.



Polymorphism is a feature that allows one interface to be used for a general class of action, that is, the ability of an object to take many forms or identities.

1.3 Benefits of Object-Oriented Programming

Java removes several features considered unsafe and also frees memory. Hence, it cuts down on the chances of making mistakes in the program.

A major benefit of the object-oriented approach is the ability to reuse the defined objects and modify existing objects. The object-oriented approach to program development saves programming time. Besides, adaptability is also another benefit as it is able to fit in different environment. Another advantage of using object-oriented approach is reliability as it is able to operate correctly under al conditions.

When using this approach, however, the development team must use different analysis, design, and programming techniques and tools than those used in structured programming development.

1.4 Introduction to Java Programming

Java programming language is an object-oriented language that is used both for generalpurpose business programs and interactive World Wide Web(WWW)-based Internet programs developed by Sun Microsystems. The first version of Java was introduced in 1995 and the language was then called Oak.

The language is modeled after the C+ programming language and that it has also eliminated some of the most difficult features to understand in C++. It is similar in syntax with C+ and similar in semantics to SmallTalk.

Java can be used for developing applets, which resides on WW

servers downloaded by

a browser to a clients system, and run by the browser. It minimizes download time as the size of the applet program is small and is invoked by the HTML(HyperText Markup



Language) web page. It can also be used for developing applications, which are standalone program that does not require any Web browser to execute.

A program written in Java is platform independent, which means one program version will be able to run on different platforms. The Java source program is compiled into Java Virtual Machine code called bytecode. With an interpreter, each Java bytecode instruction is parsed and run on the computer. The compilation of the program happens just once but interpretation occurs each time the program is executed.

1.5 Resources used to create a Java Program

To create a Java source program, the following are the software required: A text editor e.g. notepad, wordpad etc. Java Development Kit, which includes the executable files needed for compiling and executing the program etc. Other Java Development Tools e.g. Borland Jbuilder.

1.6 Structure of a Java Program

The general structure of a Java source code is as follows:

/ import packages public class classname { / instance variables Access specifier data type cariablename1; Access specifier data type variablename2;

/ methods Access specifier data type methodname1(parameter list) { / function body 1



Access specifier data type methodname2(parameter list) {/ function body 2 } }

The following is an example of a basic Java application: public class HelloWorld { public static void main(String[] args) { / this function outputs Hello World System.out.println(Hello World) ; } }

The following is the explanation of the above source code: Line 1: The keyword class defines a new class and first is the name of the class. Line 3: It defines a method called main(). When a Java application is executed, the compiler always executes the main() method first. This main() method is simply a starting place for the interpreter. The method header includes the following: public This keyword is an Access modifier, which allows the main() method to be accessed by codes outside this class. static This keyword allows the main() method to be Access directly, without needing to create a particular instance of the class. This is necessary since the main() is called by the Java interpreter before any objects are made. void This keyword tells the compiler that the main() method does not return any value when it is called. The main() method produce outputs that does not send any value back to any other method that might use it. String[] args This represents an argument passed to the main() method. This declares a parameter named args, which is an array of instance of the class String.



String represents a Java class that can be used to represent character strings. The identifier args is used to hold any Strings that might be sent to the main() method. The identifier need not be named args it could be any legal Java identifier but the name args is traditional. Line 5: The // double slash sign signals comment. Everything that follows after the double slash becomes a comment. The compiler will ignore the contents of the comment. Line 6: This statement displays the string Hello World and positions the cursor on the next line, and stands ready for additional output. System is a predefined class that provides Access to the system, and out is one of the System objects, which represents the screen. The println() method displays the string, which is passed to it on the screen. Any literal string in Java appears between double quotation marks. The dots(periods) in the statement are used to separate the names of the class, object and the method.

The entire class definition, including al its members, will be placed between { and }. The use of { and } (curly parenthesis) can be used to separate program blocks.

The output of the above sample program is shown below:



1.7 Access Specifier

An Access specifier defines the circumstances under which a class or class members in programs can be accessed. The Access specifiers of Java can be any of the following: public, private, protected and also default.

Most often, methods are given public access. When a method is specified as public, it can be Access by any class. If a method is specified as private, it can only be accessed by members of its own class. Without specifying an Access specifier provides default access. The default Access level is similar to the public Access level, where the member is visible to al classes in the same package. When a class member is specified as protected, the member can be accessed by al subclasses of this class in any package.

The table below summarises the Access control:

Public Same class Subclass in the same package Non subclass in the same package Subclass in a different package Non subclass in a different package Yes Yes Yes Yes Yes

Protected Yes Yes

Default Yes Yes

Private Yes No












1.8 Programming Exercises

1. State and explain the basic concepts of Object-Oriented Programming. 2. List the resources need to write a Java program. 3. Explain what is Access specifier. 4. State any 2 Access specifiers and explain the difference between the two types of Access specifiers. 5. Write a simple program that displays the following: Main Menu 1. Data Entry 2. Display 3. Exit Enter a choice :

6. The output statement, which is System.out.println(); is always used to display output. Given that System is a class, what kind of an object does out refer to?