Friday, 31 May 2013

Class and Object in JAVA

A class is blueprint or a template for creating different objects which defines its properties and behaviors. 

A class can contain fields and methods to describe the behavior of an object.

Java objects exhibit the properties and behaviors defined by its class.

Class and Object are related to each other because every Object must be type of any class. In the same time class itself is of no use until you create object. Let’s see these difference between class and object.

Difference between Class vs Object in OOPS and Java:

Class and Object are two most important concept of Object oriented programming language (OOPS)  e.g. Java. Main difference between a Class and an Object in Java is that class is a blueprint to create different objects of same type. 

1) Class is blueprint means you can create different object based on one class which varies in there property.

Example: IF Car is class then Audi, BMW, Honda.. can be considered as object because they are essentially a car but have different size, shape, color and feature.

2)class in Java contains both state and behavior, state is represented by field(variable) in class e.g. numberOfGears. On the other hand behavior is controlled by functions, also known as methods in Java e.g. start() will change state of car.

3) Object is also called instance in Java and every instance has different values of instance variables.

Example:
Person p1 = new Person("JavaSE");
Person p2 = new Person("JavaEE");

4)Class specifies how an object will look like and object belongs to a particular type.

Classes in Java: A class is a blue print from which individual objects are created.

public class ClassExample {

int x;
static float salary;

public void method1(){

}
}

Creating an Object: In java the new key word is used to create new objects.

There are three steps when creating an object from a class:

Declaration: A variable declaration with a variable name with an object type.

Instantiation: The 'new' key word is used to create the object.

Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Example:
***********************************************************
public class ClassandObjectDemo {

int i;// instance variable
static String name; // class or static variable

public ClassandObjectDemo() {
name = "abcd";
i = 600;
System.out.println("This is object Initialization of Object:");
}

/**
 * This is static method. We can acess this using classname.
 */
public static void getName() {
// Here name is static variable. So we can use it in static method.
// We can not acess i value here because it instance variable.
    // So non static variables never be used in static method.
    // So please comment below line.
// System.out.println("salary is " + i);
System.out.println("name of course is JAVA and author is " +  
                                                                    name);
}

/**
 * This is instance method. we can access this method using object of 
  * an class
 * 
 * @param gs
 * @param ns
 * @return
 */
public float getsalary(float gs, float ns) {
float ts;// local variable
ts = gs + ns;
// We can access static variable in instance method. 
        // But we can not access instance variable in static method as said 
        // in above method.
System.out.println("name of employee is " + name);
return ts;
}

/**
 * @param args
 */
public static void main(String[] args) {
//Left side of = specifies the Declaration of Object:
//right side of = specifies the Instantiation of Object:
ClassandObjectDemo objectDemo = new ClassandObjectDemo();
float salary = objectDemo.getsalary(5F, 10F);
ClassandObjectDemo.getName();
System.out.println("salary is " + salary);
}
}
***********************************************************

No comments:

Post a Comment