Java Essentials - Class and objects in java
6K views
Oct 24, 2024
Java Essentials - Class and objects in java
View Video Transcript
0:00
In this video we are discussing class and objects in Java
0:05
Class is nothing but one common blueprint of objects and one object is nothing but instant
0:11
session of a class. When you define one class, no memory space is going to define or going to occupy in the computer's
0:19
memory during the runtime of the program. But whenever we define one object under the class, then instant session will take place
0:28
Instantation means some memory space will be occupied during the runtime of the code against the objects as we are going to define under the classes
0:38
So class is one blueprint and object is the instant session of a class
0:44
So there is a basic concept. So now let us go for the discussion on them
0:49
What are the classes? A class is the internal structure or blueprint of an object
0:56
the syntax to define one class is class this a keyword and then we're having this class
1:02
name within Caldibrasis will be having the member variables and the member functions
1:07
these member variables may be having the same or different data types and they will decide
1:13
that what kind of data and for what this data will be stored there and this member functions
1:19
will be defining or will be allowing us to do some legitimate operations on those variables so member variables are there to store some data of same or different data types and member functions are defining that what are the permitted operations that we can carry out
1:38
on those data. A class will contain some member variables and member functions and members can be
1:44
accessed throughout the inter class and sometimes from the outside of the class also
1:50
When we define one class, no memory space is going to get off
1:54
So this is our way to define one class. Now what are the, what are the respective objects
2:03
An object is an instance of the defined class type. When a class is defined, no memory space is allocated, but whenever object is defined
2:12
memory spaces are getting allocated. We can say that the classes are the user defined data types, and the objects are the variables
2:21
of that type. very basic concept so classes are nothing but use the different data types and
2:27
objects are nothing but the variables defined under those data types in Java
2:33
we need to initialize objects by the keyword new and syntax for creating an
2:39
object will be like this so that is a class name then the object name is equal to
2:44
new and the constructor of the class that is a class name constructor so this
2:50
the syntax and here we are using the keyword new For the better understanding let us go for one practical demonstration to show you that how to define classes and objects in our Java program in this demonstration we shall discuss class and object
3:05
in Java so here the name of the class is student and this particular class is having three
3:11
member variables of the student class and their string name i nt id and string major so as here
3:18
we haven't mentioned them that they are under a private score of public scope that means
3:23
they are in the default scope so here we're having one method the name of the
3:28
method is set name from the name of the method it is quite obvious that it will
3:32
take one string as input and which will set the name instance variable here
3:37
and similarly for set major it takes a string as input argument and it
3:41
initializes the major instance variable and the display method is actually printing the name ID and the major and in this way all the three instance
3:52
variable contents will get printed using this display method so here the name of the
3:58
program is class object or Java you can find that this particular class is having
4:02
class object or Java you can easily find that in the same Java source code we
4:07
have declared two classes one is a class student and the one is the class class
4:12
object so what will happen if we define multiple classes in the same Java program
4:17
in the same Java source code then one of the classes will be of the type will be
4:22
will be under the scope public and main function will be defined in one of the classes only So this public static void main string array arcs and here we have defined two objects under the
4:35
student class and those two objects have got instantiated, memory spaces have got allocated
4:41
and they have got instantiated by using new student class constructor. Now we are using the set name, we are using the set major to initialize the name and
4:52
the major instance variables and directly were writing s1. ID is equal to
4:59
1 021 to instantiate or to initialize this ID instance variable. So similarly we have
5:06
done the same for the s2. S2 is another object under the class student and which has got
5:12
instantiated using the constructor of the student class. And now we are going for
5:18
the s1.2. Display methods. So let us go for the s1. display and s2
5:22
for the output so the student name joy has been printed the student ID
5:28
one zero to one a major subject here computer science and in this case the name
5:33
and the ID and the subject has got printed and they have got printed after
5:37
calling the method is one dot display method and s2 dot display method so in this
5:43
way we have discussed that how the Java in case of Java how we can
5:48
define one class and then how to define objects under the class and access class members thanks for watching this video
#C & C++
#Educational Software
#Java (Programming Language)
#Programming