Object oriented programming concept uses the fact that a program consists of a group of objects that may have some relationships among them. Java is a true object-oriented language and therefore the underlying structure of all Java Programs is classes.
A class is a user defined datatype. Anything need to be represented in a java program must be encapsulated in a class that defines the state (Member data) and behavior (Member Methods). Once defined datatype class , it is used to create basic program components known as object of that type.
Classes create objects and objects use methods to communication between them. That is all about object-oriented programming.
Classes provide a convenient method for packing together a group of logically related data items and function that work on them. In Java items are called fields and the function are called methods. Calling a specific methods in an object is described as sending the object a message.
Defining A Class
A class is a user-defined data type with a template that serves to define its properties and actions. Consider following syntax
class classname[extends superclassname] { [variable declaration;] [method declaration] } |
According to the above syntax, A class is declared by use of the class keyword. classname is the name of class which is valid java identifier.Everything inside the square brackets is optional. It is used to create an inheritance. where extends is the keyword and superclassname is the name of superclass. A class may contain member variable, instance variable and any number of methods which are used to manipulate the data.
Ex,
class Data
{
int no1,no2;
void getData()
{
no1=10;
no2=20;
}
}
Creating Object
Objects are at the heart of object-oriented technology. Once the class type has been defined we can create “Variable” of that type using declaration that are similar to the basic type declaration. In Java these variables are termed as instance of classes. An object is an entity that can store data and send and receive message.
An object in java is essentially of memory that contains space to store all the instance variable. Creating an object is also referred to as instantiating an object.
Declaring objects
Declaring object is two step process. First declare a variable of the class type. This variable does not defined an object. Instead it is simply a variable that can refer to an object.
Second acquire an actual physical copy of the object and assign it to variable .The new operator creates an object of the specified class and returns a reference to that object. Here is an example of creating an object of type Data.
Data obj1; //declare
obj1=new Data( ); //instantiate
The first statement declare a variable to hold the object reference and the second one actually assign the object reference to the variable.The variable obj1 is now object of the Data class.
Creating object reference
Ex,
Data obj1=new Data ();
Data obj2=new Data ();
Here, Each object has its own copy of the instance variable of its class. It means that any changes to the variables of the one object have no effect on the variables of the another.It is also possible to create two or more references to the same object.
For Ex,
Data obj1=new Data ();
Data obj2=obj1;
Here, obj1 and obj2 will both refer to the same object. The assignment of obj1 and obj2 did not allocate any memory or copy any part of original object. Thus any changes made to the object through obj2 will affect the object to obj1 which is referring, since they are the same object.
Accessing class member
Each created object has its own set of variables. There is a need to assign values to those variable in order to use in program. Remember that all variable s must assign the value before they are used. The instance variable and methods can not directly accessed for that must need to use concern object and the dot operator as per following format.
Objectname.variable name
Objectname.methodname(parameter list);
Ex,
class Data
{
int no1,no2;
void getData()
{
no1=10;
no2=20;
}
void putData()
{
System.out.println(“Number 1= “+no1);
System.out.println(“Number 2= “+no2);
}
}
class MainData
{
public static void main(String args[])
{
Data obj1=new Data();
obj1.getData();
obj1.putData();
}
}
As shown in above example the class Data has two member variable and two member method. The object of class Data is created in the main() . Using the object the two methods of class Data is called.
Posted by : Ruchita Pandya
No comments:
Post a Comment