Showing posts with label OBJECT. Show all posts
Showing posts with label OBJECT. Show all posts

Monday, 27 July 2015

Object and Class in java

The Concept of Object and Class in Java

Object in Java


Basically an entity that has state and behavior is known as an object. It has three main characteristics like state,behavior and identity.

Object is an instance of a class.

Object and Class

Class in java



A class is a group of objects that has common properties.

A class can be defined as a template/ blue print that describes the behaviors/states that object of its type support.

Syntax to declare a class

class {  

    data member;  

    method;  

}  



Sample Program:


class FirstProgram{

   int a;     //data member
   String name;//data member
   public static void main(String args[]){  
      
      FirstProgram fp=new FirstProgram();  //creating an object of FirstProgram
      System.out.println(fp.a);  
      System.out.println(fp.name);
   }
}  


Output:


0
null


Note: The Output of the program shows the default values in the variables 'a' and 'name'.

Monday, 20 January 2014

CONSTRUCTORS IN JAVA


A constructor initializes an object immediateily upon creation.It has the same name as the class in which it resides and is syntactically similar to a method.Once defined, the constructor is automatically called immediately after the object is created,before the new operator completes.Constructors look a little strange because they have no return type ,not even void.Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially.

Using object in java


As you know, a class provides the blueprint for objects; you create an object from a class.A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.