Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between classes and object instances?

A class is a binding of methods and variables in a single unit.
An object is an instance of a class.

These are definitions for classes and objects in programming books. My friend said that a class is a blueprint of an object. An object is a real "thing" of a blueprint. He has given an example: if Company is a class, INFOSYS, CTS, and TCS etc are objects. Whenever I think about classes and objects, these definitions confuse me. If channel is a class, what would be objects for the class? If water is a class, what would be objects of class? Can you clarify?

like image 645
swaminathan_manickam Avatar asked Dec 08 '12 06:12

swaminathan_manickam


People also ask

What is the difference between classes and objects?

A class defines object properties including a valid range of values, and a default value. A class also describes object behavior. An object is a member or an "instance" of a class. An object has a state in which all of its properties have values that you either explicitly define or that are defined by default settings.

What is the difference between a class and an object or instance 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.

What is difference between instance and object?

An instance is also the physical manifestation of a class that occupies memory and has data members. The difference between the two is that an object represents a set of instances while an instance is a certain, specific representation.

What is the difference between an object and an instance of a class Java?

In simple words, Instance refers to the copy of the object at a particular time whereas object refers to the memory address of the class.


3 Answers

I'll give you a classic explanation, you can find different versions of this all over the place.

A class is like a blueprint. Say you want to build a car, the first thing you would need is a plan, This is the class. The plan will describe 'methods' such as brake and hoot. It will also describe the various components of the car. These are variables.

A car object is an instantiation of a car class. You can have lots of these for one car class.

For example:

class Car:
    def __init__(self,color):
        self.color = color

    def hoot(self):
        "do stuff"


red_car = Car('red')
red_car.hoot()
blue_car = Car('blue')
blue_car.hoot()

Now, depending on the language you are using classes themselves can be objects (this is the case in Python). Think of it this way: All blueprints have some stuff in common. That common stuff is described in the blueprint's class (which is in itself a blueprint).

Then on the point of 'water' as a class you can approach it in a few ways depending on what you want to do:

way 1:

rather have a class called Liquid with variables describing stuff like viscosity, smell, density, volume, etc. Water would be an instance of this. So would orange juice

way 2:

say you were putting together a game with a bunch of blocks that would be made up of different terrain. You could then have classes such as Grass, Water, Rock, etc (think Minecraft). Then you can have a water class instance (a water object) occupy a specific position on the map.

like image 134
Sheena Avatar answered Sep 19 '22 06:09

Sheena


If channel is a class, Start Sports, BBC, and ESPN are its objects. If water is a Class, "Fresh Lime Water" and "Soup" are its objects.

Although you might find this explanation vague, this is the answer that I could think of.

Here is how you can learn about and distinguish classes:

Say you have a class "CAR"

Its objects are Hyundai, Ford, Suzuki. It will have the same methods but different designs -> this is how you can relate objects and classes with the real world.

like image 16
droidchef Avatar answered Sep 20 '22 00:09

droidchef


A class specifies the behavior of its instances.

A class is an instance of a class too (class for a class is named a "metaclass").

A class is an abstraction. You find a class by finding generic properties applying to a group of objects.

Then a class is a template which defines the methods (behavior) and variables (state) to be included in a particular kind of object

Recognition of classes (outside classroom) requires experience.

Read anything from Alan Kay, he is the inventor of Object Technology, and one of the inventors of Smalltalk, the only pure objects environment as of today.

like image 5
Hernán Avatar answered Sep 19 '22 00:09

Hernán