0% found this document useful (0 votes)
33 views11 pages

I Bca Java Theory Unit I Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

I Bca Java Theory Unit I Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAMAL MOHAMED COLLEGE (AUTONOMOUS), TIRUCHIRAPPALLI-620020.

DEPARTMENT OF COMPUTER APPLICATIONS [WOMEN]

Hours/ Credit Marks for Evaluation


Semester Course Code Course Category
Week s CIA ESE Total
II 23UCA2CC3 CORE – III 5 5 25 75 100

Course Title Programming in Java


UNIT-I
Introduction – Need of object oriented programming – Principles of Object Oriented
Language – Applications of OOP - History of Java – Java Essentials – Java Virtual Machine
– Java Features - Java Programming Constructs: Variables – Primitive Data Types –
Identifier – Literals – * Operators – Expressions * – Primitive Type conversion and Casting –
Flow of Control

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.

Principles of Object Oriented Language


OOP languages follow certain principles such as class, object, and abstraction. These
principles map very closely to the real world.
Classes
 A class is defined as the blueprint for an object. It serves as a plan or a template.
 The description of a number of similar objects is also called a class.
 An object is not created by just defining a class.
 It has to be created explicitly.
 Classes are logical in nature.
 For example, furniture does not have any existence but tables and chairs do exist.
 A class is also defined as a new data type, a user-defined type which contains two
things: data members and methods.
Objects
Objects are defined as the instances of a class, e.g. table, chair are all instances of the class
Furniture. Objects of a class will have same attributes and behavior which are defined in that
class. The only difference between objects would be the value of attributes, which may vary.
Objects have unique identity, state, and behavior. There may be several types of objects:
 Creator objects: Humans, Employees, Students, Animal
 Physical objects: Car, Bus, Plane
 Objects in computer system: Monitor, Keyboard, Mouse, CPU, Memory
Abstraction
Abstraction in Java refers to hiding the implementation details of a code and exposing only
the necessary information to the user.
 In simple terms, abstraction “displays” only the relevant attributes of objects
and “hides” the unnecessary details.
Can you classify the following items?
• Elephant • CD player
• Television • Chair
• Table • Tiger
How many classes do you identify here?
 The obvious answer anybody would give is three, i.e., Animal, Furniture, and
Electronic items.
 But how do you come to this conclusion?
 Well, we grouped similar items like Elephant and Tiger and focused on the generic
characteristics rather than specific characteristics.
 This is called abstraction.
Everything in this world can be classified as living or non-living and that would be the
highest level of abstraction.
 When we are driving a car, we are only concerned about driving the car like start/stop
the car, accelerate/ break, etc.
 We drive cars without knowing the internal details about how the engine works and
how the car stops on applying brakes.
 We are happy with the abstraction provided to us, e.g., brakes, steering, etc. and we
interact with them.
 We are not concerned about how the actual start/stop mechanism or accelerate/brake
process works internally.
In real life, human beings manage complexity by abstracting details away. In
programming, we manage complexity by concentrating only on the essential
characteristics and suppressing implementation details.
Abstraction shows only important things to the user and hides the internal details, for
example, when we ride a bike/car, we only know about how to ride bikes/cars but cannot
know about how it works? And also we do not know the internal functionality of a bike/car.

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

 Inheritance aids in reusability.


 When we create a class, it can be distributed to other programmers which they
can use in their programs.
 This is called reusability.
 Suppose someone wants to make a program for a calculator, he can use a predefined
class for arithmetic operations, and then he need not define all the methods for these
operations.
 This is similar to using library functions in procedural language. In OOP, this can be
done using the inheritance feature.
 A programmer can use a base class with or without modifying it.
 He can derive a child class from a parent class and then add some additional features
to his class.
The real life example of inheritance is child and parents, all the properties of father are
inherited by his son.
Encapsulation
 Encapsulation is one of the features of object-oriented methodology.
 The process of binding the data procedures into objects to hide them from the outside
world is called encapsulation.
 It provides us the power to restrict anyone from directly altering the data.
 Encapsulation is also known as data hiding.
 An access to the data has to be through the methods of the class.
 The data is hidden from the outside world and as a result, it is protected.
 The details that are not useful for other objects should be hidden from them.
 This is called encapsulation.
For example, an object that does the calculation must provide an interface to obtain the result.
However, the internal coding used to calculate need not be made available to the requesting
object.
Polymorphism
Polymorphism simply means many forms.
 It can be defined as the same thing being used in different forms.
 For example, there are certain bacteria that exhibit in more than one morphological
form.
 In programming, polymorphism is of two types: compile-time and runtime
polymorphism.
 Runtime polymorphism, also known as dynamic binding or late binding, is used to
determine which method to invoke at runtime.
 The binding of method call to its method is done at runtime and hence the term late
binding is used.
 In case of compile-time polymorphism, the compiler determines which method
(from all the overloaded methods) will be executed.
 The binding of method call to the method is done at compile time.
 So the decision is made early and hence the term early binding.
 Compile-time polymorphism in Java is implemented by overloading and
 Runtime polymorphism by overriding.
 In overloading, a method has the same name with different signatures. (A signature is
the list of formal argument that is passed to the method.)
 In overriding, a method is defined in subclass with the same name and same signature
as that of parent class.
 This distinction between compile-time and runtime polymorphism is of method
invocation.
 Compile-time polymorphism is also implemented by operator overloading which is a
feature present in C++ but not in Java.
 Operator overloading allows the user to define new meanings for that operator so that it
can be used in different ways.
The operator (+) in Java is however an exception as it can be used for addition of two integers
as well as concatenation of two strings or an integer with a string. This operator is overloaded
by the language itself and the Java programmer cannot overload any operator.
Answer the following questions:
1. What is software crisis? (2 marks)

2. What are the two main problems in procedural languages? (2 marks)

3. Define extensibility. (2 marks)

4. What are objects? What are attributes and behavior? (4 marks)

5. Define classes. (2 marks)

6. Define Abstraction. (2 marks)

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)

9. Draw a diagram to represent different types of polymorphism? (4 marks)

10. Explain in detail the principles of OOPs. (10 marks)

You might also like