Real-world objects share two characteristics: they all have state and they all have behavior. For example, dogs have state (name, color, breed, hungry) and dogs have behavior (barking, fetching, and slobbering). Software objects are modeled after real-world objects in that they, too have state and behavior. A software object maintains its state in variables and implements its behavior with methods.
An object is a software bundle of variables and related methods.
The following illustration is a common visual representation of a software object:
Packaging an object’s variables within the protective custody of its methods is called encapsulation. Typically, encapsulation is used to hide unimportant implementation details from other objects. When you want to change gears on your bicycle, you don’t need to know how the gear mechanism works, you just need to know which lever to move. Similarly in software programs, you don’t need to know how a class is implemented, you just need to know which methods to invoke. Thus, the implementation details can change at any time without affecting other parts of the program. An object has three characteristics:
- State: Attributes (properties) defines state of object.
- Behaviour: Represents the behaviour (functionality) of an object such as deposit, withdraw etc.
- Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
Class is a template or blueprint from which objects are created. A class defines new data type having attributes and associated behaviour (method(s)). A method is a block of code or procedure that can be called to perform some action, and it may return a value. Object is an instance of a class. The new
keyword is used to allocate memory at run time. All objects get memory in heap memory area. The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.
// Java object example class Student{ int id;//field or data member or instance variable String name; public static void main(String args[]){ // Creating an object of Student Student s1=new Student(); // Accessing member through reference variable System.out.println(s1.id); System.out.println(s1.name); } }
Example 2
Dog tuffy = new Dog("tuffy","papillon",5, "white");
The result of executing this statement can be illustrated as :
We can assign value of reference variable to another reference variable. Reference Variable is used to store the address of the variable. Assigning Reference will not create distinct copies of Objects. All reference variables are referring to same Object.
// Object reference example class Rectangle { double length; } class RectangleDemo { public static void main(String args[]) { Rectangle r1 = new Rectangle(); // Both r1 and r2 refering to same object // Any chane in the object state will affect to both. Rectangle r2 = r1; r1.length = 10; r2.length = 20; System.out.println("Value of R1's Length : " + r1.length); // Will print 20 System.out.println("Value of R2's Length : " + r2.length); // Will print 20 } }
Strings are a special case; they are immutable (so it cannot be modified at all) and act like primitives in case of assignments.