Unit-1: 1. Introduction To Object Oriented Programming
Unit-1: 1. Introduction To Object Oriented Programming
Unit-1: 1. Introduction To Object Oriented Programming
Unit-1
1. Introduction to Object Oriented Programming
The C language is structured, efficient and high level language. Whenever a new programming
language is designed some trade-offs are made, such as
FORTRAN, which is efficient for scientific applications, but is not good for system
code.
BASIC, which is easy to learn, but is not powerful, it is lack of structure.
Assembly Language, which is highly efficient, but is not easy to learn.
These languages are designed to work with GOTO statement. As a result programs
written using these languages tend to produce "spaghetti code", which is impossible to
understand.
During the late 1970s and early 1980s, C became the dominant computer programming
language, and it is still widely used today. Since C is a successful and useful language, you might
ask why a need for something else existed. The answer is complexity. A the program complexity
is increasing it demanded the better way to manage the complexity.
When computers were first programming was done by manually toggling in the binary
machine instructions by use of the front panel. As long as programs were just a few hundred
instructions long, this approach worked. As programs grew, assembly language was invented so
that a programmer could deal with larger, increasingly complex programs by using symbolic
representations of the machine instructions. As programs continued to grow, high-level
languages were introduced that gave the programmer more tools with which to handle
complexity.
The 1960s gave birth to structured programming. This is the method of programming
championed by languages such as C. The use of structured languages enabled programmers to
write, for the first time, moderately complex programs fairly easily. However, even with
structured programming methods, once a project reaches a certain size, its complexity exceeds
what a programmer can manage. To solve this problem, a new way to program was invented,
called object-oriented programming (OOP). OOP is a programming methodology that helps
organize complex programs through the use of inheritance, encapsulation, and polymorphism.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference 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. In Java, the basis of encapsulation is the class. A
class defines the structure and behavior (data and code) that will be shared by a set of objects.
Each object of a given class contains the structure and behavior defined by the class. The objects
are sometimes referred to as instances of a class. Thus, a class is a logical construct; an object
has physical reality.
When you create a class, you will specify the code and data that constitute that class.
Collectively, these elements are called members of the class. Specifically, the data defined by the
class are referred to as member variables or instance variables. The code that operates on that
data is referred to as member methods or just methods. The members can be public or private.
When a member is made public any code outside the class can access them. If the members are
declared, then only the members of that class can access its members.
Inheritance
Inheritance is the process by which one object acquires the properties of another object. This is
important because it supports the concept of hierarchical classification. Without the use of
hierarchies, each object would need to define all of its characteristics explicitly. However, by use
of inheritance, an object need only define those qualities that make it unique within its class. It
can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes
it possible for one object to be a specific instance of a more general case. The Class from which
the properties are acquired is called super class, and the class that acquires the properties is
called subclass. This is mainly used for Method overloading and Code reusability.
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface
to be used for a general class of actions. More generally, the concept of polymorphism is often
expressed by the phrase “one interface, multiple methods.” This means that it is possible to
design a generic interface to a group of related activities. This helps reduce complexity by
allowing the same interface to be used to specify a general class of action. It is the compiler’s
job to select the specific action (that is, method) as it applies to each situation.
4. Applications of OOP
There are mainly 4 type of applications that can be created using java programming:
1) Standalone Application
2) Web Application
An application that runs on the server side and creates dynamic page, is called web application.
Currently, servlet, jsp, struts, etc. technologies are used for creating web applications in java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the advantage
of high level security, load balancing and clustering. In java, EJB is used for creating enterprise
applications.
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used for
creating mobile applications.
5. History of JAVA
1. Brief history of Java
2. Java Version History
Java history is interesting to know. The history of java starts from Green Team. Java team
members (also known as Green Team), initiated a revolutionary task to develop a language for
digital devices such as set-top boxes, televisions etc. For the green team members, it was an
advance concept at that time. But, it was suited for internet programming. Later, Java technology
as incorporated by Netscape. Currently, Java is used in internet programming, mobile devices,
games, e-business solutions etc. There are given the major points that describes the history of
java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
5) Why Oak? Oak is a symbol of strength and choosen as a national tree of many countries like
U.S.A., France, Germany, Romania etc. During 1991 to 1995 many people around the world
contributed to the growth of the Oak, by adding the new features. Bill Joy, Arthur van Hoff,
Jonathan Payne, Frank Yellin, and Tim Lindholm were key contributors to the original
prototype.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
7) Why they choosed java name for java language? The team gathered to choose a new name.
The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA" etc. They wanted
something that reflected the essence of the technology: revolutionary, dynamic, lively, cool,
unique, and easy to spell and fun to say. According to James Gosling "Java was one of the top
choices along with Silk". Since java was so unique, most of the team members preferred java.
8) Java is an island of Indonesia where first coffee was produced (called java coffee).
10) Originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of
Oracle Corporation) and released in 1995.
11) In 1995, Time magazine called Java one of the Ten Best Products of 1995.
There are many java versions that has been released. Current stable release of Java is Java SE 8.
Translating a Java program into byte code makes it much easier to run a program in a
wide variety of environments because only the JVM needs to be implemented for each platform.
Once the run-time package exists for a given system, any Java program can run on it. Remember,
although the details of the JVM will differ from platform to platform, all understand the same
Java byte code. Thus, the execution of byte code by the JVM is the easiest way to create truly
portable programs.
The fact that a Java program is executed by the JVM also helps to make it secure. Because the
JVM is in control, it can contain the program and prevent it from generating side effects outside
of the system.
In general, when a program is compiled to an intermediate form and then interpreted by a virtual
machine, it runs slower than it would run if compiled to executable code. However, with Java,
the differential between the two is not so great. Because byte code has been highly optimized, the
use of byte code enables the JVM to execute programs much faster than you might expect
To give on-the-fly performance, the Sun began to design HotSpot Technology for Compiler,
which is called, Just-In-Time compiler. The JIT, Compiler also produces output immediately
afetr compilation.
7. Features of Java
There is given many features of java. They are also known as java buzzwords. The Java Features
given below are simple and easy to understand.
o Simple
o Secure
o Portable
o Object-oriented
o Robust
o Multithreaded
o Architecture-neutral
o Interpreted
o High performance
o Distributed
o Dynamic
Simple
Java was designed to be easy for the professional programmer to learn and use effectively.
According to Sun, Java language is simple because: syntax is based on C++ (so easier for
programmers to learn it after C++). removed many confusing and/or rarely-used features e.g.,
explicit pointers, operator overloading etc. No need to remove unreferenced objects because
there is Automatic Garbage Collection in java.
Secure
Once the byte code generated, the code can be transmitted to other computer in the world with
knowing the internal details of the source code.
Portable
The byte code can be easily carried from one machine to other machine.
Object Oriented
Everything in Java is an Object. The object model in Java is simple and easy to extend, while
primitive types, such as integers, are kept as high-performance non-objects.
Robust
Multithreaded
Java was designed to meet the real-world requirement of creating interactive, networked
programs. To accomplish this, Java supports multithreaded programming, which allows you to
write programs that do many things simultaneously.
Architecture-neutral
The Java designers made several hard decisions in the Java language and the Java Virtual
Machine in an attempt to alter this situation. Their goal was “write once; run anywhere, any time,
forever.” To a great extent, this goal was accomplished.
Distributed
Java is designed for the distributed environment of the Internet because it handles TCP/IP
protocols. In fact, accessing a resource using a URL is not much different from accessing a file.
Java also supports Remote Method Invocation (RMI). This feature enables a program to invoke
methods across a network.
Dynamic
Java programs carry with them substantial amounts of run-time type information that is used to
verify and resolve accesses to objects at run time. This makes it possible to dynamically link
code in a safe and expedient manner.
8. Program Structures
Simple Java Program
Example.java
class Example
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
We can use any text editor such as "notepad' or "dos text editor". The source code is typed and is
saved with ".java" as extension. The source code contains one or more class definitions. The
program name will be same as class name in which main function is written. This in not
compulsory, but by convention this is used. The source file is officially called as compilation
unit. We can even uses our choice of interest name for the program. If we use a different name
than the class name, then compilation is done with program name, and running is done with class
file name. To avoid this confusion and organize the programs well, it is suggested to put the
same name for the program and class name, but not compulsory.
To compile the program, first execute the compiler, "javac", specifying the name of the source
file on the command line, as shown bellow:
c:\>javac Example.java
The javac compiler creates the file called "Example.class", that contains the byte code version of
the source code. This byte code is the intermediate representation of the source code that
contains the instructions that the Java Virtual Machine (JVM) will execute. Thus the output of
the javac is not the directly executable code.
To actually run the program, we must use Java interpreter, called "java". This is
interpreter the "Example.class" file given as input.
When the program is run with java interpreter, the following output is produced:
Hello World
The first line contains the keyword class and class name, which actually the basic unit for
encapsulation, in which data and methods are declared.
Second line contains "{" which indicates the beginning of the class.
where public is access specifier, when a member of a class is made public it can be accessed by
the outside code also. The main function is the beginning of from where execution starts. Java is
case-sensitive. "Main" is different from the "main". In main there is one parameter, String args,
which is used to read the command line arguments.
Fourth line contains the "{", which is the beginning of the main function.
System.out.println("Hello World");
Here "System" is the predefined class, that provides access to the system, and out is the output
stream that is used to connect to the console. The println(), is used to display string passed to it.
This can even display other information to.
If you do not already have the JDK software installed or if JAVA_HOME is not set, the Java CAPS installation will not
be successful. The following tasks provide the information you need to install JDK software and set JAVA_HOME on
UNIX or Windows systems.
The following list provides the Java CAPS JDK requirements by platform.
Solaris
IBM AIX
Macintosh
Microsoft Windows
2. To set JAVA_HOME:
a. Right click My Computer and select Properties.
b. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK
software is located, for example, C:\Program Files\Java\jdk1.6.0_02.
jdk-8uversion-linux-i586.tar.gz
1. Download the file.
Before the file can be downloaded, you must accept the license agreement. The archive binary can be installed
by anyone (not only root users), in any location that you can write to. However, only the root user can install the
JDK into the system location.
2. Change directory to the location where you would like the JDK to be installed, then move the .tar.gz archive
binary to the current directory.