- Podcasts
- The Java Developers Hub
- Object-Oriented Programming (OOP) in Java | Classes, Objects & the new Keyword
Object-Oriented Programming (OOP) in Java | Classes, Objects & the new Keyword
The Java Developers Hub
- December 3, 2026
- 1 h 5 min
- Episode 8
About this episodeObject-Oriented Programming (OOP) is one of the most important concepts in Java development. It provides a structured approach to writing…
- Understanding Object-Oriented Programming in Java.
- What classes are and why they are important.
- Understanding objects and how they relate to classes.
- Differences between classes and objects.
- How to define and create Java classes.
- Understanding object properties and methods.
- Using the new keyword to create objects.
- Understanding object references in Java.
- Accessing object fields and methods.
- Creating multiple objects from a single class.
- Practical examples of classes and objects.
- Benefits of OOP in software development.
- Beginners learning Java programming.
- Computer science and software engineering students.
- Developers transitioning to object-oriented programming.
- Aspiring Java backend developers.
- Programmers preparing for Java technical interviews.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Object-Oriented Programming (OOP) in Java | Classes, Objects & the new Keyword
Yes, so let's start. Welcome back Coder Army to our Java series and today we are going to talk about a very interesting topic which is Object Oriented Programming, in short we call it OOPS. Now you must have heard about this a lot of times and if you already know about Object Oriented Programming, then too you will have a lot of doubts about it, right? Today, in this video, we are going to clear all those doubts. And if you haven't heard about it, you are going to read object-oriented programming for the first time. Even then, you are going to enjoy it a lot. Because we are going to start from scratch and build our concept. So, let's go to the screen. It's going to be fun. So, first of all, what is object-oriented programming? Okay, we will understand this later. Before that, let's talk about what we know so far. So, imagine that we are doing something. We have to represent a name in it. We have to make a variable whose name is name. You will say, no problem, it will be fine. Now that it is a name, we can represent it in a string. So I have made string name from let. And here I can set any value of it. Let's say Aditya. Very good. Now I have represented the name of Aditya. Now I feel that I have to store the age of this Aditya. So you will say, no problem, I will make a variable of age. Int age. And let's say we put 28 in it. This is also done. Okay. I have stored Aditya's name. I have stored Aditya's age. Okay. If I want to store Aditya's roll number. Let's say he is a student. I want to store his roll number in a college. No problem. We will do int roll number. We have learned all this till now. And let's say Aditya's roll number is 101. We have stored this too. Okay. Now let's store Aditya's college too. So again string. Because college will also be in a string. And let's say I put here IIT Guwahati. Okay. Very good. Okay. So I have stored all the things of Aditya here. Now we are not understanding object oriented programming. Now we are talking about the basic that we know. Okay. I will remove this from the top. Yes. So, I have stored some details of Aditya. Okay. Now, you are understanding some problem here. You must be seeing a problem that to store one Aditya, one information of Aditya, I need four independent variables here. Name, age, roll number, college. Right? Now, these four independent variables are spread separately in the code. Okay? Meaning, I don't have any block that I can say, I am calling this block Aditya. Right. I have not put it in any block. I have kept them separately. Okay. So I don't understand whose name is this. Is this age of the same person whose name we are talking about? Is this role number of the same person? Is this college of the same person? I don't understand by looking at the code. Because one. If I talk about a student, whose name is Aditya. Okay. He has these four characteristics. Okay. And I have represented these characteristics through different variables. Okay. Now let's see. Now you will understand. Now what do we do? We talk about one more student. Let's say after this I have to represent one more student. Whose name is Rohit. Okay. Okay. Very good. So what do I do? First I save his name. Now again I will need a string for that too. Okay. Now obviously I can't put a variable name here. Because I have already written that. So I have to put name to. Right. Let's say I will write Rohit in this way and in the same way I will store the rest of the variables age, roll number and college. So let's do one thing, let's cut them directly from here. Sorry, let's copy it and paste it here like this. This is a little extra cut. Yes, something like this. So, what I do is, I change age to age 2, roll number to roll number 2, college to college 2. Then, I change age to 28. Let's say, Rohit's roll number is 102. And, I also change college to IT. Okay? So, I just made a few changes. So, you understand? Again, now, to represent Rohit, I again need a set of 4 variables. These 4 variables, right? So, I basically want to say, this is my Aditya and this is Rohit. But looking at the code, I don't know what this age 2 is. Is this role number 2 representing Rohit or Aditya? Who is this college 2 representing? Right? I have a lot of doubts in my mind. But till now, we used to write such codes. Okay. Now, we are looking at some problems. So, I will tell you one more problem. Let's say, tomorrow I make a function which prints all the details of one user. Let's say I want to print his name, roll number, age, and his college. So, I will make a function print. Obviously, you know how we make a function. We can make a function outside the main function. So, let's say void print. Let's assume it is outside the main function. Now I am showing you a series. This code will be inside the main function. And this will be outside the main function. I will call it in the main function. But how will I make a void print? I will pass a parameter here. And what will happen in the parameter? Let's say it needs a name. It needs an age. It needs a roll number. Just like this, we need a college. So, we need all these things. Only then, it will be able to print it. And you know, how can we print it? Nothing, we just, let me make it a little smaller. We can just call system.out.println. Right? I am not writing here, but you understood that we can simply print it. So, now you are seeing, one more problem is that whenever I have to print a student, I have to pass these four details in a function. If I forget even one thing, then I will not be able to print the student properly, right? So now I can understand a lot of situations and problems. So let's quickly list down all the problems and then move on. So I've listed down all the problems that we were facing. The first problem is that we need a lot of independent variables to represent one student. To represent one student, we needed 4 independent variables. So I was saying, where is our student? String name, int age, int roll number, string college, all these are somehow representing Aditya. But these are independent variables. Name, age, roll number, and college are independent of each other. I can't understand the code, that this is a student. We have written it, so we know that we are putting it in imaginary blocks. These are all imaginary blocks, but actually there is no block in the code. Okay, so let's see the next point. If I pass the student to a function, I need to pass all the four variables. Obviously, we have seen that when I had to pass it to a function, like I made a print function, then what I had to do, I had to pass all the variables. If I forget even one, then my student will not be able to print properly, right? And this print function doesn't even know what it is doing. It just takes independent 4 variables and prints them. So basically our code doesn't have any idea that we are conceptually representing a student. Right. Third, I need to create a new set of variables if I want to represent a new student. We saw that if we had to make Rohit, then I would have to take name 2, age 2, role number 2, college 2, these types of variables. Now suppose I have to make another student tomorrow, then I will have to take those 4 variables, I will have to change their name a little, name 3, age 3, something like this. Right. Okay. And last is very interesting. Look, I have written here, no authority over data. What does it mean? It means that when we declare these four variables, then who owns these four variables? Who has the authority? For example, I said, this name, age, roll number, and your college. Okay. These are independent variables. According to the code, they don't have any link between them. But if I talk, if I want to represent a student, then all these should be related to each other, right? They all represent a student. So, they should be related to each other. So, there should be someone who owns them. So, no one has authority over the data. If anyone can change it, everyone can consider it independent. Okay. You will understand more about this when we move forward. But I hope you have understood all the problems. We have four problems which we have to face. If we write the code which we have been writing till now. Okay. So, here... To solve all these problems, there is the birth of object-oriented programming. Now what is this? Now we will talk about this. First of all, Let's get rid of a myth. Look, object-oriented programming is a completely different programming paradigm. Complete paradigm means a way of writing code. So till now, as we were writing code in some way, object-oriented programming is a different way of writing code or thinking about code. So when you write code, you think how to write code. So this is a way of writing code. Apart from this, we also have ways of writing code. Now, the Java that we are studying is an object-oriented programming language. But there are also languages that are not object-oriented. They are called functional languages. But we are not going to talk about that. We are going to talk about object-oriented programming. So, we will focus on this. Okay. So, look. First of all, let's talk. We have seen the problems. Now, I am saying that what is object-oriented programming? How does it solve those problems? So, look. Its concept is that... Let me ask you the same question first. Why do we write codes? Why do we write codes? So you will say that we write codes to solve a problem. At the end of the day, you are solving a problem. If you make a simple calculator app, you can perform any addition, subtraction, multiplication, in it. If you do 2 plus 3, then the answer will be 5. So here also you are solving a problem. Right? What is the problem? That we take two numbers, what will be their addition? Or what will be their subtraction, multiplication, division? Right? So basically, what I mean to say is that you write code to solve a problem. And what are those problems? They are real-world problems. In our world, whatever problems arise, we write code to solve them. What is code? We call an application code, we call a website code, etc. So to solve any real-world problem, we make a website, software, application, right? Okay, great. Now, what I mean to say about object-oriented programming is that That if you want to solve a real world problem, then first you have to mimic the real world in programming. Mimic means, just like our real world, we have to make something in the same way in programming. So we will be able to solve better problems. And will be able to think better about the problem. So this is called Object Oriented Programming. If you want to solve the real world problem, then first start mimicking the real world in programming. What does this mean? Now this means that in the real world, we just gave the example of student, so I will go with that. In the real world, student is an object. This is a student. Now you will say, why did we call it object? Right? Look, we have to say something. All the entities in the real world, we have to name them something. So I named them objects. Everything is an object. Now I am not objectifying by saying this. I am giving a term that I want to give something to everyone. we have to say, whether it is a living thing or a non-living thing, we have called everyone an object, right, we can classify everyone as an object, whether it is a living thing or a non-living thing, okay, so I am saying that in the real world, a student is an object, object means a complete entity and it has some things, okay, things means it has some characteristic, it has some identity, like that student must have some name, There must be some age of that student. There must be some, let's say, roll number. And there must be some college of that student. Those four variables which we had declared in OOPS. I had said that this is the identity of that student. If you see any student, you must know that he has any name, age, roll number, college. You must know. So, OOPS, Object Oriented Programming says that if you take this student from the real world. This is your real world, let's say. Real world. And this is your programming world. Okay. So if you are trying to represent this student in the real world in the programming world, then don't you think you should make only one variable instead of four independent variables? Let's say only one variable or you should make only one block in which we can represent these four together. Okay. So in this block, there is a part of name Ek paat ho let's say age ka, ek paat ho let's say roll number ka aur ek paat ho college ka. So, one block represents all these things. Because in the outside world, there is only one block. Now, what is this person? It is an object. So, I will call it a block. One block represents all these things. So, basically, the meaning is the same. Because in real life, we have a student as an object. So, in the programming world also, we should have a student as an object. Now, there is a living object in real life. Okay. Which has four characteristics. How do I mimic that? I work. I make a big block. I put all those characteristics in it. So, as these four characteristics come inside a student, they have an intrinsic property, right? So, in programming too, these four characteristics, name, age, role number and college, should come inside it. It should have an intrinsic property, right? Very good. So, this concept is called oops. This is its basic concept, okay? That it should be like this. So, basically, we have to mimic the real world. So, what we have to do is, we have to talk in terms of objects. Till now, in programming, we were talking in terms of variables, functions, in terms of all these. But now we have to think in terms of objects. And now in Java, if we make a block in which our multiple variables come, we call it objects. So this is the basic concept of object-oriented programming. So now let's see what's happening. What's happening in programming? Something like this is happening in programming. That I have some syntax. Right now I'm going in syntax, we'll see that in a while. So something will happen, a code block will open somewhere. Right? A code block will open like this. And in that, we will represent all the variables, name, age, role number, and college together. So, something like this will happen according to me. That we will represent name, age, role number, and college together. We will represent them in the same block. Now, this is the same block. The problems I talked about earlier, those were all independent variables. Now, these are not independent variables. Now, this is the same block. And the same block means, we are representing a real world student. Right? But now, let's talk a little about mercy. You will enjoy it a lot, which I am going to explain to you. Now think, whenever someone gives you an oops, he tells you that we have to mimic the real world. Very good. I want to go a little deeper in this. I am telling you that I said I had to mimic a student, a real world student. But what is a student? It is a human. What is a student? It is a human. And a human can't be just a student. A human can be a lot of things. If a human goes to college, he becomes a student. If a human goes home, he becomes a son or daughter of his parents. If a human goes to an office tomorrow, he becomes an employee. So basically, humans play multiple roles. They can be a student, a son or a daughter, or an employee. This depends on what scenario they are in. If they are in a college or a school, then they are called a student. If they are at home, Right? So, he is called a son or daughter. And if he is an employee, basically if he is in an office, then he is called an employee. So, something like this happens. I am talking about the real world. Now, I said, how did I represent a student in programming? I represented it with four variables. Name, age, role number, and college. But you think, are these four variables enough to represent an entire student? The answer is no. In a real world student, there are many identities. Apart from his name, age, role number and college, there can be his grades. There can be his subjects. There can be many more things. There can be his individual marks in every semester. There can be many things. There can be many things in a student who are part of his identity. But we have only represented four things, name, age, role number and college. Why? And one more question, we have only represented the student, but the student is actually a human. Now, when that human goes to his home, he will have more characteristics. For example, what will be the name of his home? Now, that human will become an employee tomorrow. So, he will have more characteristics. Let's say, he will have an employee's ID, his office name, his salary, etc. So, basically, I want to say this. That a human plays different roles in the real world. And in every role, he has his own identity. So when I talk about a student, I talk about some of his identities. Then when he goes home, he has some other identities. When he becomes an employee, he goes to office, he has some other identities. So in programming, should I represent this entire hierarchy? Look, the answer is absolutely no. You think of it like this, that I wanted to mimic the real world. Mimic means to mimic the real world. Just like you see the real world, show yourself in programming in the same way. Okay, very good. And look, why did I have to mimic the real world in programming? I had to solve the problems through my code. So if I want to mimic the real world and solve my problems, then am I concerned with this entire hierarchy? The answer is no. It depends on what I am writing the code for, what I am making the application for. So let's say I am making a portal for college, where students can come, mark their attendance, see their subjects, etc. So if I make a portal for college, Let's say I'm making a portal in my application. This is the real world on the top and this is the programming world on the bottom. So I'm concerned only with the student. I'm concerned only with the student part of that human. I don't mean that he will be called a son or daughter tomorrow. I don't mean that he will be called an employee tomorrow. I mean that if a person is a student, then how can I represent the student in my programming? And because I want to make it a college application. And I found out that even students can have many characteristics. But I only mean those characteristics for which I have to write code. Which is going to be useful in my code. I will get the name of the student useful. Let's say age will be useful. Role number will be useful. And let's say college name will be useful. Assume that these four variables are enough for me to write the code. So I am concerned only with these four variables. I don't mean that it has grades as well as subjects. If both these variables will work, then you can include them as well. But if they don't work, then I don't care. Okay? I hope you have understood that I am not representing the whole real world as it is. I am just trying to mimic a part of it. So if a student has a lot of things in the real world, then I am only interested in those particular characteristics that solve the problem for me. make that particular characteristic an object, I will put it in a container, like name, age, roll number, etc. And I said this is my object. Now this object is representing the real object or mimicking it. Okay. So remember, if tomorrow I make an application for employees, then I will focus on the characteristics of the employee. If tomorrow I make an application for a son or daughter, then I will focus on its characteristics. I will always store all the things in the container in programming that a son or daughter will need, or here I will store all the things that an employee will need. That means I will always focus on the characteristics of humans. Which will help me to solve a problem. Okay. I hope you understood this. This one. Many people can't understand that we are mimicking in programming. But characteristics can be very high. So I hope this thing will be clear to you. Okay. Now let's go towards actual code. To represent a real world student in programming. Okay. So first I will make two worlds. This is real world. Okay. And this is our programming. Look. I said. That in the real world, student is an object. Something like this. And what does student have? Characteristics. Student can have name, age, role number, and college name. Okay? Okay, very good. Now you answer one more question. Whenever I say student, like this is a student. What image comes to your mind? Because think, student is an abstract term. Abhi tak mai kisi absolute student ki baat nahi kar raha. Absolute matlab ek bachcha jiski mai exactly baat kar raha hu. Kisi naam ki baat nahi kar raha. Par agar mai baat kar raha hu. Bhai let's say Arun. Ek exact bachcha hai. Arun ke baas khud ka naam hoga. Obviously arun hi hoga. Uski age hoga. Uska roll number hoga. Uska college ka naam hoga. Par Arun ek exact bachcha hai. Aise hi mai baat kar raha hu. Let's say Varun. Varun is also an exact student, an absolute student of which I am talking. He will also have some name, age, role number, college. But when I say student, it is an abstract term. I just want to tell you, I want to represent an idea of what a student is. Basically, when I say student, I'm talking about an idea of what a student looks like. Every student has a name, age, role number, and college. But when I talk about an absolute student, I'll say like Arun, Varun, Aditya, Rohit, etc. So in programming, first we represent this abstract term using a keyword called as class. So see what I write. First of all, I'll write a keyword class. And then I'll write the one I want to represent. So, student. We'll call this class's name. Okay? Then I'll turn on my curly brackets. You know what curly brackets mean. In Java, it's to turn on the code block. Now, what did I do? Till now, class student. Now, in this, I can represent all the characteristics that a student has. Name, age, role number, college. Using variables. You know it. I can write string, your name. Okay? Int, your age. Int, your role number. Something like this. And string is your college. Right? I hope you understood this. So in programming, I write a code or syntax that I have made a class student in which name, age, roll number and college are the four variables. Okay, so I have represented a real world abstract idea of a student. But remember, I have not made an actual object yet. I have not made a container in my memory yet, which will actually hold the data, name, age, roll number and college. Abhi tak mainne bas yeh represent kiya hai ki mera student dikhta kaisa hoga. Ki mera student ka idea kaisa hoga. Isi liye hum java mein, isi liye hum in classes ko kya kehte hai? Hum in classes ko kehte hai blueprint of an object. It is telling a blueprint that how the object will look like. We haven't made an object yet, we will make it below. But it is telling me how an object will look like, what characteristics will be there in it. So, name, age, roll number and college, these four characteristics will be there in it. Now, I have to make an exact object of this student. So, how can I make this object? Before that, I will copy this class. This class is going to be useful to us. Now, I have copied my student class and I will show you how I make its object in programming. Before that, understand a small thing. We have talked about this before. We have two types of data types. You know, one is your primitives. And we talked about non-primitives. Non-primitives. Okay. And till now we were reading all the data types. Which all did we read? We read integer. In that you know int, short, byte, long. We read floating points. In this we read float and double. We read boolean. We read character. In this we read care. You already know all this. We talked about it. Apart from this, we also have non-primitive data types. We haven't talked much about it yet. We just told you that, for example, arrays are a non-primitive type. Now you understand, these non-primitive types are those which we call objects in programming. Basically, all the objects we are going to make, objects, are a non-primitive data type. Why non-primitives? Because obviously they can't be represented in a variable. Like we made int, float, boolean, char etc. These are non-primitives. What was the object? It's a container. And multiple things can come inside that container. So we called it non-primitive. Okay? Good enough. Now, since everything is an object, there will be some objects that are already made. Okay? which we have already made in Java in our language, and something which we will make by ourselves. What we will make by ourselves, we will call them user-defined data type. Okay? So, it means that these will also be a data type. Like this is a data type, this will also be a data type. But these will be user-defined. We have made such objects by ourselves. If you want an example of this, then look at this. Here we are making this class, student. We will make an object of this. So, this is a user-defined data type. So, we have made a data type. We have made a container. We are storing things in it. But we have defined it by ourselves. It is user-defined. Okay. So, I hope you have understood this. This was just a small thing. After ignoring this, we will move on to how we will create an object of this student. Okay. So, now I have to make an object of this student. So, this is a two-step process. I will tell you how I have to write code. First of all, I will I will make a variable of this student as S1. I have declared a student. Till now no student has been made in the memory. I have just declared a variable. Now what is the type of this variable? Student. I said that it is a user defined data type. We have represented a data type ourselves. Like I write int x. Right? So what I say is that there is an object in memory whose name is x. It can store a variable. Let's say 4. And what type is that? It is of integer type. Similarly, I wrote student s1 here. I said s1 is a variable. It can store something. What can it store? I will tell you now. And what type is it? It is of student type. It is of student type. Okay. So, first I declared. Now, if I... Again, if I talk about int x, then I call it declaration. If I had to give value to it, then I would have written x equal to 4 in the next line. Later, I told you that we can also mix it. Int x equal to 4. We can write like this too. So, first we saw declaration and definition in different lines. Then we saw both together. So, here also we see declaration in different lines. Now, see how we can do definition. Now, I can write this s1 equal to. Which value do I have to put in this variable? New student. Now, this syntax is a bit new. It can be a bit new for you guys who are reading object-oriented programming for the first time. Now, if we understand this carefully, then what does it say? First of all, I said declaration. I declared a student as S1. I made a variable. And in that S1, I put new student. Now, let's understand this one by one. Okay. Very good. Okay. You must be seeing this. Now, I told you that we have two types of memory. One is your heap memory. Let's say this is your heap memory. And one is your stack memory. Now what is memory, how does it work, what are the things in it, we will see that later. A proper video will be made dedicated for this. For now, let's assume we have two memories, heap memory and stack memory. All our objects are made up of heap memory. Now I will tell you what happens. As soon as you have written this line, I will tell you what will happen. First of all, you wrote student s1. So nothing happened. You declared a variable named s1. And you said that we are going to represent an object, a user-defined data type. Very good. Then you wrote s1 equal to new student. Now see what happened. So I will go right to left because you assign these right things to the left from assignment operator. So I will tell you. This is a new keyword. It says that in our heap memory, I have to allocate a space. I have to allocate a space in which I will store my object. Which type of object will I store? I will store the object of student type. So, what did Neo say? That brother, allocate a space in heap memory and what will that space store? That space will store the information of our student. Because what is its type? Type is student. This new keyword said yes. Okay. Then what did this student say? We will see this in the next lecture. This is very important. But for now, you just understand that as we wrote new student, then what will happen? A space will be allocated in our heap memory. Then look, I assigned this new student to a variable named s1. What does this mean? Now look, when this space will be allocated, So this will have some address, let's say its starting address will be 1001 And it will have some ending address, what will be the ending address, that will come later But now you see the starting address will be 1001, from here the memory starts Then I have allocated a chunk of space So when I write new student and assign it as s1 So its starting address will be 1001 assigned in this s1 variable But now where is this s1 variable? This is the S1 variable in your stack memory. And what does it store? It stores this 1001 memory address. So I can't say in one way that it is storing the location of our object. This S1 is storing this. the location of our object because it is storing its starting address. If I get the starting address, then obviously I get the complete address of the block because it is stored in the contiguous memory. So what do I call this S1? I call this S1 our reference variable. What do I call this? I call this reference variable. Now, what is this reference variable? It is nothing. It is a special kind of variable in our Java which stores someone's reference. Reference means it stores someone's address. Just understand this much. So, S1 itself is not an object. In the starting, many kids get confused in this. They think student S1 means S1 only became an object. No. S1 is not an object. S1 is just a reference variable which is storing the address of an actual object. Which type of object? Student type object And where is that student type object? It is in heap memory And where is S1? It is in stack memory Why is it in stack? Why is it in heap? We will see later But now you understood This reference variable will be in stack Object will be in heap S1 will only point the address of this object It will refer it So what did I call it? Reference variable Okay, very good So what is the name of this object? You said, S1 is the name of the reference variable. It points to an object. But what is the name of this object? There is no name for this object. It is unnamed. Unnamed. Okay? We don't name the heap that is stored, we just refer it to a reference variable, and reference variable has its own name. So the reference variable S1 has its own name, so we generally say that student S1 is an object, but actually S1 is not an object. This question can be asked specifically in interviews, if you are giving an interview for Java. Is S1 an exact object? No, S1 is just a reference variable. Which holds the actual object. Actual object is made from here. Okay. And it doesn't have any name in Java. Okay. So, if you understood till here. Okay. And you understood this syntax too. So, what did I say? We can write both these lines in one line. How can I write simply? I can write. Student. S1 equal to new student. New student. With this one line, I have said everything. I have said that first of all, allocate a block of memory in heap memory and assign it to reference variable named S1. Okay. Now, one more thing. Whenever I write a new keyword, I mean to allocate a chunk of space in heap. Now, the chunk of space allocated in heap memory is in your runtime. Okay. That means new keyword works on runtime. Now, the question can come, what is runtime? So, let me tell you. Look, in code, we generally talk about two times. One is your compile time. And one is your run time. And in programming, some things are in compile time and some are in run time. For example, we talked about some errors that will come in compile time. When your code starts compiling, it will tell you that you have an error. And some errors come in your runtime. When your code starts running. Then some errors come. So basically I said some things happen in compile time. And some things happen in runtime. This memory allocation. From new keyword. This happens in your runtime. So exactly when you wrote code. Then only memory will not be allocated in heap. But when your code is running. Then your memory will be allocated in heap. That's why we call this new keyword. Dynamic memory. Memory allocation. It dynamically allocates memory. What does dynamically mean? On runtime. So, if you talk about runtime, then we call it dynamic allocation. And if you talk about compile time, then we call it static. Just remember that there are some terms. Static memory allocation means the one that is at compile time. Dynamic memory allocation means the one that is at runtime. Okay? Contrary to this, when I used to write int x equal to 4, let's say, then this will be allocated at compile time. That means one memory address will be x, in which we are storing 4, and all this will be done at compile time. But whenever you make an object, all that is made at runtime. Okay? So, as we wrote student s1 equal to new student, now you must have understood that we have a stack memory, we have a heap memory, in this heap memory, an object will be formed, and here will be an s1, which will be pointing to this object. All this is done. Now, this scenario has been made. But what I said, in the student class, the student class we saw above, where is the student class? Here it is. Now, in student classes, there are 4 fields. Name, age, roll number, college. Now, I don't have to give any value. So, how will I give value? So, you understand that in this object, 4 things will come. Let's say, name will come, age will come, roll number will come, and college will come. I am writing small. But how will we give values to this? What will be its syntax? So, its syntax is something like this. I write something like this. S1 dot. We call this dot operator. S1. name equal to, now whatever name I want to keep, let's say Aditya. Similarly, S1. age, whatever age I want to keep, let's say 28. S1. roll number, let's say 101. S1. college, let's say IIT Guwahati. This is what we kept above, this is what we kept below. Something like this. So, this is how I add values in the fields of this object using s1. name,. age,. rollumber,. college,. operator. So, basically, if I tell you the work that I did in chunks, then first of all, I made a class named student, then I made the object of that class in this way, and I stored values in that object. So, this will be done one after the other. So, first of all, I made a class named class student, I would have made its object using this line. So, it would have come after this. And then, I would have stored the value in that object. Something like this. And it would have come after this too. Okay. So, first this. Then this. And then this. Because I have written it separately here. So, I have told you. Okay. Very good. Now, if I have to make another student. I have made a blueprint once. Now, how many students can I make of this? Like in real life too. The abstract term you have is student. How many students can be of that too? Arun is also a student. Varun is also a student. Aditya is also a student. Etc. Etc. So, similarly, I can also make this class's infinite student. Obviously, what will I do next time? Like the class was, next time I will write student s2, made a new reference variable, equal to new student. I wrote the same. Okay. What will happen here again? Again I am saying allocate memory in one more heap. So now basically our heap will look like this. So our heap will have two memories. One for S1 and one for S2. And our stack space this stack space also has two reference variables. S1, S2. S1 points to And s2 points to this. It has its own name, age, roll number, and college. Now I can give values to s2 as well. How? In the same way. s2. name, s2. age, s2. roll number. Something like this. Let's say I gave Rohit. Here I gave 28. Here, I gave the roll number, let's say, 102, in the same way. I can also give such values. Okay? So, I hope, you must be able to understand things, that how I gave values to the first one, and how I gave values to S2. Now, I will write this together, first by combining them. So, look, I have written the entire code together. First of all, I have class student. Then I made two students. S1 and S2. Now I wrote S1 values. S1. name, S1. age, S1. olumber and S1. college. And then I filled values in S2. So what did I tell you? I told you that we have two types of memory. We have heap memory in which two objects are formed. S1 and S2 are reference variables. In your stack, it must be pointing to S1 and S2. We understood all this by looking at it. And you must be understanding what dot operator does. Whenever we write S1 dot name, we first go to S1. Then I say dot means I came to this object. And I want to access its name field here. Or I want to put any value in the name field. So dot operator takes me there. S1 is just a reference variable, it stores an address. The starting address of this object. This dot operator is the only way to reach there. As soon as I reach there, I have 4 fields, name, age, role number and college. I can access anything from these 4. So, after writing S1 dot, I can write anything further. Whether I write name, whether I write age, whether I write role number or college. Okay. I can access anything because I have reached this object now. Whose job was to reach this object? This dot operator. Remember. Okay. So, I can do all this. And one more thing. Till now, we have seen that how we will assign name, age, role number and college. If I have to print them or access them, how will I do it? It is still simple. Let's say if I have system dot out dot println. And if I want to print the name of this S1, I will write S1. name in the bracket. So I can access it through the dot operator. If I want to put a value in it, I will put S1. name equal to Aditya. If I want to print the value, I will print S1. name. Just like I do with normal variables. Now let's move ahead. But before moving ahead, I want to tell you some things first. I want to tell you about naming convention. See, it's a very small thing. First of all, it understands what convention is. Convention is not a rule. It's a good practice. That we do this. In Java, we name things. So, first of all, all the variables we have. Now, whether they are primitive variables. Like int, float, care, boolean etc. Or our reference variables. Reference variables. Whatever we made S1, S2 above, all that. Whenever we give them a name, we always give them a name in the camel case. What is a camel case? Nothing. If you are giving a name to it, like let's say first name, last name, there is a name for a variable. So you keep it like this, that the first letter of the first word is small. And after that, the second word, third word, whatever will come, its first letter is capital. And there is no gap between the two. First name. Now let's say I have to write last name. So L is small, N is big. Last name. Something like this. If there were three words, like first name of student. Let's say there are four of them. So I will keep it like this. First F will be small. Then N capital will come. Then O will also come. Then S will also come. Something like this. First name of student. Okay. First should be small. After that all should be capital. We call this camel case. We keep all the variables name in camel case. Okay. Very good. Second thing. All our classes. Which we had made class on top. Student. We will keep its name in capital letter. Okay. Starting with. Capital letter. Like we had kept on top. What we had kept its name. Student. So it was S capital. If we make let's say human, then it would also be S capital. If we make chair a class, then it would be C capital. So it always starts with capital letter. Because it represents an abstract idea, we always like to keep it in caps. Okay. And one last thing, all our functions are also represented through camel case. Camel case. This is just a naming convention. It's not like you can start a class with a small name and put a student instead of a student and get an error. Or you can put a variable with a capital F instead of a first name and get an error. This is not a compiler error. This is just a good coding practice. We don't always put the name of a variable in the camel case. We keep the name of the classes in capital and the function in camel case. Whenever you debug a code in Java, you will see that these conventions are being followed. That's why you should also follow these conventions. I just wanted to tell you this. Now let's move on. So, till now we talked about how we represented a real-world student in programming using class and object. So what did I do? I made a class called student. And filled all its characteristics in it. But did I represent a complete real world student? Now think carefully. Does a student have only its characteristics? Which we have represented till now. Right? The identity that we had said. Its name, age, number. Does a real world object have these things? Think about it. The answer is no. An object has other than characteristics, it also has behaviors. Okay, what is behavior? Look, I will give you an example of this and explain. Let's say I have a real world object, animal. Animal is a real life object. Now again, it also has some characteristics. What would be the characteristics? Like? There must be a type of animal. If we say type, then what kind of animal is it? Is it a carnivore, herbivore, etc. How many legs does it have? So let's say number of legs is one of its characteristics. There must be other characteristics like if it is a wild animal or domesticated. There are many kinds of characteristics. But apart from its characteristics, there can also be a behavior of a variable. For example, an animal can make a sound. It can make sound. Apart from that, an animal can eat, can run, etc. So, an animal can do a lot of things. Basically, it can do something. An animal can do something. So, we have called its do something as behaviors. So, again, it can be an animal's characteristics or behavior. But right now, if I were to make a class of an animal, let's say class animal, I was only representing its characteristics using variables. I would make a string type, I would make an int number of legs, something like this. I was just using variables to represent its characteristics. But I haven't represented its behavior yet. Now if I want to mimic a real world, then don't you think I should also represent its behaviors? Because a behavior is also a part of an animal. This real world animal, like its characteristics are a part of it, similarly its behavior is also a part of it, its intrinsic part of it. For example, the animal that eats, runs, makes sounds, it does that by itself. It works with its body. It doesn't need any external thing to make sounds, to eat, to run. No one is helping it from outside. It makes sounds and eats from inside. So this is its intrinsic nature. This behavior is also its intrinsic nature. So I should also represent this. So basically, what does it mean? If I make a class animal, So I will add all these things in it. String, type of animal, int, number of legs, etc. But how do I represent its behavior? Have we read anything in programming that mimics its behavior? How did we mimic its characteristics? Using variables. You know this, right? See, this is string, int, a variable. Using this, we have represented the characteristics. Now, how to do the behavior? What is there in our programming? See, behavior says, what can that object do? And we have read something in programming too, functions. And what are functions? That does something, right? So, they can represent behavior very well. Behavior is also that, what can that object do? And functions also actually does something, right? So, through functions, we can can represent the behavior of objects. So what can I do? I can make functions of make sound, eat, and run. For example, I will make functions of in, let's say void, make sound. Make sound. Something like this. And I will define it here. Let's say, like this. Now the body will come inside this. Now see, I made this function inside this class only. Why? Because what did I say? This behavior is also the intrinsic nature of this animal. So to represent its intrinsic nature, Now if I want to make the function of eat, I can make another function void eat. We will see later that it depends on logic that it will be of void type or any other type. But to represent it, I told you that I can make void eat like this. So now exactly I have mimicked a real world scenario because I have represented the characteristics of an object and the behavior of that object Both of them are represented in their programming in an object. Now, the animal has its characters and its behaviors. Now, if I apply the same thing on a student, then what I said is that a real life student, if he is a student, he will have his own characteristics. Characters can also be called data. Let's say we talked about name, age, role number, college, etc. But there must be some behavior of it. Let's say it has a behavior of mark attendance. Every student can mark their own attendance. So, mark attendance is a behavior. I don't give it here. It's just a behavior. I represent it as functions in programming. Now, how will I make it? Same, I will make class student. Something like this. All my variables will come up. You know, I have done all this. String, name, int age, intro number, etc. String college They will come up And my behaviors will come down So I can write here let's say void Mark attendance I have made it a function Something like this And here will come anything I want to do Body of the function So see again I have represented characteristics using variable I have represented behavior using functions Okay So again what is it representing? It is representing its characters Or data And what will it represent? It will represent the behavior of any object. Okay? Now see, how can I call these functions? Now when I write code, that I have made an object, sorry, let's make it this way. How do I make an object? Student s1 equal to new student. And if I want to fetch the name of s1, I can do s1. name. Now if I want to call this mark attendance, I will write the same syntax. Dot operator and our mark attendance And you know whenever we call a function What do we do? We give parenthesis and put semicolon That we have called a function We have already studied this So after doing s1. markAttendance What did I do? I called this function of s1 Which lies inside this object Right? That means now our s1 object will be created in memory In that there are not just these four variables Name, age, roll number and your college Apart from this there is also function Mark attendance. Remember this. Okay. Now, these entire objects are made not only from characteristics but also from behavior. And I have told you how we will call. Same using dot operator. Okay. Now I have one more question from you. When we used to write a print function, let's say void print, that I have to print the student's detail on the screen. So what I had to do was, I had to pass everything here, right? Its name, age, role number, different, different. But now I can pass the whole student directly. Student S. So let's say what void print will do? It will take a complete student S and it will print it. To print, you know we don't have to do anything here. Here we will write system.out.println and print s.name, print s.h, etc. I will print it like this, no problem. So basically to print, I need to pass the whole student, not its individual variables. Very good. But the question here is, do you still think that this print function We should write this outside. What if I write this function inside this class? What if I make the print function an identity of the student? Now you will say that we were focusing on the real world scenario. And in the real world, there is no such thing as print. Print is not a behavior. Student's print behavior is not real. Right? Absolutely correct. But you think like this, the programming in which we had to mimic the real world, we have already done that. But now when we have to write code, any such helpful functions, any such helpful data, which will make our code easy to put in an object, we will also put that in it. Now basically, after mimicking the real world scenario, you have to think like this, that we have made a real student a digital student. But in a digital student, there can be more such behaviors which will not be there in a real student. For example, in a real student, There won't be any behavior named print. Because obviously you don't print yourself in real life. But our digital student can print himself. So the function named print makes sense here. So the behavior named print is the version of this digital student. Not of this real student. So we can also put this in here. So what if I put this print in here as well. So the benefit of this would be that if I have made my class student. In which all the other things come. And here I have put void print. Now you think, do I need to take any parameter? Absolutely not. I don't need to take any parameter. Because all the parameters are inside this class. I can use them. Now we will see how in the code. And here I will just print system.order.println. I will directly print name. Your age, your role number, your college, etc. Simple. Basically, I don't need to take any parameters because this print function has become a part of this student. It has become an identity of this student. Now, I can say that each student can print itself. Every student can print themselves. This is a part of this digital student. Again, I told you that in the real world, there can't be any function called print. But in a digital student, we need print. So, we put it in this. Now you have to think that in programming it is not necessary that we mimic our real objects as it is in our digital objects. Right? Some objects can be physical and some non-physical. For example, an animal is a physical object that exists. We can represent it digitally. A student is a physical object that exists. We can represent it digitally. But think about it. Bank account. Bank account, does it exist in the real world? I mean, bank account is not a physical thing. But we can also represent it using a class. But why will we make a class of bank account? Look, if we make a class of bank account, we will make it because bank account is also a complicated object. It can have multiple things. Like your account number, your IFSC code, your... the name of the branch, etc. etc. There can be a lot of things. So we can also represent it through a class. So now you understood. Any such thing, which is complicated to represent in one variable, which you can't represent in a simple int, char, float, there are multiple things in it. It's like a box. What you will do is, you will make an object, like we made a bank account. So in this, its account number, its IFSC, its Branch etc etc We could store And in this We could write its functions Or its behavior Okay So Mimicking doesn't mean that you will only pick up physical identities. You can pick up non-physical identities too. Like your bank account. Because bank accounts also have multiple characteristics. Right? Similarly, consider location. Now, in location, if I write this with a pen. Yes. There can be many characteristics in location too. Like its longitude, latitude, etc. Right? So, to represent that too, I will have to make a class in programming. So any real life object, whether it's physical or non-physical, which is a little complicated, meaning it can't be represented with one variable. In programming, what do I do for that? I make a class. Okay? I hope you have understood that everything in Java, We can start thinking like objects in the object oriented paradigm. Now let's quickly look at whatever we have read in the code. It will be fun. So again, my boilerplate code is ready here. What I had to do is, first of all, I make my student class. And first of all, where will we write that? Will we make our student class inside this? Class, student? Absolutely not. We will not make it outside this main function. We will not make it outside this main function. We will make it outside this. We will make it outside this code block. If you see, this is also a class. We will come to this later. Now we will make outside of this, let's say class student. So I made a student class. Now see what was in our student class. First of all, there were some characteristics. So let's see that. So one string became, let's say, name. Let's keep it like this. One became our age. One became our roll number. And apart from this, what did we keep? Let's write it completely. Let's roll number. Apart from this, what did we say? String college. Something like this. Right? Now let's do one thing, here will be a string, okay. Let's represent some of its behaviors. So let's say, let's represent one, void mark attendance. Okay, something like this. And I don't do anything, here I just write system.out.println, that attendance marked. Let's say, attendance marked. Now who has marked attendance? So I want the student's name to come, so attendance marked by, Here I can write plus and name. Now what is this name? This is the name of this student. Basically the name of the object is the same name. Since the mark attendance is a part of the student, he will know what his name is. So he can simply print the name. He doesn't need to take the name in his parameter. Because he has access to the name. Right? Okay. Now let's write another function void print. Okay? Print. He will not do anything. He will print all these values. So basically system.out.println println and it will print comma separated. So, I will quickly write that. So, I have printed name, age, role and college. Very good. Now I have to make the objects of this student class. Where will I make that? I will make that again in main. Because what I told you, look, in programming, our flow of control only executes the main function. Right? Whatever is written in main will be executed. Now if I actually write student s1 equal to new student, means I will make the object named student in main, then only some memory will be allocated for this class. Otherwise, I have just such a code written, so no memory will be allocated for it. Okay? So what will I do? Here I will Let's say s1 equal to new student And let's say I make one more s2 equal to new student Now if I want to give value to s1. name And let's say I gave it Aditya which we talked about S1. age let's say I gave it 28 S1. roll number I have given this as 101. See, the ID itself represents the VS code. That what I am going to put. As soon as I wrote a dot, it put everything. That what I can put. So I can write college here. Why? Because it knows what is in that object, so it brings it to me and gives it. So let's say I have given here. IIT Guwahati. Okay, very good. Now what can I do in this way? I can put all these things in one more variable s2. So I will write here s2. name, s2. age, s2. number and s2. college. Let's change the name to Rohit and change 101 to 102. Now let's see what I do next. I call the mark attendance function of S1. I call the mark attendance function of S2. Very good. Now I ask S1 to print itself and ask S2 to print itself. So everything should be printed on the screen. So let's run it quickly. See this. Attendance marked by Aditya. Attendance marked by Rohit. Aditya's details and Rohit's details. Everything is printed. Very good. Okay. So, I hope you understood how we made a class, how we made both its objects and how we stored its values and called their behaviors. Okay. So, you know all this. All this represents characteristics and data and all this represents behavior. Now I was telling you to ignore this boilerplate code. Now you know that this code is a function and our flow of control always executes whatever is written in the main function. Now this function's return type is void and it takes the parameters string args. We haven't seen why it takes it but we know that it does. And we haven't understood yet what is public and static. But we have understood a lot of things. We will see this later. Now let's go above this too. Now look carefully, we have written above public class demo. You ignore this public. You just see class demo. This is also a demo class. And what is the name of this class? The name of this file is demo.java. Why is this? We will see this later too. But you can understand that the class demo we have written is also a class. And in this class, we have written a main function. See, public static void main. I have just closed it. You can close this code block. Okay. So, the main function I have written, it comes in this class. Because, like, This is not at all similar to how I made a student class and wrote some functions in it. Similarly, I made a demo class in which I wrote a main function. And we have to do this. I mean, we have to make a class in which we have to write a main function. We can write some code in it. That means, our Java is purely object-oriented programming. Almost. Now, why am I saying almost? I'll tell you later. So, I can write here, Java is almost complete object-oriented programming. Java is almost complete object oriented programming Why I am saying almost, I will tell you later Because if you want to write a little bit of code in Java Then first you have to make a class In that you have to write a main function And in that only you can write your rest of the code If I had to make my own class Then I made it here Outside this class Outside my demo class But where did I have to declare its object Inside the main function So these small things that are left, you will understand that why we made the main class and What is public static? Why did we write a string argument here? You will understand all these things slowly. But I hope you have understood a lot of things. If you want to write some code in Java, then you will have to make at least one class first. That means without any classes, you were always making a class in the boilerplate. And a function was written in it, its name was void main. So I hope you have understood all these things. This was just an introduction of objects and classes. In the next lecture, we will understand it in a little more depth. But if you understood it well, then you understand, you understood the core idea, which is the idea of the principle of oops. Okay. Rest, we will meet you in the next lecture. Till then, thank you so much.
