Class and object in Java

Asked By 10 points N/A Posted on -
qa-featured

 

A Java program is composed of object and classes. Where do object and class in Java differ? What are their functions in Java programming?

SHARE
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #102040

Class and object in Java

qa-featured

 

A class is an encapsulated entity that contains the definitions of some meaningful data and procedures that can operate on the data. Whereas an object is an instance of the class from which it is created. The object responds to different operations performed on the data defined in the definition of the class.

Physically a class does not exist in memory to be operated upon. However an object of the class contains the physically allocated memory space for data members of the class. Two or more instances can be created from a class, called objects. In general, the objects do not share the data elements (except in some cases), however, the methods/operations defined in the class are shared by the data members.

Answered By 5 points N/A #195501

Class and object in Java

qa-featured

 

Hi,

Let me first compare the real world object with software object since they have very similar characteristics. Whatever we see in this world as things can be considered as objects like table, chair, dogs, cars, humans etc… They have states and behaviors. For example, if we consider humans, the state is named, sex, religion etc. whereas behavior is walking, talking, thinking, etc… Likewise software object also has state and behavior, the object's state is stored in fields and behavior is shown via methods.

A class is a blueprint from which individual objects are created. Java class objects exhibit the properties and behaviors defined by its class.

The following example code would help you to understand more about class and objects.

public class Human{

String name;

String sex;

String religion;

void walking(){

}

void talking(){

}

void thinking(){

}

}

 

Related Questions