2011-11-23

What is a class and object in java ?

Java is an object oriented programming language. This means Java programs use objects and classes.So what is an object? An object is anything that really exists in the real world and can be distinguished from others. Every thing that we see physically will come into this definition , for example every human being, a book, a tree, and so on.
Now every object has properties and exhibits certain behaviour. Let us try to illustrate this point by taking an example of dog. It got properties like name,height,color,age etc. These properties are represented by variables. Now the object dog will have some actions like running,barking,eating etc. These actions are represented by various methods (functions) in our programming. In programming various tasks are done by methods only. So we can console that objects contain variables and methods.

A group of objects exhibiting same behaviour (properties and actions) will come under same group called class. A class represents a group name given to several objects. For example take the dogs: Tom,Jimmy,Pinky,Subbu etc . All these four exhibit same behaviour and hence belongs to the same group, called dog. So dog is a class name which contains four objects. In other words we can define a class as a specification or blueprint or template of an object that defines what goes to make up a particular sort of object. A class can be used as a model for creation of objects.
A class and object are almost the same with the difference that a class does not exist physically, while the object does. For example if we say dog; it forms a picture into our mind with 4 legs, 2 ears some length and height. This picture in our mind is class. If we tally that picture with the physical things around us, we can find Tom living in our house is satisfying these qualities. So tom physically exists is an object and not a class. In technical terms we say object as "It is an encapsulated form of all non static variables and non static methods of a particular class."The process of creating objects of a class is called instantiation.

Comment for any doubts.......

Loading

Enter your Email here: