1.1 Introduction to Java
The most prominent use of computers is to solve problems quickly and accurately. The solution adopted to solve a problem is provided as a sequence of instructions or specifications of activity which enables a user to achieve the desired result. For example, a problem could be about how to obtain a particular food item from a restaurant with the least effort. Placing an order to the nearest restaurant over the phone for the food item is one of the solutions to this problem.
The knowledge of a domain plays an important role while solving a problem. A domain is defined as the field of business or technology to which a problem belongs. For example, in the restaurant scenario, the knowledge of all the restaurants in nearby area forms the domain knowledge.
The knowledge about the restaurant as well as the type of food served can also be considered as domain knowledge in this case. Having a good knowledge of the domain helps to solve the problem quickly. In the scenario described, if the restaurant specializes in Mexican cuisine and you call them to order Chinese food, then it would be a waste of time and effort. Here, if you had known the name of the nearest restaurant that serves only Chinese food, it would have saved your time and effort. Hence, having adequate domain knowledge before designing a problem solution is always recommended.
The solution for solving a problem in the field of information technology is achieved by developing software applications. A software application can be defined as a collection of programs that are written in high-level programming languages to solve a particular problem. In other words, it is a collection of pre-written programs to solve specific application needs of end users.
Figure 1.1 shows the development of software application as a solution for managing various operations in a banking domain. These operations are related to accounts, customers, payments, and various other transactions that are carried out in a banking domain.
As shown in figure 1.1, the development of software application is done using a programming language. A programming language is used as a medium for communicating the instruction to the computer. The programming language enforces a particular style of programming that is referred to as a programming paradigm.
1.1.1 Structured Programming Paradigm
Earlier programming languages, such as C, Pascal, Cobol, and so forth followed structured programming paradigm. In structured programming paradigm, the application development is decomposed into a hierarchy of subprograms.
Figure 1.2 displays an application developed for bank with some of the bank activities broken down into subprograms.
The subprograms are referred to as procedures, functions, or modules in different structured programming languages. Each subprogram is defined to perform a specific task. Thus, to manage various bank processes, the software application is broken down into subprograms that will interact with each other to solve the business needs.
The main emphasis of structured programming languages was to decompose applications into procedures to solve complex problems, whereas data was given less importance. In other words, the efficiency of programs was measured on time and memory occupancy, rather than on how to control the data shared globally between the procedures. Hence, most of the development efforts in the structured programming languages were spent on accomplishing the solution rather than focusing on the problem domain.
This also often led to a software crisis, as the maintenance cost of complex problems became high and the availability of reliable software was reduced.
Figure 1.3 shows handling of data in structured programming languages.
1.1.2 New Paradigm: Object-oriented Programming Paradigm
The growing complexity of software required a change in the programming style. Some of the features that were aimed for are as follows:
- Development of reliable software at reduced cost.
- Reduction in the maintenance cost.
- Development of reusable software components.
- Completion of the software development within the specified time interval.
These features resulted in the evolution of object-oriented programming paradigm. The object-oriented programming paradigm enables you to develop complex software applications for different domain problems with reduced cost and high maintenance. The software applications developed using object-oriented programming paradigm is designed around data, rather than focusing only on the functionalities.
The object-oriented programming paradigm basically divides the application development process into three distinct activities. These are as follows:
Object-oriented Analysis (OOA) – This process determines the functionality of the system. In other words, it determines the purpose of developing an application.
**Object-oriented Design (OOD) **– This is the process of planning a system in which objects interact with each other to solve a software problem.
Object-oriented Programming (OOP) – This is the process that deals with the actual implementation of the application.
Figure 1.4 shows different activities involved in the object-oriented software development.
As shown in figure 1.4, the OOA and OOD phases deal with the analysis and designing of the software application. These activities are carried out manually using a modeling language, Unified Modeling Language (UML). The UML contains graphic notations that help to create visual models in the system. The actual implementation of these visual models is done using an OOP language.
An OOP language is based on certain principles that are as follows:
- Object – Represents an entity which possesses certain features and behaviors.
- Class – Is a template that is used to create objects.
- Abstraction – Is a design technique that focuses only on the essential features of an entity for a specific problem domain.
- Encapsulation – Is a mechanism that combines data and implementation details into a single unit called class. It also secures the data through a process called data hiding where a user cannot manipulate the data directly.
- Inheritance – Enables the developer to extend and reuse the features of existing classes and create new classes. The new classes are referred to as derived classes.
- Polymorphism – Is the ability of an object to respond to same message in different ways.
The main building blocks of an OOP language are classes and objects. They allow you to view the problem from the user’s perspective and model the solution around them. Hence, a clear understanding of classes and objects is important for application development using OOP language.
1.1.3 Concept of an Object
An object represents a real-world entity. Any tangible or touchable entity in the real-world can be described as an object.
Figure 1.5 shows some real-world entities that exist around everyone.
As shown in figure 1.5, the real-world entities, such as a car, flower, bike, or person are treated as objects. Each object has some characteristics and is capable of performing certain actions. Characteristics are defined as attributes, properties, or features describing the object, while actions are defined as activities or operations performed by the object.
For example, the properties of an object, Dog, are as follows:
An object also executes actions. Thus, the actions that a Dog can perform are as follows:
The concept of objects in the real-world can be extended to the programming world where software ‘objects’ can be defined.
Like its real-world counterpart, a software object has state and behavior. The ‘state’ of the software object refers to its characteristics or attributes, whereas the ‘behavior’ of the software object comprises its actions.
Figure 1.6 shows a software object, such as a Car with its state and behavior.
As shown in figure 1.6, the object ** Car** will have the state as color, make, or model. On the other hand, the behavior of a car includes: driving, changing gear, increasing speed, and applying brakes.
The advantages of using objects are as follows:
- They help to understand the real-world.
- They map attributes and actions of real-world entities with state and behavior of software objects.
1.1.4 Defining a Class
In the real-world, several objects have common state and behavior. For example, all car objects have attributes, such as color, make, or model. These objects can be grouped under a single class. Thus, it can be defined that a class is a template or blueprint which defines the state and behavior for all objects belonging to that class.
Figure 1.7 shows a car (any car in general) as a class and a Toyota car (a specific car) as an object or instance created for that class.
Typically, in an OOP language, a class comprises fields and methods, collectively called as members. The fields are known as variables that depict the state of objects. The methods are known as functions that depict the behavior of objects.
Figure 1.8 shows the design of a class with fields and methods in an OOP language.
Table 1.1 shows the difference between a class and an object.