Class

Class is the so fundamental of java which defines the nature and shape of the object. Thus it forms the object oriented programming in java.

A class is a data type which we can create objects of that type. Thus class is a template of an object and Object is the instance of a class.

General form of Class

A class contains data and code that operates on data these defines the class nature and shape.

class can be created by using class keyword.

class ClassName

{

type instance-variable1;

type instance-variable2;

// ... type instance-variableN;

type methodname1(parameter-list)

{ // body of method }

type methodname2(parameter-list)

{ // body of method }

// ...

type methodnameN(parameter-list)

{ // body of method }

}

The variables declared within the class are called instance variables because each instance of class have its own copy of variables.

The code inside the class are called methods which perform operations on variables.

Both combined cal it as a members of class.

The general form of class does not contain a main() method as there is no need to specify main() in every classes.

Which class should be executed first that must have main() method.

Simple class

class Square

{

double length;

double breadth;

}

It is a class of type Square. A class does not have any objects when we defined we have to create objects to this.

  • Square square1 = new Square(); //creating object of type Square

When we creating another object it have its own variables which we can access by using dot(.) operator.

  • square1.length =10.0; // using dot operator.
//simple class example
class Rectangle
{
    double length;
    double breadth;
}
public class Area{

     public static void main(String []args)
     {
         double area;
     Rectangle rectangle1 = new Rectangle();
     Rectangle rectangle2 = new Rectangle();

     rectangle1.length=10.0;
     rectangle1.breadth=3.5;

     area=rectangle1.length*rectangle1.breadth;
     System.out.println("Rectangle1 area is::"+area);

     rectangle2.length=11.0;
     rectangle2.breadth=4.0;

     area=rectangle2.length*rectangle2.breadth;
     System.out.println("Rectangle2 area is::"+area);
     }
}

Output:

Rectangle1 area is::35.0
Rectangle2 area is::44.0

Declaring a Object

It is two step process

Rectangle rectangle1; //declaring a reference to an object of type Rectangle.

rectangle1 = new Rectangle(); //dynamically allocates a memory to the object rectangle1 using new keyword.

This returns a reference to that object which stored in the variable.

Assigning Object Reference variables

Rectangle rectangle1 = new Rectangle();

Rectangle rectangle2 = rectangle1; //assigning rectangle2 to rectangle1

Means rectangle2 refers to the object rectangle1;

//example for assigning one object to another
class Rectangle
{
    double length;
    double breadth;
}
public class Area{

     public static void main(String []args)
     {
         double area;
     Rectangle rectangle1 = new Rectangle();
     Rectangle rectangle2 = rectangle1;
     rectangle1.length=10.0;
     rectangle1.breadth=3.5;
     area=rectangle1.length*rectangle1.breadth;
     System.out.println("Rectangle1 area is::"+area);
     area=rectangle2.length*rectangle2.breadth;
     System.out.println("Rectangle2 area is::"+area);
     }
}

Output:

Rectangle1 area is::35.0
Rectangle2 area is::35.0

we can simply unhook rectangle1 from the original object without affecting the object or affecting rectangle2 as

rectangle1=null;

results matching ""

    No results matching ""