Tutorials at Free Tutorials Corner

We provide all latest Tutorials in easy way to learn for beginners.Happy Learing .

Placement Papers at Free Tutorials Corner

One place to find all latest placement papers,interview question,interview tips and many more.

Jobs at Free Tutorials Corner

All the latest job - IT, BPO, Government jobs, daily updates .

Tips at Free Tutorials Corner

Technical Tips, Interview Tips, Money Making Tips, etc...

Video Tutorials at Free Tutorials Corner

Video tutorials for all the latest technologies.We are mainly focused to provide Tutorials in Tamil.

Showing posts with label java tutorials. Show all posts
Showing posts with label java tutorials. Show all posts

Wednesday, 1 May 2013

Java Introduction-2


BENEFITS OF OOPS

• Modularity (separation of duties)

• Extensibility (responsive to future requirements)

• Modifiability (easy to make small changes)

• Flexibility (not cast in concrete)

• Maintainability (big savings)

• Reusability (don’t reinvent the wheel)

• Data abstraction (hidden data representation)

• Program encapsulation (operations bound to data)

• Software libraries (fixed reusable)

• Polymorphism (type-related generic operations)

Data abstraction

A powerful way to manage abstraction is through the use of hierarchical classifications.

This allows you to layer the semantics of complex systems, breaking them into more

manageable pieces. From the outside, the car is a single object. Once inside, you see

that the car consists of several subsystems: steering, brakes, sound system, seat belts,

and so on. In turn, each of these subsystems is made up of more specialized units.

• groups the pieces of data that describe some entity, so that programmers can manipulate that data as a unit advantages

• your programs become easier to read

• your programs become easier to reuse

• you can easily augment what a class provides

• you can easily improve the way that data are stored

The Three OOP Principles

  1. Encapsulation
  2. Inheritance
  3. 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, as if it were stamped out by a mold in the shape of the class. For this reason, 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.

• Binds code & the data together

• The protective wrapper that prevents from the code & data from being arbitrarily accessed from outside.

• Mechanisms for hiding complexity is to be mark the variable or class as PROTECTED OR PUBLIC

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

mentioned earlier, most knowledge is made manageable by hierarchical (that is, top-down)

classifications.

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.

Derived class / Sub class

It is the class that has been created from an existing class.

Base class / Super class

It is the class from where the sub classes are derived

Polymorphism

Polymorphism (from the 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.

• Mechanism that allows one name to be used for 2 or more related but technically different purposes.

• “ONE INTERFACE MULTIPLE METHODS”

• Appropriate method for the situation, is selected at Compile-time / Run-time.

Types of Polymorphism

• Compile-time Polymorphism = Early Binding = Static Binding

• Run-time Polymorphism = Late Binding = Dynamic Binding

STATIC POLYMORPHISM:

The overloaded function to be executed is selected at Compile time.

Examples:

Function Overloading

Operator Overloading

DYNAMIC POLYMORPHISM:

The function to be executed is selected at Run time

Example:

Virtual Functions

Garbage Collection and Finalize

Objects are garbage collected

• No explicit free

• Avoids dangling pointers and resulting type errors

Problem

• What if object has opened file or holds lock?

Solution

• finalize method, called by the garbage collector

– Before space is reclaimed, or when virtual machine exits

– Space overflow is not really the right condition to trigger

finalization when an object holds a lock...)

• Important convention: call super.

Note : System.gc( ) used call garbage collection manually

ENVIRONMENT & TOOLS

• Java Development Kit (JDK) comprises of the development tools & Java Standard Library (JSL)

• It includes a huge collection of class libraries called APIs.

Some of the important tools from JDK

  1. javac -- compiles Java Programs.
  2. java -- executes Java Programs.
  3. appletviewer -- execute Java Applets.
  4. javadoc -- creates HTML documentation for the Java Programs.

VIRTUAL MACHINE

• The NAME OF A CLASS (and not the name of a file) should be the parameter for Java Virtual Machine (JVM)

• The Java Compiler generates Byte code in .class file, which is interpreted by JVM, thus makes Java as Platform-independent.

• The corresponding .class file must be found in the list of directories in the CLASSPATH environment variable.

• The .class is loaded to the JVM and the execution starts by calling the method: public static void main(String args[])

COMMONLY USED API’s

Language support package

Util support package

IO package

Networking package

Applet package

AWT package

Swing package

SQL package

Java Introduction-1

The Creation of Java

Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and
Mike Sheridan at Sun Microsystems, Inc. in 1991. It took 18 months to develop the first
working version. This language was initially called “Oak” but was renamed “Java”
in 1995. Between the initial implementation of Oak in the fall of 1992 and the public
announcement of Java in the spring of 1995, many more people contributed to the design
and evolution of the language. Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yellin,
and Tim Lindholm were key contributors to the maturing of the original prototype.

• Java is an object-oriented language similar to C++.
• Simplified to eliminate C++ language features that cause common programming errors.
• Java source code files (files with a .java extension) are compiled into a format called bytecode (files with a .class extension).
• Class files can then be executed by a Java interpreter.
• Compiled Java code can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (VMs), exist for most operating systems.
• Bytecode can also be converted directly into machine language instructions by a just-in-time compiler (JIT).

FEATURES OF JAVA
Simple & Powerful
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted & High Performance
Distributed
Dynamic

Simplicity
• Almost everything is an object
• All objects on heap, accessed through pointers
• No functions, no multiple inheritance, no go to, no
operator overloading, few automatic coercions
Portability and network transfer

• Bytecode interpreter on many platforms
Reliability and Safety

• Typed source and typed bytecode language
• Run-time type and bounds checks
 
Two Paradigms
  1. Procedure-oriented Programming
  2. Object-oriented Programming
As you know, all computer programs consist of two elements: code and data. Furthermore,
a program can be conceptually organized around its code or around its data. That is,
some programs are written around “what is happening” and others are written around
“who is being affected.” These are the two paradigms that govern how a program is
constructed.

The first way is called the process-oriented model. This approach characterizes
a program as a series of linear steps (that is, code). The process-oriented model can be
thought of as code acting on data. Procedural languages such as C employ this model to
considerable success.

The second approach, called object-oriented programming, was conceived.
Object-oriented programming organizes a program around its data (that is, objects) and a
set of well-defined interfaces to that data. An object-oriented program can be characterized
as data controlling access to code.

Procedure-oriented Programming
• Organizes a program around its procedure
• Emphasis of a program is on how to accomplish a task
• User has little, if any, control
• Follows bottom-up approach

Object-oriented Programming
• A program around its data & a set of well-defined interfaces to that data.
• Emphasis on data rather than procedure
• Programs are divided into Objects (relevant real-world objects )
• Data is hidden and cannot be accessed by external functions
• Objects can communicate with each other through functions
• New data and functions can be easily added whenever necessary
• User has a lot of control
• Follows bottom-up approach