Hi Friends,
After Many days i am going to post And will be continue , Today i will talk about Class and Object in Java, we all know its very easy topic but for freshers its very confusing then only know definition of it but not real meaning of it. Many java programmers dont understand the exact meaning of Class and Object even after reading theory of Class and Object.
Before starting i ask you for drawing the picture of Laptop, and how you will do it?
First you will think about Laptop and then you will put this thinking on Paper and I will ask for manufacturing of Laptop according to your drawing.
Then
Drawing of laptop is Class And Manufactured Laptop is Object.
Now you come to know that class is just Logical Entity and Object is Physical or Real entity.
First See Differences--
Class | Object |
---|---|
Class is Blueprint of object, means first we just draw logical thing on paper and then by using that blueprint we create object in real life. | Object is an Instance of class, here instance means result of class, means you are drawing of laptop on paper and then you manufacture |
Logical entity means it doesnot exists in real | Physical or real entity means exists in real life |
doesnot occupy any space or memory, means class is only on paper so its not possible to occupy any memory in real life | Occupy memory because it exists in real, Object are created in heap memory |
Class is declared only once | Object is created many times depends on requirement. |
basic way of creating object is by new keyword | we can create class by using Class keyword |
Now you have checked differences, now you have some idea about it
Now come to technical wording
Class-
Class is a blueprint of an object that contains variables
for storing data and functions to performing operations on these data.
Class will not occupy any memory space and hence it is
only logical representation of data.
Class will not occupy
any memory space. Hence to work with the data represented by the
class you must create a variable for the class, which is called as an
object.
When an object is
created by using the keyword new, then
memory will be allocated for the class in heap memory area, which is called as
an instance and its starting address will be stored in the object in stack
memory area.
When an object is
created without the keyword new, then
memory will not be allocated in heap I.e. instance will not be created and
object in the stack contains the value null.
When an object
contains null, then it is not possible to access the members of the class using
that object.
To
create a class, Just use the keyword "class" followed by the class
name:
Synatx to create Class
class Student {
//assign variable
//create obj here
}
Object:
Objects are the basic run-time
entities in an object oriented system.
They may represent a person,a
place or any item that the program has to handle.
Object is an instance of a class Object is a real world entity.
Object is an instance of a class Object is a real world entity.
Object has state and behavior in Java. State is
represented using instance
variable and static
variable and behaviors are implemented
using methods in Java.
Synatx to create Object
Student stu=new Student();
objects are
created with "new" keyword. " new" allocates memory to
the newly created object at runtime.
Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.
Happy Learning
Neeraj Srivastava
Java Developer
Neeraj Srivastava
Java Developer
Thank u,it was really useful & easy to understand!
ReplyDeleteThank you.
DeleteGood explanation in easy language.
ReplyDeleteThank you
DeleteThank you
ReplyDelete