Introduction to Java
Introduction to Java
What is an Object
As the name “Object Oriented” implies, objects are key to understanding object-oriented
technology. We can look around and see many real-world objects viz. dog, desk, pen,
television, bicycle etc.
Real world Objects share two characteristics viz. State and Behavior.
e.g. dogs have state (name,breed etc) and dogs have behavior (barking etc). Bicycles have
state (current gear, number of gears, two wheels) and behavior (braking, accelerating, etc).
Consider for a moment a manufacturing plant that processes composite materials for
making diverse items like bicycle frames and airplane wings. Manufacturing plants are often
divided into separate shops: mechanical, electrical and so forth. Shops are further divided into
cells, and in each cell, we have some collection of machines. Along a manufacturing line, we
might find vats containing raw materials, which are used in a chemical process to produce
blocks of composite materials, and which in turn are formed and shaped to produce end items
such as bicycle frames and airplane wings. Each of the tangible things that have been
mentioned thus far is an object.
Definition
An Object is a software bundle of variables and related methods. Software objects are
often used to model real-world objects you find in every day life.
OR
An Object has state, behavior and identity; the structure and behavior of similar objects
are defined in their common class; the terms instance and objects are interchangeable.
What is a Class
The concept of a class and an object are tightly interwoven, for we cannot talk an object
without regard for its class. However there are important differences between these two terms.
Whereas an object is a concrete entity that exists in time and space, a class represents only
an abstraction, the “essence of an object”. Thus we may speak of a class mammal, which
represents the characteristics common to all mammals. To identify a particular mammal in this
class, we must speak of “this mammal” or “that mammal”.
1
Object Oriented Fundamentals
Definition
A class is a set of objects that share a common structure and a common behavior.
Consider for a moment the similarities and the differences among the following classes
of objects: flowers, diases, red roses, yellow roses, petals and ladybugs. We can make the
following observations.
A daisy is a kind of flower.
A rose is a kind (different) kind of flower
Red roses and yellow roses are both kind of roses.
A petal is a part of both kind of flowers.
Lady bugs eats certain pests such as alphids, which may be infesting certain kinds of
flowers.
From this example, we conclude that classes, like objects do not exist in isolation. Rather
for a particular problem domain, the key abstraction are usually related in a variety of
interesting ways, forming the class structure for our design.
We establish relationship between two classes for these reasons:
1) A class relationship might indicate some sort of sharing. E.g daises and roses are both
kind of flowers, meaning that both have brightly coloured petals, both emit a fragrance
etc.
2) A class relationship might indicate some kind of semantic connection. Thus we say that
red roses and yellow roses are more alike than are daises and roses and daises and
roses are more closely related than are petals and flowers. Similarly there is a
combining connection between ladybugs and flowers: Ladybugs protects flowers from
certain pests which in turn serve as the food source for the lady bug.
2
Object Oriented Fundamentals
As another example, roses and candles are largely independent classes but they both
represent things that we might use to decorate a dinner table (Association).
ENCAPSULATION
Encapsulation is the mechanism that binds together code and the data it manipulates
and keeps both safe from outside interface and misuse. One way to think about encapsulation
is as a protective wrapper that prevents the code and data from being arbitrarily accessed by
other code defined outside the wrapper. Access to the code and data inside the wrapper is
tightly controlled through a well-defined interface. To relate this to the real world, consider the
structure of a plant. To understand how photosynthesis works at a high level of abstraction,
we can ignore details such as the responsibilities of plant roots or the chemistry of cell walls.
Similarly, in designing a database application, it is standard practice to write programs, so that
they don’t care about physical representation of the data, but depend only upon a schema that
denotes the data’s logical view. In both of these cases, objects at one level of abstraction are
shielded from implementation details at lower levels of abstractions.
Definition
Encapsulation is the process of compartmentalization the elements of an abstraction
that constitute its structure and behavior. Encapsulation serves to separate the contractual
interface of an abstraction and its implementation.
INHERITANCE
Inheritance is the most important “is a” hierarchy. It is an essential element of object-
oriented systems. Basically, inheritance defines a relationship among classes, wherein one
class shares the structure or behavior defined in one or more classes (denoting single
inheritance and multiple inheritance, respectively). Inheritance thus represents a hierarchy of
abstractions, in which a subclass inherits from one or more superclasses.
Semantically inheritance denotes “is – a “ relationship. For example, a bear “is a” kind
of mammal, a house “is a” kind of tangible asset and a quick sort “is a” particular kind of sorting
algorithm. Inheritance thus implies a generalization/specialization hierarchy, wherein a
subclass specializes the more general structure or behavior of its superclasses.
As we evolve our inheritance hierarchy, the structure and behavior that are common for
different classes will tend to migrate to common superclasses. That is why we often speak of
inheritance as being generalization/specialization hierarchy. Superclass represents
generalized abstractions and subclasses represents specialization’s in which fields and
method from the superclass are added, modified or even hidden. Indeed neglecting the “is a”
hierarchies that exist can lead to bloated, inelegant designs. Without inheritance every class
would be a free-standing unit, each developed from the ground up. Different classes would
bear no relationship with one another, since the developer of each provides methods in
whatever manner he chooses. Any consistency across classes is the result of discipline on the
3
Object Oriented Fundamentals
part of the programmers. Inheritance makes it possible to define new software in the same
way we introduce any concept to a newcomer, by comparing it with something that it already
familiar.
POLYMORPHISM
It is a feature that allows one interface to be used for a general class of actions. The
specific action is determined by the exact nature of the situation. Consider the stack (LIFO).
You might have a program that requires three types of stacks. One stack is used for integer
values, one for floating point values and one for characters. The algorithm that implements
each stack is the same, even though the data being stored differs. In a non-object-oriented
language, you would be required to create three different stacks routines, with each set using
different names. However because of Polymorphism, one can specify the general set of stack
routines that all share the same name.
More generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods”.
OVERVIEW OF JAVA
History of Java
Sun Micro systems began for software to run on TV and VCR sets i.e. Interactive TV
and VCR sets. A team of software engg. led by James Gosling, Patric Naughton, Chris Warth,
Ed Frank and Mike Sheridan laid specifications for these projects in 1991. By September
1992, the basic system was drafted. These interactive sets were called set-top boxes. The
hardware was called 7(Seven) , OS was called Green and the programming language was
called OAK.
Between the initial implementation of OAK in the fall of 1992 and the public
announcement of java in 1995, many more people contributed to the design and the evolution
of the language. Bill Joy, Arther van Hoff, Jonathan Payne, Frank Yellin and Tim Lindholm
were key contributors to the maturing of the older prototype.
Hotjava was the first commercial product to be developed completely in Java.. On May
23, 1995, Java was commercially released. Java Programs can be embedded in HTML pages
and downloaded by Web browser to bring live animations and interaction to web clients.
The power of Java is not limited to web applications. Java is a general purpose
programming language. It has full programming features and can be used to develop
standalone applications. Java is inherently object oriented. Although many OOPL began
strictly as procedural languages, java was designed to be object oriented from the start.
Characteristics of Java
1) It is simple
No programming language is simple, but java is bit simple as compared to other OOP
language such as C++.
2) It is object oriented
Java is called OOP language because Java is centered on creating objects, manipulating
objects and making objects working together.
3) It is distributed
4
Object Oriented Fundamentals
JAVA APPLICATIONS
Java programs can be one of the two types : applications and applets. Applications are
stand-alone programs like any program written using high-level language. Applications can be
executed from any computer with a Java interpreter and are ideal for developing software.
Applets are special kind of Java programs that can run directly from Java compatible Web
browser. Applets are suitable for deploying (install) Web projects.
5
Object Oriented Fundamentals
Documentation Section
The documentation section comprises of a set of comment lines giving the name of the
program, the author and other details which the programmer would like to refer to at a later
stage. Comments must explain why and what of classes and how of algorithms. This would
greatly help in maintaining the program.
/** this the documentation comment
*@author armin
*@version 1.1
*/
6
Object Oriented Fundamentals
Package Statement
The first statement allowed in a Java file is a package statement. this statement
declares a package name and informs the compiler that the classes defined here belong to
this package.
e.g
package student ;
The package statement is optional. That is, our classes do not have to be part of a package.
Import Statement
The next thing after a package statement (but before any class definitions) may be a
number of import statements. This is similar to the #include statement in ‘C’.
import student.test ;
This statement instructs the interpreter to load the test class contained in the package
student. Using import statement, we can have access to the classes that are part of other
named packages.
Interface statements
An interface is like a class but includes a group of method declarations. This is also an
optional section and is used only when we wish to implement the multiple inheritance feature in
the program. Interface is a new concept in Java.
Class Definitions
A Java program mat contain multiple definitions. Classes are the primary and essential
elements of a Java program. These classes are used to map the objects of real world
problems. The number of classes used depends on the complexity of the problem.