Java - Chapter 3
Java - Chapter 3
Java - Chapter 3
Software objects are conceptually similar to real-world objects: they too consist of a state and
related behavior. An object stores its state in fields (also called variables in programming
languages). An object also has methods (functions in programming languages). Methods
operate on an object's internal state and serve as the primary mechanism for object-to-object
communication. Hiding internal state and requiring all interaction to be performed through an
object's methods is known as data encapsulation — a fundamental principle of object-
oriented programming.
Just as in C programming language we can declare variables of different data types such
as int, float, char, etc., in Java, the entire set of data and code of an object can be made a
user-defined data type using the concept of class. A class may be thought of as a ‘data
type’ and an object as a ‘variable’ of that type. Once a class has been defined, we can
create objects belonging to that class. A class is thus a collection of objects of similar
type. E.g., banana, apple, grapes and mango are members of the class fruit.
Data abstraction refers to the act of representing essential features without including the
background details or explanations. Abstraction means simplifying complex reality.
3. Code re-use: If an object already exists (perhaps written by another software developer),
you can use that object in your program. This allows specialists to implement/test/debug
complex, task-specific objects, which you can then trust to run in your own code.
4. Pluggability and debugging ease: If a particular object turns out to be problematic, you
can simply remove it from your application and plug in a different object as its
replacement. This is analogous to fixing mechanical problems in the real world. If a bolt
breaks, you replace it, not the entire machine.
3. Inheritance
Inheritance is the process by which objects of one class acquire the properties of objects of
another class. Defining new classes from the existing one is called inheritance.. The new
class will get all the methods and properties of the existing class. The new class is known as
the sub class / child class / derived class. The existing class is known as the super class,
parent class / base class. Object-oriented programming allows classes to inherit commonly
used state and behavior from other classes. Inheritance is implied by “is-a” relationship.
In OOP, the concept of inheritance provides the idea of reusability. That is, we can add
features to an existing class without modifying it. We derive a new class from an already
existing class. The new class has the features of the old class as well.
For example, helicopter is a part of the class aircraft. This class aircraft can have other
subdivisions such as passenger aircraft, cargo plane, etc. In real life a manager is an
employee. So in OOPL, a manager class is inherited from the employee class.
4. Polymorphism
Polymorphism means the ability to take more than one form. A mathematical operation may
have different behaviour in different instances. This behaviour depends upon the type of data
used in the operation. E.g., consider the operation of addition (+). When applied on two
numbers, we get the sum of the two numbers (3 + 4 = 7). But when the same operator is
Page 2 of 5 Java - Chapter 3
The Java Programming Language
applied on strings, it produces a third string by concatenation (e.g., “INTER” + “NET” gives
“INTERNET”).
Polymorphism allows objects having different internal structures to share the same external
interface.
5. Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the
call. Dynamic binding means that the code associated with a given procedure call is not
known until the time of the call at runtime.
Consider a variable N. If the type of variable is implicitly associated by its contents, we say
that N is dynamically bound to a data type T. This associative process is called dynamic
binding.
Consider the following example which is only possible with dynamic binding:
6. Message Communication
Message passing involves specifying the name of the object, the name of the method and the
information to be sent. E.g., consider the statement:
Employee.salary(name);
Here, Employee is the object, salary is the message (method) and name is the parameter that
contains information.
What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces. Conceptually
you can think of packages as being similar to different folders on your computer. You might
keep HTML pages in one folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be composed of hundreds or
thousands of individual classes, it makes sense to keep things organized by placing related
classes and interfaces into packages.
The Java platform provides an enormous class library (a set of packages) suitable for use in
your own applications. This library is known as the "Application Programming Interface", or
"API" for short. Its packages represent the tasks most commonly associated with general-
purpose programming. For example, a String object contains state and behavior for
character strings; a File object allows a programmer to easily create, delete, inspect,
compare, or modify a file on the filesystem; a Socket object allows for the creation and use
of network sockets; various GUI objects control buttons and checkboxes and anything else
related to graphical user interfaces. There are literally thousands of classes to choose from.
This allows you, the programmer, to focus on the design of your particular application, rather
than the infrastructure required to make it work.
QUESTIONS
Answers to Questions