Class and Object in Java | Coders Pathshala
Class and Object in Java
Hello Coders,
Welcome to "Coders Pathshala". Today we are going to understand the concept of "Class and Object" in Java. So let us start with it. Java is an object-oriented programming language. So understanding the concept of "Class and Object" is of utmost importance.
A class is a basic building block of all Java programs. It is a logical way to group together :
1. fields that hold values (Data)
2. methods that operate on those fields
Class = Field (Data) + Methods
The class provides the structure or blueprint for creating objects.
Let us take an illustration of a designer of a house who fundamentally determines a plan of a house for example how might it look like yet doesn't assemble any house. Based on this plan, a builder can build at least one house. Similarly a class indicates the format for making objects. It possibly portrays how the object will appear as though when they are created. So a class is only an outline or layout dependent on which at least one object can be created.
Let us try to understand the concept of class and object using a simple Java Program.
1. Create Rectangle Class
Rectangle Class
The above-mentioned code defines a class named "Rectangle" with length and breadth as fields. Also methods named setData() and area() are also defined to manipulate the fields. setData() method is used to initialize the fields length and breadth whereas the area() method is defined to calculate the area of the Rectangle object.
2. Creating object of Rectangle class
Creating rectangle object
Here inside the main method, we are creating the object of Rectangle class. Let us try to undertsand the following statement :
Rectangle r = new Rectagle();
This statement is doing the three things .
1. Creating reference variable
To the left hand side of "=", we have Rectangle r ,
which is creating a reference variable of type Rectangle. A reference variable holds the reference of an object.
2. Creating object
To the right hand side of "=", we have new Rectangle(); which is actually creating the object of type Rectangle Class. new is the dynamic memory allocator. Memory is allocated to the object as soon as this statement is executed.
3. Assigning the object to the reference variable
When we combine step 1 and 2 ,using "=" , the rectangle object is being assigned to the reference variable r. The reference variable "r" will be used to manipulate the specified object now.
So in the statement Rectangle r = new Rectagle(); r is a reference variable which will be used to manipulate the Rectangle object .
Once we have reference variable ,we can call any method of the corresponding class to perform some operations on the object.
e.g. the statement r.setData(10,20); will initialize the object referenced by r with length as 10 and breadth as 20.
I hope, now you are able to understand the concept of "Class and Object in Java." I have also uploaded the video lecture on my YouTube channel named "Coders Pathshala" whose link is given below:
YouTube Video Link: https://youtu.be/uTnjsm6DaWA
𝗦𝗼𝗰𝗶𝗮𝗹 𝗟𝗶𝗻𝗸𝘀:
1. 𝗬𝗼𝘂𝘁𝘂𝗯𝗲 - https://www.youtube.com/c/CodersPathshala
2. 𝗙𝗮𝗰𝗲𝗯𝗼𝗼𝗸 - https://www.facebook.com/CodersPathshala
3. 𝗟𝗶𝗻𝗸𝗲𝗱𝗜𝗻 - https://www.linkedin.com/company/coders-pathshala
4. 𝗧𝘄𝗶𝘁𝘁𝗲𝗿: https://twitter.com/CodersPathshala
5. 𝗜𝗻𝘀𝘁𝗮𝗴𝗿𝗮𝗺: https://www.instagram.com/coderspath/
6. 𝗣𝗶𝗻𝘁𝗲𝗿𝗲𝘀𝘁: https://in.pinterest.com/coderspathshala/_saved/
Please like, comment, follow, share and subscribe my channel and
pages.
#CodersPathshala #DeepakRattan
Happy Coding!
{𝗖𝗼𝗱𝗲𝗿𝘀 𝗣𝗮𝘁𝗵𝘀𝗵𝗮𝗹𝗮}
Note : This reference is taken from youtube :
https://www.youtube.com/c/CodersPathshala
Note : This reference is taken from youtube :
https://www.youtube.com/c/CodersPathshala


Well explained 👍
ReplyDelete