dadoo
dadoo

Reputation: 19

Is a class an instance of an object?

Is a class an instance of an object? Can you create many objects from the same class, and can a class have many instances?

Thank you for your help, I'm reading a book on Java programming and am currently confused about these definitions.

Upvotes: 1

Views: 860

Answers (4)

Dania
Dania

Reputation: 11

A class is not an instance of an object. In fact, an object is an instance of a class. Here is the explanation,

Conceptually, each class represents and describes the characteristics of an independent concept in a program. these characteristics are also owned by any object of this class type. Usually, instance variables of a class defines properties of an entity. For example, if you have a class called Student, then obviously this class should have instance variables that represents student's characteristics such as name, level, grade...etc. Also, it can have methods like getGrade (), getName (). Now, any object of type Student will have the properties defined by its class (eg. name, level..), meaning the information stated in the class is encapsulated in its objects. Also, this object can be used to reach for the methods like getGrade or getName (again because these methods are encapsulated inside each object).

The class is the definition of a single concept structure, and the object is an implementation of this structure. That's why, we say, an object is an instance of a class. Note that we can have various instances (objects) of a single class, each object will encapsulate the same structure defined by its class, but with different values. For example, you may have several objects of the class Student each represents a different student with different name, level, grade...etc. Although these objects are different, they share the same structure, meaning they are all students.

Data encapsulation provided by class/object technique is really useful. Suppose you have 100 students who you need to store their names, levels, and any other related information. Instead of defining 100 String variables to store each student name, and 100 int variables to store the levels, you simply create a class which defines a student structure and create as many objects as you want. It saves both time and effort, and it makes debugging much easier.

Upvotes: 0

e.doroskevic
e.doroskevic

Reputation: 2167

The CLASS works as a BLUEPRINT which allows you to create OBJECTS based on this BLUEPRINT.
You CAN create many instances (OBJECTS) of the same CLASS.

To clarify:

Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.

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

Upvotes: 0

allen1
allen1

Reputation: 744

A class describes the structure of an object and is not an instance. An instance is an object that conforms to the structure defined by the class. There can be many instances of a class.

For example, consider a "Person" class. When creating the class, you could say, "A person may have a first name or a last name," but you're not creating an actual person, you're just saying what makes up a person. When you create a person, you create an instance of the class "Person" and you may have many people, which means having many instances of the class "Person".

Upvotes: 1

arshajii
arshajii

Reputation: 129497

A "class" conceptually is not an object (or an "instance of an object", which makes no sense really), but rather a sort of blueprint from which objects are created. Objects are instances of classes. You can indeed instantiate a class many times.

Don't confuse this with instances of the Class class, which are objects (e.g. String.class, int.class etc.).

Upvotes: 6

Related Questions