Saturday 19 March 2011

Java Classes, objects and methods

INTRODUCTION:
Java gives you the capability to do object-oriented programming. Object-oriented programming enables the programmer to group together data and the code that uses data into discrete units. Objects in java are defined using java classes. Anything we wish to represent in a java program must be encapsulated in a class that defines the state and behaviour of the basic program components known as object. Classes creates objects and objects use methods to communicate between them.
In java, the data items are called fields and the functions are called methods. calling a specific method in an object is described as sending a message to the object. A class provides a sort of template for an object and behaves like a basic data type such as int.

DEFINING A CLASS:
A class is a user defined data type used to implement an abstract object, giving you the capability to use object-oriented programming with java.
Once the class-type has been defined, we can create 'variables' of that type. These variables are termed as instances of class, which are actual objects.

The general form of a class definition is:
             Class name [extends superclassname]
             {
                   [variable declarations;]
                   [method declarations;]
             }
Everything written inside the square brackets is optional. Thus, the following would be a valid class definition.
             Class empty
             {
             }
Classname and Superclass name are any valid java identifiers. The keywords extends indicates that the properties of the superclassname class are extended to thr class classname. This concept is known as inheritance.

ADDING VARIABLES:
Data is encapsulated in a class by placing data fields inside the body of the class definition. These variables are called instance variables because they are created whenever an object of the class is instantiated. Instance variables can be declared exactly the same way as we declare local variables.

Example:
     Class Movie
     {
          String title;
          int length;
          int cost;
          char type;
     }

The class movie contains four instance variables, out of which one is of type string, two are of type int and one is of type char. The two int variables can be declared in one line as
      int length, cost;

ADDING METHODS:
A class has no life without methods. The objects created by such class cannot respond to any message. Therefore it is necessary to add methods for manipulating the data contained in the class. Methods are declared inside the class immediately after the variable declaration.
The general form of method declaration is:
     type method-name (parameter-list)
     {
          method-body;
     }

Where, -methodname denotes the name of the method.
            -type denotes the type of value the method returns
            -parameter-list denotes the list of parameters to be passed into the method.

The parameter-list must always be enclosed in parantheses. The variables in the list are separated by commas. In the case where no input data is required , the declaration must retain the empty parantheses.

Let us consider Movie class again and add a method getData() to it.
    Class Movie
    {
       String title;
       int length;
       int cost;
       char type;
       void getData (String s,int x,int y, char z)
        {
           title = s;
           length = x;
           cost = y;
           type = z;
        }
       }
Nothe that the method has a return type void because it does not return any value. The getData method is basically added to provoide values to the instance variables.

CREATING OBJECTS:
In java, objects are essentially a block of memory that contains space to store all the instance variables. The other name of creating an object is instantiating an object.

To create an object a new keyword is used as an operator. This operator creates an object of the specified class and returns a reference to thst objects.

The syntax of creating an object is as follows:
Class-name object-name;         //declare
Object-name = new class-name () ;         //instantiate

Example:
      Movie mov1;
      mov1 = new Movie();

The first statement declares a variable to hold the object reference and the second actually assigns the object reference to the variable. Both statements can be combined into one as shown below:
      Movie mov1 = new Movie();
Where Movie is the class name , mov1 is the object created by the default empty constructor of the class. We can create any number of objects of a class. Each object has it's own copy of instance variables of its class. Thus, any changes to the variables of one object have no effect on the variables of the another. Two or more references can be created to the same object just by assigning one object reference variable to another.

Example:
     Movie mov2 = new Movie();
     Movie mov3 = mov2;
Here, Both mov3 and mov2 refer to the same object.

Java's two reference of same object


No comments:

Post a Comment