Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Sunday, 15 May 2016

Creating an Object

Creating an Object ?
A Class Provides the blueprints for objects. So basically an object is created from a class. In java, the new keyword is used to created new objects.
There are three steps to create an object from class:

1)  Declaration:  
A variable declaration with a variable name with an object type.
2)  Instantiation:
The “New” keyword is used to create the object.
3)  Initialization:
The “New “ keyword is followed by a call to a constructor. This call initializes the new object.

Let’s see an example for better understanding about it.

public class Puppy{
public Puppy(String name){//Constructor

System.out.println("passed the Name is:"+name );
}
public static void main(String args[]){
Puppy myPuppy =new Puppy("tommy");

}
}





Friday, 13 May 2016

What is an Object ?

   Let us now look deep into what are objects. If we consider the real world we can find many objects around us, cars, Dogs, Human, etc. All these objects have states and behavior. If we consider Dog, then its state is – name, breed, color and behavior is-barking, wagging, running etc.
Ø  If you compare the software object with real world object, they have very similar characteristics.
Ø  Software objects also have a state and behavior. A software object’s state is stored in fields and behavior is shown via methods.
Ø  So in software development, methods operate on the internal state of an object & object to object communication is done via methods.