I Bca Java Theory Unit I Part 1
I Bca Java Theory Unit I Part 1
Introduction
Object-oriented programming (OOP addresses the problems commonly known as the
software crisis. Software has become very complex which has led to many problems in the
development of large software projects. Many software have failed in the past. The term
‘software crisis’ describes software failure in terms of
Exceeding software budget
Software not meeting clients’ requirements
Bugs in the software
OOP is a programming paradigm (A paradigm is a standard, perspective, or set of ideas)
which deals with the concepts of object to build programs and software applications. It is
modeled around the real world. The world we live in is full of objects. Every object has a
well-defined identity, attributes, and behavior. Objects exhibit the same behavior in
programming. The features of object-oriented programming also map closely to the real-
world features like inheritance, abstraction, encapsulation, and polymorphism.
Need of object oriented programming
There were certain limitations in earlier programming approaches and to overcome these
limitations, a new programming approach was required. We first need to know what these
limitations were.
Procedural Languages
In procedural languages, such as C, FORTRAN, and PASCAL, a program is a list of
instructions. The programmer creates a list of instructions to write a very small program.
As the length of a program increases, its complexity increases making it difficult to
maintain a very large program. In the structured programming, this problem can be
overcome by dividing a large program into different functions or modules, but this results
in other problems. Large programs can still become increasingly complex.
There are two main problems in procedural language:
the functions have unrestricted access to global data and
they provide poor mapping to the real world.
Here are some other problems in the procedural languages. Computer languages generally
have built-in data types: integers, character, float, and so on. It is very difficult to create
a new data type or a user-defined data type. For example, if we want to work with
dates or complex numbers, then it becomes very difficult to work with built-in types.
Creating our own data types is a feature called extensibility: we can extend the
capabilities of a language. Procedural languages are not extensible. In the traditional
languages, it is hard to write and maintain complex results.
Object-Oriented Modeling
In the physical world, we deal with objects like person, plane, or car. We deal with
similar objects in OOP. Objects are defined by their unique identity, state, and
behavior. The state of an object is identified by the value of its attributes and
behavior by methods.
o Attributes
Attributes define the data for an object.
Every object has some attributes.
Different types of objects contain different attributes or
characteristics.
For example, the attributes of a student object are name, roll number,
and subject;
and the attributes for a car object would be color, engine power,
number of seats, etc.
o Behavior
The response of an object when subjected to stimulation is called
its behavior.
Behavior defines what can be done with the objects and may
manipulate the attributes of an object.
For example, if a manager orders an employee to do some task, then he
responds either by doing it or not doing it.
Behavior actually determines the way an object interacts with
other objects.
We can say that behavior is synonym to functions or methods: we call
a function to perform some task. For example, an Employee class will
have functions such as adding an employee, updating an employee
details, etc.
Another real life example of Abstraction is ATM Machine; All are performing operations on
the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we
can't know internal details about ATM.
Inheritance
Inheritance is the way to adopt the characteristics of one class into another class.
Here we have two types of classes: base class and subclass.
There exists a parent–child relationship among the classes.
When a class inherits another class, it has all the properties of the base class and it
adds some new properties of its own.
We can categorize vehicles into car, bus, scooter, ships, planes, etc.
The class of animals can be divided into mammals, amphibians, birds, and so on.
The principle of dividing a class into subclass is that each subclass shares common
characteristics with the class from where they are inherited or derived.
Cars, scooters, planes, and ships all have an engine and a speedometer.
These are the characteristics of vehicles.
Each subclass has its own characteristic feature,
e.g., motorcycles have disk braking system,
while planes have hydraulic braking system.
A car can run only on the surface, while a plane can fly in air and a ship sails over
water (see Fig. on inheritance).
7. Which principle of OOPs helps in reusability of code? Explain with example. (4 marks)
8. How is data hiding accomplished in Java? Explain with a simple diagram. (4 marks)