DEV Community

loading...

What are Classes and Objects What is their difference?

beransantur profile image Beran Santur ・1 min read

Class

Classes are just blueprints. That's all.

They are a data type -user defined- just like String.

They have attributes (data) and they have methods (functions)

They can hide data and methods which are only used internally in class.

Objects

If classes are blueprints, objects are the buildings that made from those blueprints.

They carry all the data and methods class has. So they are created from classes and they represent them.

They all can have a different identity for example Car is a class but Audi is an object.

Class: Fruit Object: Apple, Banana, Mango, Guava etc..

Difference between Objects and Classes

Classes are just defination of objects they are just templates or blueprints of objects whereas objects are instances of classes.

Classes is declared once but objects can be created multiple times.

Class is a logical entites but objects are physical entities.

Objects allocates memory when its created, on the contrary classes don't allocate memory when its created(memory is taken up for class definitions isn't on the stack nor heap. When you create an object of a class it will always go on either the stack or heap. Class declaration tells the compiler and runtime how much memory to allocate for each class instance, but only when requested.).

Discussion (0)

Forem Open with the Forem app