Thursday, 24 February 2011

what is object? and what is class? in java

OOPS in java

Opps is required

to design and develop,

to support and represent

a real world object in programming languages

Definition of Opps

Opps is an approach that provides a way of modularizing of program,

by creating memory area for both data and methods,

that can be used as template for creating copies of such modules (objects) on demand.

Opps is a technique to create programs based on real world objects, unlike procedural programming here in the opp programming model, programs are organized around objects and data rather than actions and logic.

Building Blocks Of Opps: Classes, Objects

Definition of Class

A class is a software construct that defines the instance variables and methods of an object.

A class itself is not an object.

A class is a template that defines how an object will look and behave when the object is created or instantiated from the specification declared by the class.

You obtain concrete objects by instantiating a previously defined class.

You can instantiate many objects from one class definition.

just as you can construct many houses that area all the same from a single architect’s drawing.

A class is a blue print of an object.

A class is a speciation that defines what sort of objects should be created.

A class is a logical construct of an object.

Hence a class defines the structure, state and behavior.

Definition of an object:

In the programming implementation of an object, it’s (An object’s) state is defined by its instance variables. Instance variables are private to the object. Unless explicitly made public or made available to other “friendly” classes, an object’s instance variables are inaccessible from outside the object.

An object’s behavior is defined by its methods. Methods manipulate the instance variables to create new state; an object’s methods can also create new objects.

The small picture to the left is a commonly used graphical representation of an object. The diagram illustrates the conceptual structure of a software object—it’s kind of like a cell, with an outer membrane that’s its interface to the world, and an inner nucleus that’s protected by the outer membrane. An object’s instance variables (data) are packaged, or encapsulated, within the object. The instance variables are surrounded by the object’s methods. With certain well-defined exceptions, the object’s methods are the only means by which other objects can access or alter its instance variables. In Java, classes can declare their instance variables to be public, in which cases the instance variables are globally accessible to other objects. Declarations of accessibility are covered in later in Access Specifiers.

Object is a physical construct of a class.

It can also define as Instance of a class.

Technically object can be defined as it is an encapsulated form of all instance variables & instance methods of a particular class.

Monday, 21 February 2011

What is encapsulation


Encapsulation:

Encapsulation is the process of protecting /hiding data, by declaring variables as private and providing public setter and getter method.

(or)

Wrapping or binding variables of a class with methods which operated on those variables is called as Encapsulation.

1) If it is needed how can we give access to outside member?

By using public methods. Declare variables as private and access them into public methods with in the class, call this public methods form outside the class.

2) What is the difference between in accessing variables directly and via public setter and getter methods?

If you allow user to access variables via method, we can validate by authentication logic if required.

This the main reason behind encapsulation, suggesting us to protect variables with private access specifier and provide the accessibility via methods.