Thursday 26 January 2017

Classes in Java


                                             
Classes in Java


The class is at the core of java. Class is the logical construct upon which the entire java language is built because it defines the shape and nature of an object.

When we declare a class , we specify the data that it contains and the code that operates on that data. A class code defines interface to its data.

A class is declared by the use of the class keyword. It's general form is :

            class class_name  {
               type instance_variable1 ;
               type instance_variable2 ;
            .    
            .
            .
             type instance-variable n ;

               type method_name1 ( parameters list )
          {
               //body of method ;
          }
                   type method_name2 ( parameters list )
          {
               //body of method ;
          }
             .
             .
             .
                type method_nameN ( parameters list )
          {
               //body of method ;
          }
     }

 The data or variables defines with in  the class are called instance variables. The code is contained inside a method. Together , the method and instance variables are called as members of class.

  ex-          class BoxDemo
             {
                    double height ;
                    double width ;
                    double length ;
             }
 Here , length , breadth and height are the instance variables of Class BoxDemo.

Objects

When we create a class , we create a new datatype which is used declare objects of that type. Declaring objects of a class is a 2 step process. First , we declare a variable of class type , which is used to refer to that object. Second , we acquire an actual  , physcial copy of the object and assign it to that variable ,which is done by using new operator. 

The new operator dynamically allocates memory for an object and returns a reference to it. This reference is the adress of the object allocated by new.This reference is stored in the variable.

In the above Ex ,  the object of class BoxDemo is created as follow:

                   BoxDemo b = new BoxDemo () ;

This statement combines 2 steps .


The new operator

The new operator dynamically allocates memory for an object.It's general form is :

                      class_name variable_name = new class_name () ;

Here , Variable_name is a variable of the class type being created. The class_name is the name of the class that is being instantiated. The class_name followed by parentheses specifies the constructor for the class.

Method

Method  defines the functionality of the code defined within the class. Its general form is :

        type   method_name ( parameters_list )
 {
        // Body of method
 }

Here , type specifies the type of data returned by the method. This can be any valid types that you create. If the method does not return anything , then its return type must be void.

The method_name specifies the name of the method. The parameters_list is a sequence of type and identifiers pairs seperated by commas. Parameters receives the value of argunment passed to the method , when it is called.

The general form of return type is :

               return value ;
  where value is value returned.

                                                                        By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *