Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between Classes, Objects, and Instances

What is a class, an object and an instance in Java?

like image 583
Pranjut Avatar asked Aug 01 '09 05:08

Pranjut


People also ask

What is the difference between classes and objects?

Object is an instance of a class. Class is a blueprint or template from which objects are created. Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. Class is a group of similar objects.

Are instance and class the same?

A class is a blueprint or prototype from which objects are created. An instance is a single and unique unit of a class. Example, we have a blueprint (class) represents student (object) with fields like name, age, course (class member).

What is the difference between class object and instance object in Python?

Everything in Python is an object such as integers, lists, dictionaries, functions and so on. Every object has a type and the object types are created using classes. Instance is an object that belongs to a class. For instance, list is a class in Python.


1 Answers

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. So, 'object' and 'instance' are the same thing, but the word 'instance' indicates the relationship of an object to its class.

This is easy to understand if you look at an example. For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister's house is another object (another instance of class House).

// Class House describes what a house is class House {     // ... }  // You can use class House to create objects (instances of class House) House myHouse = new House(); House sistersHouse = new House(); 

The class House describes the concept of what a house is, and there are specific, concrete houses which are objects and instances of class House.

Note: This is exactly the same in Java as in all object oriented programming languages.

like image 121
Jesper Avatar answered Oct 03 '22 17:10

Jesper