0% found this document useful (0 votes)
2 views

Introduction to Java

Object Oriented Programming (OOP) is centered around objects that represent instances of classes, emphasizing the relationships between classes through inheritance. Key concepts include encapsulation, inheritance, and polymorphism, which help structure and manage complex software systems. Java, a prominent OOP language, is designed to be simple, secure, and platform-independent, making it suitable for both applications and applets.

Uploaded by

Armin Shroff
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Java

Object Oriented Programming (OOP) is centered around objects that represent instances of classes, emphasizing the relationships between classes through inheritance. Key concepts include encapsulation, inheritance, and polymorphism, which help structure and manage complex software systems. Java, a prominent OOP language, is designed to be simple, secure, and platform-independent, making it suitable for both applications and applets.

Uploaded by

Armin Shroff
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Fundamentals

Object Oriented Fundamentals


What is Object Oriented Programming
OOP is a method of implementation in which programs are organized as cooperative
collections of objects, each of which represents an instance of some class and whose classes
are all members of hierarchy of classes united via inheritance relationships.
There are three important parts in this definition :
1) Uses Objects, not algorithms, as its fundamental logic building blocks.
2) Each object is an instance of some class.
3) Classes are related to one another via inheritance relationship.

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

From the perspective of human cognition, an object is any of the following :


1) A touchable and/or visible thing
2) Something that may be apprehended(held) intellectually (mentally)
3) Something towards which thought and action is desired.

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.

A single object is simply an instance of a class. Objects that share no common


structure and behavior cannot be grouped in a class because, by definition, they are unrelated
except by their general nature as objects.
It is important to note that the class – as defined by most programming languages – is a
necessary but insufficient vehicle for decomposition. Sometimes abstractions are so complex
that they cannot be conveniently expressed in terms of a single class declaration. eg. An entire
Inventory control system is conceptually individual object, but cannot be expressed as a single
class. Instead it is better for us to capture the abstraction as a cluster of classes, whose
instance collaborate to provide the desired structure and behavior.
Examples of Class and Object
As all human beings have common features, we can have a class of human being, lets say it is
X. An instance of X class can be existing person whose has the features of the class X. Lets
call it Y. Therefore X is the class and Y is the object.

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.

In all there are three basic kinds of relationship:


1) Generalization/specialization, denoting an “is a” relationship. E.g. A rose is a kind of
flower meaning that a rose is a specialized subclass of the more general class
(Inheritance) flower.
2) Whole/part, which denotes the part of the relationship. E.g. a petal is not a flower, but it
is a part of the flower (Aggregation).

2
Object Oriented Fundamentals

3) Association – which denotes some semantic dependency among various classes,


which are, otherwise unrelated classes such as ladybugs and flowers.

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

THE THREE OOPS PRINCIPLES


All Object oriented programming languages provide mechanisms that help you
implement the object oriented model. They are encapsulation, inheritance and polymorphism.

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

Distributed computing involves several computers on a network working together. Java is


designed to make distributed computing easy. Networking capability is inherently
integrated into Java.
4) Java is interpreted
You need an interpreter to run Java programs. The programs are compiled into Java
Virtual Machine Code called bytecode. The bytecode is machine independent and can run
on any machine that has java interpreter.
5) It is Robust
Robust means reliable. No programming language can assure reliability. Java puts a
lot of emphasis on early checking of possible errors as java compilers can detect many
problems that would throw show up at execution time in other languages. It has a runtime
exception handling feature to provide programming support for robustness. Java can catch
and respond to an exceptional situation so that the program can continue its normal
execution and terminate gracefully when a runtime error occurs.
6) It is secure
Java being an internet programming language, is used in a networked and distributed
environment. You can download a java applet, run it on your computer and it will cause no
damage to your system.
7) It is Architectural Neutral
The most remarkable feature of java is that it is architectural neutral (Platform
Independent). You can write one program that runs on any platform with JVM (Java Virtual
Machine). You can run Java Applets from a Web Browser, but java is more than just a web
applet. You can also run stand-alone java applications directly from operating system by
using a java interpreter.
8) It is portable
Java programs can run on any platform without having to be recompiled. This is one
positive aspect of portability.
9) It is high performance
10)It is multithreaded
Multithreading is capability for a program to perform several tasks simultaneously within a
program. E.g downloading a video file while playing a video file would be considered
multithreading. It is smoothly integrated in Java.
11)It is Dynamic

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.

JAVA APPLICATION TOOLS


JDK consists of set of separate programs, each of which is invoked from a command
line. Besides JDK, there are more than half a dozen Java development packages in the
market today
Café by Semantec ( http://www.symantec.com )

5
Object Oriented Fundamentals

Visual J++ by Microsoft (http://www.microsoft.com)


JBuilder by Borland (http://www.borland.com)
JFactory by Rouge Wave (http://www.rougewave.com)
Visual Age for Java by IBM (http://www.ibm.com)
These tools provide an integrated development Environment (IDE) for rapidly
developing Java programs. The use of development tools makes it easy and productive to
develop dynamic Java programs.
It is always better to use an IDE tool for developing Java programs because such tools
are very convenient. Editing, compiling, debugging and online help are in one graphical user
interface. Just enter a source code in one window or open to modify an existing file in a
window, then click a button, menu item or function key to compile the source code. The
compilation errors are shown in a second window. When you click the error message, the IDE
tool directly points to the error in the source code. After fixing the errors, recompile the code.
Click another button, menu or function key to run (execute) the program. The program again
runs in a window and one sees the output.

Java Program Structure


A Java program may contain many classes of which only one class defines a main
method. Classes contain data members and methods that operate on the data members of
the class. Methods may contain data type declarations and executable statements. To write a
java program, we first define classes and then put them together. A java program may contain
one or more sections as shown in the following figure.

Documentation Section Suggested


Package Statement Optional
Import Statement Optional
Interface Statements Optional
Class Definitions Optional
Main Method Class Essential
{
Main method definition
}

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.

Main Method class


Since every Java stand-alone program requires a main method as it starting point, this class is
the essential part of a Java program. A simple Java program may contain only this part. The
main method creates objects of various classes and establishes communications between
them. On reaching the end of main, the program terminates and the control passes back to
the operating system.

You might also like