Core Java - [First Class/Object Example]

♠ Posted by Unknown in at 23:27

Creation of Class and Object


A class is a user-defined data type with a templates that serves to define its properties. Once the class type has been defined, we can create “variables” of that type using declarations that are similar to the basic type declarations. In java, these variables are termed as instances of classes, which are the actual objects. The basic form of a class definition is :

                        class classname [extends superclassname]
                        {
                                    [ variable declaration;]
                                    [methods declaration;]
                        }

classname and superclassname are any valid Java identifiers. The keyword extends indicates that the progperites of the superclassname class are extended to the classname class. This concept is known as inheritance.

An object in Java is essentially a block of memory that contains space to store all the instance variables. Creating an object is also referred to as instantiating an object.

Object in Java are created using the new operator. Here is an example of creating an object of type Rectangle.

                        Rectangle rect;                       //declare
                        rect = new Rectangle();          //instantiate

The first statement declares a variable to hold the object and the second one actually assigns the object reference to the variable. The variable rect is now an object of the Rectangle class.

Both statements can be combined into one as shown below.

                        Rectangle rect = new Rectangle();

The method Rectangle() is the default constructor of the class. We can create any number of object of Rectangle.

Example :

//Save This file with the name of class which have main() method.
import java.lang.*;
import java.io.*;
class Rectangle
{
            int length, width;
            void getdata(int x, int y)
            {
                        length = x;
                        width = y;
           
            }
            int RectArea()
            {
                        int area = length * width;
                        return area;
            }
}

class ExampleOne
{
            public static void main(String [] args)
            {
                        Rectangle Rct = new Rectangle();
                        int x, y;
                        x = Integer.parseInt(args[0]);
                        y = Integer.parseInt(args[1]);
                        Rct.getdata(x, y);
                        int ar = Rct.RectArea();
                        System.out.println("The are of Rectangle is :" + ar);
            }
}

It is important to understand that each object has its own copy of the instance variable of its class. This means that any changes to the variables of one object have no effect on the variables of another. It is also possible to create two or more references to the same object.

0 comments:

Post a Comment