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

0 comments:

Post a Comment