Free YouTube Transcribe

Video transcript

OOP 1 | Introduction & Concepts - Classes, Objects, Constructors, Keywords

Kunal Kushwaha · 16,409 words · 75 min read

Want to search this transcript, jump the video from any line, or download it as TXT, SRT, or VTT?

Open in the transcript tool

Full transcript

Teaser

0:00In the next few videos we'll talk about

0:01like packages, static, non-static,

0:03singleton class. We'll talk about

0:05properties, polymorphism, overriding,

0:07constructor, and special methods,

0:08inheritance types, encapsulation,

0:10abstraction, access control, object

0:12class, object cleaning, interface,

0:13abstract abstract classes, interfaces,

0:16generics, exception handling, collection

0:18framework, lambda expression, enums,

0:19file handling, and fast input output.

0:22All of these things will be covered.

0:24Nice.

0:29Everyone, welcome back to another video

Introduction

0:31and we are starting with a new topic.

0:34So, we are doing the complete data

0:36structure algorithm bootcamp and this is

0:37a series that we're starting on now,

0:40object-oriented programming, right?

0:42So, the next few videos, including this

0:44one, will be on object-oriented

0:45programming. If you haven't already

0:46subscribed, make sure you do so and

0:48let's get started.

0:50So,

0:52I'm going to teach you

0:54literally like everything about it,

0:56okay? So, I will teach you like what is

0:59object-oriented programming, what are

1:00the various principles specific to Java

1:02stuff as well.

1:04Um

1:05And you know,

1:07we don't just because

1:09content on this you can find in so many

1:11books and stuff. So, what makes this

1:13course special? I will actually teach

1:15you how to think. I will teach you from

1:16a beginner's point of view. I will teach

1:18you how to master object-oriented

1:20programming

1:22in a simple, easy-to-understand

1:23language. I will teach you what doubts

1:25you can have because in object-oriented

1:26programming people can have so many

1:28doubts. So, I will tell you this is the

1:30doubt you may be having and we'll then

1:31look into it. So,

1:33it's going to be great. It's going to be

1:35a really top-notch, amazing playlist.

1:37So, make sure you like, share, and

1:38subscribe and let's get started.

1:40Okay. So,

1:44object object-oriented programming,

1:45okay.

1:47Uh before that, I want to mention

1:49about the playlist itself. So,

Notes

1:53the notes will be provided.

1:54Um

1:56text notes

1:57So, like text files

1:59in GitHub repo.

2:04Okay? So, for notes you'll be having

2:06text text files in the GitHub repo and

2:10this particular whiteboard stuff um

2:14this file

2:15is just like rough work.

2:19Okay? If there's a concept that requires

2:21for me to use the whiteboard, I will use

2:23it. So, main main notes can be found

2:25over here, like detailed notes. You

2:28don't have to make notes yourself. You

2:30can use the ones that I provide, okay?

2:32Focus on the lecture itself, don't make

2:33notes. Do not make notes because I will

2:36be providing those, okay? So, don't no

2:37need to make notes.

2:39So,

2:40this playlist is I'm super excited for

2:42this because it's going to be great.

2:43Like if you don't know anything about

2:44it, even if you know about

2:45object-oriented programming, I will

2:47literally tell you everything and will

2:49also cover like in detail explanations

2:51of why things are working the day the

2:52way they are. In Java also, inheritance

2:54and stuff and interfaces and abstract

2:57classes and singleton class and generics

2:59also, collections framework like and all

3:01these other things we'll be covering.

3:04Cool.

3:05All right, so let's get started. So,

OOP Introduction

3:09over here I'll also teach you about like

3:10what are packages and stuff that will

3:11also come.

3:13So, we are basically going to cover like

3:15the introduction about classes and

3:17objects, the keywords like new, things

3:19like constructors,

3:21other keywords like this and final and

3:23finalize stuff like, you know,

3:24destructors and stuff, wrapper classes,

3:27packages, static, non-static stuff,

3:29singleton class, the properties of

3:31object-oriented programming like

3:32polymorphism, overloading, overriding,

3:35inheritance,

3:36the types of inheritance, encapsulation,

3:38abstraction, access control, object

3:41class, object cloning, interfaces,

3:43abstract classes, generics, exception

3:44exception handling, collections

3:46framework, lambda expression, enums,

3:49later on we'll also be doing like file

3:50handling, fast input output, and all

3:52these other things. This is the entire

3:53syllabus for object-oriented

3:54programming. All right.

3:56Cool. But, let's start with like the

3:58basic stuff. I'm just going to create

4:00another package. I'll tell you what

4:01packages are in detail later on.

4:05Okay?

4:06Introduction.

4:08Okay. So, what are we doing? Um

4:12okay, forget about object-oriented

4:13programming. So, just forget about it

4:14right now. Let me give you a problem

4:16statement. You are sitting in a class

4:18and someone let's say asks you, "Hey,

4:21your teacher asks you, "Hey, please

4:23create a data type. I want to want you

4:26to make an application that stores,

4:28let's say,

4:29five roll numbers."

4:31You're like, "Okay, ma'am, no problem. I

4:33can just write it in Java. I can say

4:36say main.java or something."

4:40Okay, public static void main.

4:43So, your teacher asked you,

4:45"Hey, Kunal,

4:46please create some sort of a data type

4:49or something that stores five roll

4:51numbers."

4:52You're like, "Okay, no problem. Roll

4:54numbers are integers, so I can just

4:58you know, I can just

4:59create an array."

5:06Something like this.

5:08Okay, or you can say new int

5:10five.

5:11That's it. No problem.

5:13Now your teacher is very impressed. Now

5:15your teacher says, "Hey,

5:17can you please uh

5:19create some sort of a data structure or

5:20something that stores five names?"

5:23Right? So,

5:27you're like, "Okay, store five names of

5:29let's say your students or something."

5:32So, you can be like, "Okay, I want to

5:34store five names, so I can create a

5:36array of an array of string, no

5:39problem."

5:40String names is equal to new string

5:43five, no problem.

5:46Now your teacher asks you, "Hey, Kunal,

5:50please create some sort of a data type

5:53to store

5:54what?

5:56Data of five students."

5:59You get confused. You're like, "Data of

6:01five students?

6:02What what do you want me to include in

6:03that data?" Then your teacher says,

6:05"Good question." So, your teacher says,

6:07"Include the roll number, marks, and the

6:11name."

6:12Roll number, marks, and name.

6:15You're like,

6:16"Okay, no no problem."

6:19So, you can say something like, "Okay,

6:20if you want the data of students, so you

6:22can say data of

6:24five students.

6:26Every single student contains, let's

6:29say,

6:29um

6:32roll number, name, and marks." You're

6:35like, "Okay, how can we do this?" So,

6:37you can be like, "Okay, let me just um

6:41create, let's say, three arrays.

6:43Roll number,

6:46right?

6:47Um string,

6:54okay? Something like this.

6:56And marks can be float or something.

7:03Okay?

7:04New

7:05float, something like that.

7:07Now your teacher is like,

7:09"You have just created different

7:12different data types for every single

7:14property, okay?" So, your teacher is

7:16like, "Okay, this is fine, but

7:18I want it to be something like this. In

7:20just one single line, every single

7:22element, for example, in this case,

7:24every single element is what?

7:26Every single element is a name. Here

7:28every single element is an integer.

7:30Similarly, I want some sort of a data

7:31structure in which every single element

7:34contains these three things."

7:39This is where classes and

7:40object-oriented programming comes in.

7:44Okay? So, what is a class?

Java Classes

7:47What is a class now?

7:49A class, again, don't write notes. I

7:51will be providing notes. A class is a

7:53named group of properties and functions.

7:58A named group of properties and

8:01functions. These three properties that I

8:03have right now, roll number, name, and

8:05marks. If I want to combine this into a

8:07single entity,

8:09I can do that via classes.

8:15By convention, class starts with a

8:18capital letter, okay? So, your teacher

8:20will be like, "Okay, I don't need these

8:21three things. I need you to give me

8:25something like this, like some other

8:27data type called student, your own data

8:29type. You made it."

8:32Students is equal to new

8:34student.

8:36Five students. Every student, like every

8:39single element in this array,

8:41should contain these three items.

8:44So, one more simple definition can be if

8:46you want to create your own data type,

8:48how can you do it? Using classes.

8:52We'll talk more about later like when we

8:54do encapsulation and stuff, I will tell

8:56you in detail what all these things are

8:57and stuff, but please focus on basics

8:59right now. We'll cover everything, okay?

9:01Don't worry.

9:03So, you're like, "Okay, I need to put

9:04this inside a class."

9:07Something like that. Like, "Okay."

9:10And be like, "I can basically then

9:13create a class. This is how you create a

9:15class. This is the syntax for it. Class,

9:19class name."

9:21That's it.

9:24That is it.

9:27As simple as that.

9:30Let's say you want to create one student

9:32first. So, I can say something like

9:34student Kunal is equal to new student.

9:38I'll tell you more this new what about

9:40this new thing is and what all these

9:41things are. Let's just forget about it

9:43right now, okay? No worries.

9:44Student Kunal. Now this Kunal is going

9:46to have this variable Kunal.

9:50This reference variable is of type

9:52student, our own data type that we

9:54created using a class.

9:55And it consists of what? It consists of

10:01three properties.

10:03Don't worry about the static stuff and

10:04stuff. I'll I'll cover that in detail.

10:06But right now, just focus on what we are

10:07doing, not how we are doing it. Things

10:09we'll cover in detail.

10:10Okay? So, here it's basically saying

10:13that uh

10:15it contains three properties, roll

10:17number, name, and marks. So, Kunal will

10:18have a roll number, name, and marks. How

10:20to access that, how to modify that, and

10:22everything we'll cover later on.

10:25Okay? So, this is basically what we mean

10:27by classes.

10:29Now, let's talk about this reference

10:31variable, object, and how this works

10:32internally, how to update and stuff, and

Example of a Class

10:35all these other Okay. So, this is

10:36basically a template, as you can see.

10:39Okay? So, a class is like a It's like a

10:42logical construct. For example,

10:44uh this is defining what? Class is what?

10:46Simply I mentioned to you, it's just a

10:48name group of properties and functions.

10:50You're You can add functions here as

10:51well. These are known as methods. We'll

10:53talk more about that later. For example,

10:54create a method that gives you the GPA

10:56of a student. Create a function like

10:59that. Okay?

11:00So,

11:02let's look into it a bit more detail in

11:04a bit more detail.

11:05A name group of properties and

11:07functions, okay? What else What are some

11:09real-life examples of classes?

11:12Car.

11:13A car can be a class.

11:16Okay? And using that class, using that

11:19template, so many companies are creating

11:21their classes their cars.

11:24There's Let's say a BMW. There's Audi.

11:27There's Tesla or whatever. Okay, so BMW,

11:30let's say Audi, and let's say Ferrari.

11:32Okay? So, these are the three classes.

11:35Let me bring up my whiteboard for the

11:36Okay, so I know that class is a name

11:38group of properties and functions. So, a

11:40car class, if this is a class of a car,

11:43okay? A class is just a name group of

11:45properties and functions. Let's say I'm

11:46just visualizing it with a box. And what

11:49all properties does the car contain?

11:51A car contains an engine.

11:54A car contains the price.

11:56And the car contains the number of

11:57seats, for example.

11:59Okay? Now, this is a template. This is

12:01like the general stuff that every car

12:03needs to have.

12:04Using this template, many companies have

12:07created their cars.

12:09Okay? So, Maruti created their own car.

12:13Okay? Maruti.

12:17Ferrari created their own car car.

12:21Using this template. They're like, "Hey,

12:23this class is a template like uh some

12:25properties and functions that every car

12:27must have. And please, you follow this

12:29to make your own car."

12:31Okay? Here we have something like Audi.

12:37Okay?

12:38Very simple.

12:40Now, what are these boxes that I'm

12:41talking about? Maruti,

12:44Ferrari, and Audi, they follow the same

12:47procedure.

12:48They have these properties, but the

12:51value of properties are different.

12:54They all have engines, but this engine

12:56is let's say small engine.

13:00This one is let's say

13:03big engine.

13:06Okay, or I can say something like

13:08mm

13:09Let's say I can say this is like uh

13:12petrol engine.

13:15This is like

13:16diesel engine.

13:19And this is let's say electric engine.

13:24So, all of these three cars have the

13:26same property, engine, but the value of

13:29that property is different.

13:33Okay? Price, for example, 1 lakh.

13:382 crore.

13:411 crore.

13:43Seats, let's say

13:45over here, two seats.

13:47Four seats.

13:49Four seats.

13:53Okay? So, using this template,

13:56they created their classes.

14:00No, they created their like cars, not

14:03classes, cars. They created their cars.

14:06This is sort of like the template. You

14:07can think of it like as a piece of paper

14:09someone gave them.

14:10Okay, it's just like a standard for a

14:12car, right? Okay.

14:14Now,

14:15please try to think it in the real world

14:17sense.

14:19Does this thing like really exist

14:21physically?

14:24Like if you're saying like obviously

14:25cars exist, but this template stuff is

14:29actually like a sort of like a rule sort

14:31of a thing, right? These are the things

14:33that exist physically.

14:35When you ask ask someone, "Please show

14:38me an example for car." Obviously,

14:41they're going to show you a car.

14:44Right?

14:46They're going to show you something

14:47physical.

14:48One more class example can be what? That

14:51we all share in common. Human.

14:54Human class.

14:55So, human class Humans have some rules.

14:57Two arms Like by default, two arms, two

15:00legs, two eyes, hair, mouth, nose, ears.

15:04By default, these are the rules.

15:06Right?

15:09So, some people have like big eyes. Some

15:11people have smaller eyes. Some people

15:12have a large mouth. Some people have a

15:13big mouth. Some people have, you know,

15:15ears different shape. Uh some people

15:17have long hair. Some people have short

15:19hair. Right? Some people have longer

15:21nails. Some people have shorter nails.

15:23So, human class is also like a group of

15:25properties and functions. These are all

15:27the properties that humans should have.

15:30But what actually Who are humans? We. We

15:33are the ones who are roaming the earth.

15:34We have all these properties, but

15:36different people may have the different

15:37value of these properties.

15:40Are you able to understand the

15:41difference between the class and what

15:44represents the class?

Java Objects

15:47These things

15:49These things

15:51are known as what? Objects.

15:55So, a class is what? A class is a

15:57template of an object.

15:59And what is an object? An object is an

16:01instance of a class.

16:05Okay? So, as we mentioned about the data

16:07type, this can be a separate data type

16:09called a class data type. And classes

16:12help us in defining this data type.

16:16Classes help us in defining this data

16:19type.

16:21So, when you create an object of a

16:22class, you're actually creating an

16:23instance of that class.

16:26What is this instance? Instance means

16:29a physical stuff of that class.

16:32When babies are born, they are instance

16:34of the human class. Human class is a

16:36rule. God has provided this as a rule.

16:38Like, okay, this is the human class.

16:39This is a rule. It does not like It is

16:40not a physical thing. It's a rule. If

16:42you want to make a physical thing, you

16:44have to have a baby. Right? Same thing

16:46for cars. This is a rule for cars.

16:48Engine, price, and seats should be there

16:50minimum.

16:52And in order to make it a reality, the

16:53physical stuff that is like follow this

16:55instance of the follow this class

16:58template and create an instance out of

17:00it. That is known as an object.

17:03So, object is what?

Class vs Object

17:05So, class is what? I will explain it in

17:07a very nice way. Class is just a logical

17:09construct.

17:11It does not like exist exist physically.

17:13Logical construct.

17:16And object is what?

17:19Physical reality.

17:25Reality. This is the thing that is

17:28actually occupying space in the memory.

17:32Actually is something physically

17:34present. Occupies space in memory.

17:37Memory, RAM, or whatever you want to

17:39make. We have been dealing with objects

17:41since first lecture.

17:43We created objects of what?

17:45Arrays, array list, and stuff. All of

17:47those were objects only.

17:49Right?

17:51Okay? So, class is a template of an

17:53object. Object is an instance of a

17:54class. Class creates a data type that

17:56you can use to create objects. When you

17:57declare an object of a class, you're

17:59creating an instance of that class. The

18:00class is just a logical construct. And

18:03the object is the one that actually has

18:04some physical reality, that is occupy

18:06some space in the memory.

18:10Okay?

18:12That is the difference between classes

18:13and objects.

18:16Okay?

Properties of Object

18:18Now, objects are categorized by like

18:21It's It's like they have

18:23three essential properties. Okay? State

18:25of the object. I am not writing this

18:27down because I'll be sharing my notes.

18:29State of the object, identity of the

18:31object, and behavior of the object. So,

18:33what is the state of the object?

18:35State of the object means its value

18:37from its data type.

18:39That's sort of like the state of it.

18:41Identity of the object means whether one

18:43object is like different from other.

18:45Okay, it's It can be useful to think as

18:47identity as like where the value is

18:50stored in the memory. Right? So, for

18:51example, you know, you already know

18:53this. I don't have to repeat it.

18:57Stack memory, heap memory.

19:00Object.

19:01Two Two reference variables pointing to

19:03the same object. Already covered this

19:05thing.

19:06If you make a change via this reference

19:07variable, original object will be

19:09modified. Hence, the change will be

19:10visible via the second reference

19:12variable as well. If they're pointing to

19:13the same. Please watch the previous

19:15videos. Strings, arrays, everything is

19:17covered in that. Now, I'm telling you

19:20why it's happening. In previous videos,

19:22I told you what is happening. I also

19:24told you why it's happening. But now,

19:25I'm moving into more more details. Okay?

19:29You know, I remember in the previous

19:30videos, I used to say we will cover in

19:31object oriented programming. Now, it's

19:32the time for that.

19:34Okay? So, this is like the identity. And

19:37the behavior of the object is the effect

19:40of the data type operations. For

19:42example, we'll be creating, let's say,

19:44functions and stuff in in your objects

19:46as well. So, you can say create a, you

19:48know, um

19:50Every human should have a function

19:51called greeting.

19:53So, every human, when you call the

19:55function greeting, they will greet

19:57someone. Hi, hello, something. We'll

19:59cover that later.

20:01Okay? But, this is as simple as it can

20:03get.

20:05Okay?

20:06All right. So, why what we are doing we

20:09just covered, what are classes, objects,

20:11and stuff, and like things like that.

20:14Right? Objects are stored in the heap

20:16memory, and um

20:20the reference variables are stored in

20:22the stack memory. We'll cover more into

20:24more details about that.

20:27All right. Um

Objects Introduction

20:29now, let's see how we can make it a

20:30reality. So, how do we create objects

20:32and stuff and assign values, change

20:34values, then we'll add functions into

20:36it, then we'll cover all the other Let's

20:38take our student example. You're like,

20:39"Okay, Kunal, the class is created. A

20:42class student is created like this."

20:44So, logical construct is being created.

20:47Okay? This is a student class.

20:50And it has some properties like uh

20:52the roll number.

20:54Right? It has a name.

20:56And it has marks.

20:59So, something we have to talk about

21:00right now is how do I actually make sure

21:03that I create like various various

21:05objects like one student Kunal, another

21:07student Rahul, third student will be

21:10like um something like

21:14Anaïs, for example. Right?

21:17So, Kunal's roll number can be roll

21:19number 14, name will be Kunal, and marks

21:23will be 87.9%

21:25or something like that. Okay? Rahul's

21:27roll number can be something like uh 28,

21:30name will be Rahul, marks will be

21:33something like 92.7.

21:36Uh Anaïs's roll number will be something

21:38like name will be Anaïs, roll number

21:39will be

21:4133 or something, random stuff, and marks

21:43will be 90 95.8

21:47or 89 or something. Okay? Random stuff

21:49I've just put over there. But, the

21:51question is

21:53how do I make sure that I use this

21:54template of the class to create these

21:57objects?

21:59Okay? Before that, I need to actually

22:01understand how do I access these things?

22:06These class properties are something

22:08that everyone will be having.

22:10We know that any object of the student

22:13class will be having these three

22:15properties, roll number, name, and

22:17marks.

22:19Okay?

22:20And we can obviously

22:25How do we do this thing? Right? How do

22:27we create an object? So,

22:30before that, yeah, I need to explain to

22:32you like let's say assume Assume the

22:34object is created. Okay? Don't worry

22:35about how to create it right now. Assume

22:36it's created. Let's say the this

22:38object's name is

22:40KK. This is name is RR. This name is or

22:44not KK RR. I can say this name is

22:48student one. This is a reference

22:49variable.

22:51Student two.

22:53Student

22:55three.

22:56Since we have already worked with

22:58classes before,

22:59this is nothing new.

23:02Okay? You have worked with arrays, array

23:03list, and stuff. So, over there, what

23:04were you doing? You were saying array

23:06list

23:09list is equal to new array list. This

23:12new keyword we talked about before.

23:14This is used to create objects. We'll

23:16cover more into like in this video now,

23:18we will cover in detail like the depth

23:21Okay?

23:22We will cover the depth of it, how it's

23:24working internally. But, forget about

23:26this right now. Don't need to worry

23:28about this right now. Right now, just

23:29assume these objects are created, and

23:31these are the reference variables of it.

23:32How do we access this thing? Okay? What

23:34is student one's roll number?

23:37What is student two's percentage? What

23:40is student three's name?

23:41How do we actually access this? We know

23:43these students are of what type? Classes

23:46are nothing but data types. So, what are

23:48these students' type? They're of type

23:50student. And student has three

23:52properties, roll number, name, and

23:53marks.

How to access instance variables?

23:55Then, how do we access the roll number

23:57of student one, two, three, and so on

23:58and so forth?

24:00Using the dot operator.

24:03Okay? So, the dot operator basically

24:06links the reference variable, like the

24:08object that the name that we have over

24:10here, student one, student two, and

24:11student three, with the name of like the

24:14instance variable.

24:16Okay? So, you know, basically like just

24:18the the object uh the object properties,

24:20this one. It's going to link

24:23this one

24:24the name of the instance variable, like

24:26the name of the uh instance variable,

24:29these ones. These are known as instance

24:32variables. What are instance variables?

24:34Variables inside the object, they are

24:36known as instance variables.

24:39Okay? So, this dot operator is going to

24:42link

24:43this

24:47with this. So, when I do something like

24:50dot operator

24:52How do we use this?

24:53When I say something like print

24:59student one dot

25:02roll number, it's actually linking

25:05this object reference variable with the

25:08instance variable.

25:10So, student one's roll number is what?

25:13This actual thing is going to give me

25:16answer 14.

25:19This is known as dot operator.

25:22Okay?

25:23Commonly, we call it a dot operator, but

25:26the form in the in the Java, it is

25:27categorized as like

25:29dot as a separator.

25:31Okay? That's like the formal definition.

25:33Okay? But, you can call it a dot

25:34operator as well.

25:36That is it.

25:38You can now use this to access any

25:41instance variable. So, these are known

25:42as instance variables. These These all

25:44variables are instance variables. All

25:46the variables that are inside your

25:48object, they are instance variables. The

25:50variable that actually defines an

25:52object, something we already covered,

25:54array list list is equal to new array

25:56list, string name is equal to Kunal. So,

25:58this list, this name, these are what?

26:01Reference variables. We've already

26:02covered reference variables before.

26:03We've been doing this since you know so

26:05so long.

26:06Right? Reference variables you already

26:07know.

26:08Okay?

26:09So, let's talk a little bit more about

26:11that, how we can work with this. Okay?

26:13So, these are instance variables,

26:14variables that are declared inside the

26:17class. But, it should be outside

26:20uh the method and the constructors and

26:21stuff that we'll talk about. Basically,

26:23just like this, outside the class.

26:25Oh, sorry. Ins- uh outside the method,

26:27but inside the class.

26:29Okay? So, I'll I'll talk more about

26:30instance variables in detail after

26:32giving the example. Okay? So, bear with

26:34me for a second. Okay? So, here I can

26:36say that I have my student class. I've

26:38just put it outside the you know main

26:39class or whatever. And it has just

26:46I actually did a

26:47not so good copy-pasting over here

26:49because every It will not be an array,

26:51right? It's It will will just be like

26:53int roll number.

26:55That's it. String name.

26:59And float marks.

27:03That's it.

27:04Okay? Uh

27:06because the array that we want to make

27:08is of the students collectively. This is

27:10a data type for every single student.

27:12Okay?

27:13For every single student like this.

27:16Every single student has a roll number,

27:18name, and marks.

27:20So, if I create a student like this

27:25Okay? How do we actually make sure that

27:28these things are like modified? So, for

27:30example, if I try to print it right now,

27:32if I say Kunal.roll

27:34number, try to print that. You can see

How to create objects?

27:36it's saying giving me an error.

27:38Variable Kunal might not have been

27:39initialized.

27:42Let's talk about this for a second. So,

27:44in order to create these objects, you

27:47have to use the new keyword.

27:49Okay? So, let's talk about the new

27:50keyword a little bit more. Again, it

27:51will be again available in the in the

27:54notes. So, don't worry about it.

27:56Everything will be you know given in the

27:57notes. So, you don't have to like really

28:00make notes or anything else. Okay? So,

28:02let's talk about the new operator. What

28:04does the new operator do? So, if you

28:05have a student class like this, if you

28:07have a class

28:09student

28:10It has three properties.

28:12Int roll number.

28:15String name.

28:16Now, we're talking about the how to

28:18actually create objects.

28:20And float marks.

28:24Okay? This is what you have. This is a

28:25class. How do I create objects from

28:28this? You know, this is a data type.

28:30Student. So, it's going to be obviously

28:32student over here like we do array list.

28:34We do array list list. You can give a

28:36name to the student. I can say student

28:37number one.

28:40This is my reference variable. Okay?

28:43Now, in order to create an object. Now,

28:44what is this thing?

28:46This is actually known as like you are

28:49declaring the reference to the object.

28:53Okay? So, you're here declaring it.

28:57Declare.

28:58Declare this reference variable to this

29:00object to to to an object of this type.

29:03This is actually

29:05not creating an object. Okay? The first

29:07this line that we have over here, this

29:09one. Uh this is basically just a

29:12reference to an object of type student.

29:16At this point, the student number one

29:19does not yet like refer to an actual

29:21object. Okay? It is just in the stack

29:24memory like this.

29:26Stack memory.

29:28We have the stack memory.

29:30We have the

29:32student one.

29:35That's it.

29:37Some people may ask, "Kunal, okay, this

29:39reference variable is in the stack

29:40memory. You have not created an object

29:42whatever. You have just declared it

29:43right now. Then, what is is pointing

29:45to?"

29:47What is it pointing to?

29:49Before asking such questions, try to

29:51print it.

29:52You'll find the answer yourself. Let's

29:53see.

29:54Let's try to print it.

29:56Okay, so let's try to see if we try to

29:58print this, what will happen. Right now,

29:59by default, it's not going to It's just

30:01going to say that it is not initialized,

30:03for example, right now.

30:04Okay, that is what it's going to say.

30:06So, when the when when it is like not

30:09initialized, it will ask me to hey,

30:11initialize it, and by default, it will

30:13do it as null, for example. Okay? But,

30:16let me do something else. Let me say

30:18that okay, this is a This is a This is

30:20an array of students, right? This is an

30:22array of students. So, let's try to do

30:23one thing. Let's try to see what is

30:24inside this

30:26array. If every element in this array is

30:29a student class type, then what is the

30:31type of that thing?

30:33Print

30:34arrays.toString

30:38students. Let's try to print and see

30:40what we get.

30:46null

30:48So, when this is not initialized, the

30:50student when it is not initialized, by

30:52default, the value is what here in Java?

30:54null.

30:56For primitives, it may be different.

30:58We'll talk more about that later, but

31:00for objects, it's null, as you can see

31:02from this example.

31:04Okay? All the All the students here are

31:07null.

31:08Uh Now, let's talk a little bit more

31:11about that, like how we can actually

31:12create this object and stuff, and by

31:14default, if it's null. So,

31:17here you can see that uh

31:19this is declaring. So, this is actually

31:22just, you know, um

31:24we are just uh

31:25uh creating a reference of type student,

31:29but if we want to actually create an

31:32object of this,

31:33we do it like this.

31:35We have done this before as well.

31:37student1 is equal to new

31:39type student

31:42like this.

31:43So, this new operator, it it actually

31:46dynamically allots. What do we mean by

31:48dynamically allots? Very important

31:49point.

31:50Allocates the memory at run time.

31:54And returns a reference to it. Okay? So,

31:57let me write this down.

31:59I'm telling you everything how things

32:00work. Dynamically, you are responsible

32:03for working with the nodes and

32:05everything. Okay, I'm putting out

32:06knowledge out there.

32:07Dynamically

32:09allocates,

32:11but I'll mention this on my notes as

32:12well. Uh dynamically allocates memory,

32:15um and returns a reference to it.

32:20reference variable

32:25to it. Okay, which is going to be stored

32:27here in student1. Okay, a reference to

32:29it.

32:34Okay?

32:35That is it.

32:37And this reference is stored in this

32:39student1 variable.

32:42That is how it works in Java.

32:44The student The objects are stored in

32:46the memory heap memory. So, this is the

32:47heap memory. An object of this student

32:50type will be created like this.

32:52Okay? And it will be pointing to this

32:55type. And this is going to have roll

32:58number,

32:59name, and

33:01marks, something like this.

33:03If you create another student, student

33:05number two, it will have another object.

33:08student2 like this.

33:11This is how it works internally in Java.

33:13Very important point.

33:15Please note, in Java, this is how this

33:18thing works. Hence Hence, all class

33:21objects in Java must be allocated

33:24dynamically.

33:25What do you mean by dynamic dynamic

33:26dynamic memory allocation? Some people

33:28may not know that. If you are not aware

33:30of dynamic memory allocation, it means

33:31you have not watched the first video of

33:33the DSA boot camp.

33:34Introduction to programming.

33:36Over there, we covered it. Static and

33:38dynamic languages, I mentioned five five

33:40points. I mentioned stack and heap

33:42memory over there as well.

33:44Do you want me to repeat it? I will

33:45repeat it. Otherwise, you will say,

33:46"Kunal, you don't teach." Uh I have

33:49taught it in detail in the introduction

33:51to programming lecture.

33:53Dynamic memory allocation means that

33:56first, you know,

33:57why am I telling this? Aha. Do you want

33:59me to explain to you how at the entire

34:01Java code compiles? No. Watch the

34:03introduction to programming introduction

34:05to Java video. Over there, you will

34:06understand what is dynamic memory

34:08allocation.

Dynamic Memory Allocation

34:09But still, I'll give a brief about it.

34:11So, when you write something like this,

34:13student

34:16student1 is equal to

34:19new student.

34:22I'll talk more about the new keyword.

34:26And what this this thing is Every single

34:29character that I'm writing, right? I

34:31will tell you about that. Every single

34:32character. I will not leave anything.

34:34That is why this is going to be a long

34:35playlist. So,

34:38here here it's saying that you know, all

34:40the things that are on the left-hand

34:41side of your equal to, this happens at

34:43compile time.

34:46This happens at run time.

34:49What is compile time? Compile You all

34:51know what compilation is, because you

34:53have been following the course. I've

34:54covered it in detail. So, compiling

34:56means that the program is checking

34:57whether there are any errors or whatever

34:58like that. In Java, you know, it gets

35:00converted to byte code, then from byte

35:02code, it goes into machine code and

35:04stuff using JVM and stuff and all these

35:06other things.

35:07And run time actually means when the

35:09everything is finished happening, all

35:11the checks have happened, all the, you

35:13know, code has been converted. Run time,

35:16as the name suggests, means the program

35:18is now running. Your application will be

35:20running.

35:21That is when the memory allocated

35:24memory will be allocated.

35:25That is known as dynamic memory

35:27allocation.

35:29Like memory in the RAM or whatever will

35:32be allocated when your program is

35:34running.

35:37That's what happens, and that is how new

35:39works. Okay? Enough details about it.

35:41Please make sure you check out the

35:41previous videos. You will understand

35:43even more. But I think I already covered

35:44it in detail. Okay.

35:46And stack memory heap memory, uh

35:48variables are reference variables stored

35:50in the stack. They point to the heap

35:51memory. Heap memory also in detail, I

35:53have covered this, like all these things

35:54before, but I don't think anything is

35:55left out. I literally revised the

35:57concept from scratch. Okay?

36:00So,

36:03that's it. Yeah.

36:04Um

36:06You can use now the student1, mimicking

36:09it like as if was the object, but it's

36:11not the object. It's a reference

36:12variable pointing to that object. In

36:14reality, it is holding like internally,

36:16it may be holding like some memory

36:18address or whatever of the original

36:20object that is in the heap.

36:22But we can't get that memory object. We

36:24can't access the memory address, because

36:26in Java, it's not allowed.

36:28Okay? So, this is the key for Java

36:30safety, that you cannot manipulate

36:32references as you can with like pointers

36:35in C++ and stuff. Here, you can't do it.

36:37You can't access the address. It's not

36:39allowed.

36:40Okay? So, you cannot cause an object

36:42reference to point to an arbitrary

36:46memory location or manipulate it.

36:50You know, like for example, an integer

36:51or whatever.

36:52We have already

36:53You know, you know how integers work,

36:54right? And we'll cover that in detail as

36:56well.

36:57Okay? So, if I'm saying we covered that

37:00in detail, let's do it right now. So,

37:02let's look at this thing, what it what

37:03it basically means. So, here, if I'm

37:05saying that um

37:07the student1 is equal to new student.

37:09So, this is actually a variable that is

37:12of type student of this class being

37:15created.

37:16And the class name is the name of this

37:18class that is being

37:19instantiated.

37:22Instantiated.

37:23So, when you create an object of a

37:24class, it means that it is being

37:26instantiated.

37:28Okay? So,

37:30one more thing remaining now is I've

37:32detailedly in detail covered new. So,

37:34this new If someone asks you, "What does

37:35new does?" Dynamic memory allocation.

37:37That is what new does.

37:39Okay?

37:40Only thing that I haven't explained

37:41right now

37:42is

37:45what is this thing?

37:49This is something I have not explained

37:50right now.

37:51And I'm not going to leave out any

37:52detail.

37:53I will not let you have any doubts. I

37:56will cover every doubts in the videos

37:57itself.

37:58Still, if you have any doubts, I'll

37:59actually teach you how to study

38:01object-oriented programming as well.

38:02Just like I taught you in recursion, I

38:03will teach you how to study

38:04object-oriented programming as well.

38:06Okay? So, what is this this thing? But

38:09uh

38:10Right now, you know how to create new

38:12objects. You know that. How to access

38:15the variables of that object. You know

38:17that.

38:18Let's see how we can now manipulate it.

38:20Okay? After that, I'll teach you what is

38:22this bracket thing. Okay? No problem.

How to manipulate objects?

38:25So, now you are saying something like if

38:27I'm declaring it,

38:29okay? Um So, now I have declared it, and

38:32now I'll initialize it.

38:33Kunal is equal to new

38:36of type student. Forget about what this

38:39thing is.

38:40I will tell you right now. Okay? Right

38:42now, I will tell you in detail, but uh

38:45for now, don't worry about it, because

38:47it's coming. In one line also, you can

38:49do. In one line, you can do it like

38:50this.

38:51In one line, you can say stu

38:55student Kunal is equal to

38:59new student. Something like this.

39:03Okay?

39:04Let's try to print the by default value

39:06of Kunal, what it is.

39:09Let's run.

39:13It's giving me some random value.

39:16Okay? This random value. I'll I'll cover

39:19that in detail. Don't worry what this

39:20value is and everything. So,

39:24I'll actually how to print some pretty

39:25values. I'll cover that in detail when

39:26we do object, like when we do

39:28polymorphism, right? There, I will cover

39:30it.

39:30Okay, so right now, let's say it's just

39:31giving some definition of some object

39:33value or something. It's like some some

39:35value it's giving random value, okay,

39:36for now.

39:37Uh what if I try to print Kunal.roll

39:40number?

39:44I will cover this thing as well,

39:45system.out.println and stuff. By

39:47default, it's giving me zero.

39:51By default, it's giving me

39:52zero.

39:56Let's see if I try to print

39:59Kunal.name,

40:01what will it What will it give?

40:02By default, what is the value by default

40:05of string type when it's not

40:07initialized? We just did it. What is the

40:10by default value of objects when they're

40:11not initialized like this?

40:13Null.

40:15Right? By default, it will be null.

40:16These are primitives, so these will have

40:18by default values of an integer is zero,

40:21of boolean is what I think false, of

40:23float is what? Let's see.

40:25I think it's 0.0 or something.

40:28Kunal.marks.

40:30By default value of float is what? Think

40:33since it's primitive, 0.0. Yeah, that is

40:35correct.

40:38Very simple stuff. For all the other

40:39primitives, you can try it out. What are

40:40the default values? So in short, already

40:43covered it previously.

40:45Integers

40:46have some default values, but the

40:48objects on the other hand of more

40:50complex data types since this is a class

40:52because it starts from capital letter,

40:54you can also control click and see on

40:55it.

40:56This is a class.

40:58Right? Uh this is of type like string,

41:01so obviously it's not initialized right

41:03now. It will be of type what? Null.

41:07Okay. So I can modify it according my

41:09new Java as well. So I am accessing this

41:11reference variable via RNO, so I can say

41:14Kunal's roll number, I'm accessing it

41:16right now. Fix it to 13.

41:21Kunal.name, fix it to

41:24Kunal Kushwaha.

41:27Kunal.marks, fix it to 88.5%.

41:32Okay, float.

41:35Run this.

41:40Make sense?

41:43Very easy stuff.

41:46Let's say you don't

41:48have it like this and you have some

41:50default values available over here.

41:52Let's say some default value like 90%.

41:56In that case,

41:58let's see what will happen.

42:0290%.

42:05I will teach you why this is happening,

42:07what is happening with a diagram. You

42:10will never ever get confused or forget

42:12it in your in your entire life.

42:15My guarantee.

42:17Let's bring up the whiteboard of how

42:18this thing is working. Okay. So student

Java Constructors

42:22created like this.

42:25Okay, student class.

42:27Student class. It has a roll number, it

42:30has a name,

42:32and it has a

42:33marks.

42:35By default, this is equal to what?

42:37Internal Java implementation, it says

42:39primitives like integers are zero by

42:41default. This is a string name, so it

42:42will be by default null.

42:46Okay, I create an object. I say

42:50using new, I create an object

42:52like this.

42:56I'll do it like this. Okay, for example

42:59right now.

43:00Here now it's going to say, let's say

43:02this object name is Kunal.

43:05The reference variable name, okay?

43:07So when I do something like this,

43:10when I say Kunal.RNO,

43:13roll number,

43:14first it's going to check in the object.

43:17Does roll number exist over here?

43:20Please understand, I'm visualizing it

43:23for you. It's going to be like, "Hey,

43:25does roll number exist over here?"

43:28It's going to be like, "Uh no, it does

43:29not. Okay,

43:30look at what the default value is then."

43:32It's going to be like, "Hey, default

43:33value is zero."

43:34It's going to print zero.

43:39Then when I say something like Kunal.

43:42dot name,

43:43it's going to be like, "Hey, does name

43:44exist over here?" It's going to be like,

43:45"No, it's not. Okay, print the default

43:46value, null."

43:49Similarly, print the default value for

43:50marks.

43:52But

43:53when I do something like this,

43:55when I say

43:59Kunal.roll number is equal to 13.

44:04Kunal.roll number, this Kunal. means in

44:08simple terms, it is not It may not Yeah,

44:10it It is. In simple terms, it means the

44:12Kunal object.roll number means the roll

44:15number inside the Kunal object. So it's

44:17going to be like, I'll make a roll

44:18number over here.

44:20And the value of this is going to be

44:21equal to 13.

44:24Kunal.name is equal to Kunal Kushwaha.

44:26Okay. Name over here is going to be

44:29Kunal.name is going to be equal to

44:31Kunal Kushwaha. Let's say this is all I

44:33do.

44:34Now when I print Kunal.RNO, it will be

44:36like, "Hey, does RNO exist in the

44:38object?" It's going to be like, "Yes, it

44:39does. Okay, print 13."

44:41Kunal.name, does name exist in the

44:43object? Yes, it does, Kunal Kushwaha.

44:44Print.

44:45Kunal.marks, do marks exist in the

44:48object? It's going to be like, "No, it

44:50doesn't.

44:51It does not. It's going to be like,

44:52"Okay, print the default one."

44:54Similarly, when you create a new object

44:55like this,

44:57you create Rahul.

45:00You don't initialize You just initialize

45:01it, but you don't

45:03actually change the values of the

45:04reference variable, the the the these

45:06variables and stuff that we have over

45:08here. So in that case, what is it going

45:09to be doing? It's going to be like,

45:10"Hey,

45:12um

45:14give me Rahul.roll number." It's going

45:16to be like, "Does Rahul.roll number

45:17exist over here?" It's going to be like,

45:18"No, it doesn't. Print zero."

45:22So

45:24dot roll number, dot this thing,

45:26whenever you do from a reference

45:27variables

45:28for non-static type. I'll cover what is

45:30static and non-static later on. Forget

45:33about static and stuff right now. Right

45:34now, just imagine that every object had

45:36this has its properties that are like

45:38common to this thing.

45:42Okay, but here you cannot add like some

45:45random property.

45:46You can only do Kunal.RNO, Kunal.name,

45:49Kunal.marks

45:51for only those variables that are

45:53already present in the template of the

45:55class. You can't do something like

45:56Kunal. uh

46:02salary.

46:03Why? Because salary is not available

46:05over here.

46:07Okay.

46:08Let's try it out. You can do it in

46:09Python.

46:11Okay, you can't do it over here.

46:13This is a static type language, Java.

46:18So if you try to do something like

46:23Kunal.salary, it's going to give an

46:25error. It's like, "Hey, salary does not

46:26exist in the type of Kunal." What is the

46:28type of Kunal? Student. Does not exist

46:30over here.

46:31You can do it in Python, though.

46:33Okay, which we'll cover in the Python

46:35boot camp.

46:36So that's it. Yeah.

46:38That is it. As simple as that. But this

46:41is a little bit cumbersome.

46:43This is a little bit cumbersome.

46:45Question, doubt you may have. Kunal,

46:49can't you just allocate these values

46:51while you are creating an object? Like

46:53when you're creating an object like

46:55this, can you not just like allocate the

46:57values during that time? Because it's

46:59getting very repetitive. Kunal.roll

47:01number, Kunal.name. Let's say Kunal had

47:03Let's say student had 100 properties.

47:05Would you write this 100 times?

47:08Isn't there a better way? Yes, that's

47:10where this thing comes in. What is this?

47:13Constructor.

47:14This is known as a

47:16constructor.

47:18It's actually a function type thing

47:19because we're calling it.

47:21So it's a special function. Let's see

47:23what a constructor is and how it works.

47:25Okay. So constructor basically, I'm not

47:28going to write this in this whiteboard

47:29because it's very simple statement. A

47:31constructor basically defines what

47:34happens when your object will be

47:35created.

47:38Okay, hence this is what we call by We

47:41are This is what is simply what I And

47:43what do we need to do when the object is

47:44being created? We actually need to

47:46allocate these things.

47:49We need to

47:50allocate these things.

47:53All right. So let's see how we can do

47:55that. So let's look into it like how are

47:58we going to use the constructor to

48:00basically instant

48:02like instantly

48:05instantianize

48:07uh ins- instantiate

48:10Okay.

48:13Ins-

48:15tantiate.

48:16Okay, initialize or something, okay? Uh

48:18not initialize, but actually create an

48:20ob-

48:21create an object out of it. You know

48:22what I mean, okay?

48:23Uh so basically, what we want to do is

48:25put some values over here. I think that

48:26makes more sense, easy to understand.

48:28Okay,

48:29so instead of having these default

48:31values, let's see how we can use a

48:32constructor. We We already know how to

48:34do it. We're doing Kunal.roll number,

48:36set something. Kunal.name, set

48:38something. But let's say we we might

48:40want to do something like this. Do it

48:41once at once. Make it mandatory to do.

48:45Okay, it's not like you are only setting

48:47roll number and name and forgetting the

48:48marks whenever you're creating a new

48:50object. No.

48:51Put all the three items.

48:54Let's see how we can do that.

48:57Okay.

48:58So

48:59constructor, what it what it is and how

49:02we are doing it. So basically, whenever

49:04you

49:07This is the This is sort of like the

49:10No, this is sort of like the

49:12um

49:14how a constructor basically like um

49:17looks like. Okay, for example. So

49:21if you're saying something like um

49:29a constructor. So you're saying

49:30something like let's say student. So

49:32what we want, let's say we consider the

49:35what first and then the why later on.

49:37So, don't worry about how it's

49:38happening. Let's first see what do we

49:40want. We want to do something like this.

49:42When you are creating an object

49:44during that time, initialize all these

49:46like

49:47are the values of all these variables

49:48that the class has. Okay, no problem.

49:50This is what we want. We want to do

49:52something like this.

By Default Constructor

49:53student

49:56Kunal

49:57is equal to new

49:59student and we pass the value inside it.

50:03I can just write it in new line.

50:05is equal to new

50:08student pass it in a new line. Here I

50:10can give a roll number,

50:13name,

50:17and marks.

50:21Now when I do Kunal.name, it will give

50:23me 13. Sorry, Kunal.roll number give me

50:2513, Kunal.name Kushwaha, and Kunal.marks

50:2784.3 or whatever. So, this is what we

50:30want to do. Now, how this thing works

50:32internally? This you can see you're

50:33calling it

50:35and it starts with the class name. So,

50:36it's actually a special type of

50:38function.

50:42Special type of function in class.

50:47in the class.

50:50Then you're going to be like, "Hey

50:51Kunal,

50:53when you were doing it previously like

50:55this,

50:56when you were doing it over here, we did

50:58not create any function in the class. We

51:00only had three things,

51:02roll number, name, and marks.

51:05Then what is this function being called?

51:07You are saying this constructor is a

51:08special type of function that is inside

51:11a class, but I don't see you creating

51:13any function inside the class in the

51:16previous example. Then what is this

51:17function being called?

51:19This is known as the by default

51:20constructor.

51:22Okay, when you don't have any

51:23constructor inside the class,

51:24constructor was a function. Okay, when

51:26you don't constructor is a function that

51:30is

51:31run

51:33Let me just write it down.

51:35Constructor

51:38is special

51:41function. So, no confusion over here.

51:43It's a function. What what else do you

51:45want me to say? It's a function. Special

51:47function

51:48that

51:49runs

51:51when you create an object.

51:54When you create an object

51:57and it

52:00allocates

52:02some variables.

52:05Okay, as you like it.

52:09Okay?

52:10So, if it's a function, it will be

52:11having some arguments.

52:13Right?

52:14Here by default, it has no arguments.

52:15This is known as the by default

52:17constructor. You don't have to make it

52:19or anything. By default, if you don't

52:20have a constructor, Java will call its

52:22own constructor that is just going to

52:24assign everything empty. Just like we

52:26did right now. Everything was empty. You

52:28can see it was just printing zero, null,

52:31and 0.0.

52:33That is a by default constructor. No

52:34need to make it. What if we want to make

52:36it on our own? Let's say during calling

52:39the function, we want to give it three

52:41arguments, roll number, name, and what?

52:44Roll number, name, and marks. And we

52:46want it to bind these

52:50bind these

52:52arguments

52:56with the object.

53:00Bind these arguments with the object.

53:04So,

53:06what do we want to do? This is a very

53:07important point, otherwise you will get

53:08confused.

53:10Nothing new.

53:11We were doing Kunal.name is equal to

53:13Kunal Kushwaha outside everything. Okay,

53:17let me show you.

53:19Let me show you what we want to do.

Creating Constructors

53:26So, we are doing this thing. Kunal.name

53:28is equal to Kunal Kushwaha or something.

53:30Okay, Kunal.name is equal to Kunal

53:32Kushwaha or something. And let's say I

53:34do Kunal .marks

53:37is equal to 84.5

53:39float or something like that.

53:41Okay, this is what we were doing. Or I

53:43can just copy

53:45uncomment this out.

53:48Okay, now we want to do it inside a

53:49constructor.

53:50How do we do it? Let's say we do

53:52something like this. We say instead of

53:54doing it here, I will do it inside the

53:56constructor.

53:57The constructor is somewhat like this.

54:00It's a function, so I would have to

54:01create a function. I will just say

54:03student.

54:06That's it. This is a constructor.

54:09You don't have to add a return type or

54:11what you don't have to add a name or

54:12anything like that.

54:14The return type is the class itself,

54:16obviously, because it's going to return

54:18the create a type of the object of this

54:20class, hence this is like the return

54:22type, but name is not required.

54:25That is it.

54:27Okay, what it is public or private or

54:29whatever, that is something we'll cover

54:31when we do access modifiers. Forget

54:33about that right now, because there's a

54:35separate long hours and hours long video

54:37on that. Access modifier will be covered

54:39in detail. Right now, just focus on this

54:41simple stuff.

54:42Return type is obviously this because we

54:44are creating an object of type this, and

54:46constructor is a special function that

54:47creates an object. No problem.

54:50So, this will actually run

54:52when you create a new object.

54:54This is what we want to do. It's giving

54:56me some errors.

54:57It's like, "Okay, you want to set the

55:00Kunal's roll number equal to 13 or

55:01whatever.

55:03That is what you want to do.

55:04But what is Kunal? I don't know. This is

55:06a template.

55:08In the template, in the rule section,

55:10how can you mention just Kunal?"

55:13Now, you're like, "Hey, I'm just

55:14referencing the reference variable of

55:16the object. This is the reference

55:18variable that I've created. I want you

55:20to add this reference variable's roll

55:22number should be equal to 13."

55:25So, you're like, "But why are you adding

55:27Kunal over here? What if you want to

55:28make another one called Rahul?

55:30Would you make another constructor

55:31called Rahul like this?

55:33For Rahul, another constructor or

55:34something like this? Then another

55:35problem will be how would it

55:36differentiate which one to call?"

55:39So, what we need to do is we need some

55:42sort of a way to initialize these.

55:46I'll just write it over here.

55:48We need a way to inish like to add the

55:52values

55:54of the

55:55above properties

55:59object by object.

56:02Like for every object, it will be

56:04different.

56:05Object by object.

56:07For that, so we need to access we need

56:10we need one word to access

56:15every object.

56:17What do we mean by that? So, when you're

56:19saying student Kunal is equal to new

"this" keyword

56:21student, we need it to automatically

56:26replace this particular space with

56:29Kunal.

56:30When you're saying this is a very

56:31important point. When you're saying

56:33student Rahul is equal to new student, I

56:35automatically want it to put Rahul over

56:37here.

56:38When you're saying student Anice is

56:40equal to new student, I automatically

56:42want you to put Anice over here.

56:45Then it will automatically if you put

56:46Anice over here,

56:47what we are doing? Don't worry about how

56:49we are doing it. What we are doing? So,

56:51when you automatically put Anice over

56:52here, it will automatically set

56:54Anice.roll number is equal to something,

56:56Anice.name is equal to something. When

56:58you automatically put Kunal over here,

56:59it will set Kunal.roll number something,

57:01Kunal.name something.

57:04What is the keyword for that to access

57:06the variable like that? The the

57:07reference variable, the object? It's

57:09known as the this keyword.

57:18The this keyword.

57:23Okay, you do not have to confuse this

57:25keyword with anything. It's very simple.

57:28What is this keyword? This keyword

57:30basically means when you are calling

57:32this thing.

57:33Let's try to print what will happen.

57:34Let's see.

57:36Let's see what will happen.

57:3913 Kunal Kushwaha 88.5.

57:43What happened?

57:44Debug. Let's try to debug.

57:47Let's see what happened.

57:49So, you are over here. It will go

57:51inside.

57:52No, uh I would have to do it again. Want

57:54to go inside.

57:56It will go inside where? Constructor.

57:58Whenever a new object is being created,

58:01it goes inside the constructor.

58:04New object being created, okay, go

58:05inside the constructor.

58:07Now, this is not the default constructor

58:08because the number of parameters here

58:10are none, and this constructor also has

58:13a number of parameters none. Hence, it's

58:14going to now call this one. It's going

58:16inside this. It's going to say

58:20over here, this. Um

58:23you know, roll number is equal to 13,

58:24this.name is equal to Kunal Kushwaha,

58:26this.marks is equal to 88.5. What is

58:29this over here? This this this is going

58:32to be replaced with this thing.

58:38Kunal.

58:39Okay, this thing, this orange color this

58:43will be replaced with

58:44Kunal. Internally, this thing will take

58:46place. Kunal.roll number 13, Kunal.name

58:50is equal to Kunal Kushwaha, Kunal.marks

58:52is equal to this. Comes out of it. Now,

58:55it's just printed. Kunal.name,

58:57Kunal.roll number, Kunal.marks.

59:01So, please don't get confused with this.

59:04You if you're thinking that it's

59:06confusing, then you will get confused.

59:07It is not confusing at all.

59:09This basically is just doing this same

59:11thing. This thing.

59:13That is what it's doing, but it's much

59:14more general. When you do something like

59:16this,

59:19student

59:23Rahul is equal to new student. In this

59:27case, it will say Rahul.name is also

59:29equal to Rahul.roll number is 13, Rahul

59:32name is equal to Kunal.

59:34So class is what? Class is just a

59:36template. So

59:38in order for you to access the object

59:41inside that template like okay, you are

59:44doing something.

59:46You can also let's say add functions

59:48inside the class. Okay, let's try to add

59:50some functions inside the class. I

59:51create a function

59:53um greeting. Hello name, it just needs

59:56to say hello my name is name. So I will

59:58just say okay, no problem. Let's Let's

1:00:00do that. Function function we know we'll

1:00:02just say void return type nothing.

1:00:04Greeting.

1:00:06And it's going to take nothing. It's

1:00:07just going to say print

1:00:09um hello.

1:00:11My name is

1:00:14my name is name.

1:00:16Okay, something like that. No problem.

1:00:18No problem. Now you will understand the

1:00:20meaning of this. Now you will understand

1:00:22the meaning of this.

1:00:23So now if I try to print something like

1:00:25this, I say

1:00:28Kunal

1:00:30dot

1:00:32what? Kunal has a greeting method? Yes,

1:00:34it does.

1:00:35Kunal dot greeting.

1:00:38Pause this video, think about what will

1:00:39print.

1:00:41Think about it. Is it going to print

1:00:43hello my name is Kunal or hello my name

1:00:45is something else? Let's try to see.

1:00:47Because you have already set Kunal's

1:00:49name is equal to Kunal Kushwaha over

1:00:50here. You have set it. It should print

1:00:52Kunal Kushwaha, right? Let's see what it

1:00:54will print.

1:00:59Hello, my name is

1:01:00Kunal Kushwaha.

1:01:03Okay.

1:01:04But one more thing I want to mention

1:01:05over here here is that uh what if you

1:01:08try to write it something like this?

1:01:09This dot name.

1:01:11Because we know that okay, I will tell

1:01:14you one single word on this the output

1:01:15will also be same, but I will tell you

1:01:17one single line that will clear the

1:01:18meaning of this for you. Okay, my name

1:01:20is Kunal Kushwaha.

1:01:23One more doubt you may be having is

1:01:24Kunal, it's giving the same answer with

1:01:25or without it, then why why are you

1:01:27doing it? So let me explain. Internally

1:01:30implementation is so by the way this

1:01:31basically means that okay

1:01:34whenever you try to access any

1:01:36particular

1:01:39item of the class via its object

1:01:43obviously every single item will be

1:01:45specific to that object. Greeting for

1:01:48everyone will be different. Name and

1:01:50roll number and marks for everyone will

1:01:51be different. In order to do that we use

1:01:54the this variable. So when you do

1:01:55something like this, Kunal dot greeting

1:01:58internally it's saying that okay, hello

1:02:00my name is

1:02:03whenever you say reference variable name

1:02:06dot whatever is inside the class

1:02:09this is replaced with

1:02:11the name of the reference variable.

1:02:13So when you do Kunal dot greeting it

1:02:14will be like uh hello my name is Kunal

1:02:17dot name.

1:02:18That is what it's doing internally. When

1:02:20you call the

1:02:21constructor internally it will be like

1:02:23okay, constructor is being called uh

1:02:25Kunal is like Kunal is something we are

1:02:27creating. Kunal is equal to new

1:02:29constructor. This will be replaced with

1:02:30Kunal. It will be like Kunal dot roll

1:02:32number is this. Kunal dot name is this.

1:02:33Kunal dot marks is equal to this.

1:02:37Okay, that is how it works internally.

1:02:39The doubt you may be having is why is it

1:02:40working this or with or without it? I

1:02:42will clear that right now. Okay, but

1:02:44internal implementation is it's

1:02:46imagining that this dot is already

1:02:48present over here.

1:02:50Okay.

1:02:51If you have done Python, it's like

1:02:52similar to self in Python, the self

1:02:54keyword. That is basically what this is.

1:02:56It's just replacing it with the name of

1:02:58the reference variable, which is Kunal.

1:03:01So as you can see you can add functions

1:03:03over here as well.

1:03:05Let me say one more thing.

1:03:07Let's say I create a function void

1:03:10change name

1:03:13and I pass a new name over here.

1:03:16New name.

1:03:19Okay, I'm saying that name is equal to

1:03:22um new name, something like that.

1:03:25Okay, I say Kunal dot change name now.

1:03:30Okay, I will say Kunal dot change name.

1:03:32Kunal dot change name to

1:03:35something like um

1:03:38what?

1:03:47Shoe

1:03:48lover

1:03:49or something like that. Now when I do

1:03:50greeting, let's see what will happen.

1:03:57My name is shoe lover.

1:04:00Okay.

1:04:02So you're just changing the name, but

1:04:04the important thing over here is that

1:04:06you have to write like this over So for

1:04:07example, you do something like this. Uh

1:04:10let me show you one more thing. Let me

1:04:12show you something like um

1:04:15a constructor one more type of

1:04:16constructor then it will make things

1:04:17much more clear and when things start to

1:04:19break, okay? So let's say you don't want

1:04:21to because if I'm creating let's say for

1:04:23Rahul right now, Rahul also have the

1:04:25same properties

1:04:26because every constructor is providing

1:04:28the same property. I don't want this. I

1:04:30want to add some properties inside.

1:04:34I want to say okay, pass

1:04:38a roll number, pass a name and pass a

1:04:43marks or something like that. Then I can

1:04:44say this roll number is equal to the one

1:04:46that you're passing over here.

1:04:48Roll number. This name is equal to the

1:04:50one that you're passing like this. This

1:04:53float is equal to the one that you're

1:04:54passing

1:04:55like this. Imagine I don't put marks

1:04:58over here.

1:04:59Okay, imagine I don't put marks over

1:05:01here.

1:05:03Then let's see what will happen.

1:05:04Now if I say something like Kunal is

1:05:07equal to new Kunal, I will add roll

1:05:08number 13

1:05:09uh Kunal Kushwaha

1:05:12as a name, oops.

1:05:15Kunal Kushwaha as a name and 85.4

1:05:20float as marks. Now if I try to print

1:05:22it, let's see what will happen.

1:05:23Let me change the roll number to

1:05:25something else.

1:05:26Run.

1:05:31Uh actually it did not print it. Let me

1:05:33just hide this thing.

1:05:35Let me print it like this.

1:05:37All the three things, let's see what we

1:05:38get.

1:05:43It did not get modified.

1:05:46Did not get modified.

1:05:48Okay.

1:05:49And by the way this name and these

1:05:54this name, this variable name and the

1:05:57one that you have inside your class,

1:05:59these do not have to be same.

1:06:00Okay, that is why we are putting this

1:06:02over here. So this can even be

1:06:05roll.

1:06:07Now now it's working, okay?

1:06:09Name or naam or something like that.

1:06:12Now we have something like naam, so Java

1:06:14is smart. Java is like okay, if you're

1:06:15taking naam over here, this naam is

1:06:17naam. But if you had the same variable

1:06:19name

1:06:21okay, if you have the same variable

1:06:22name, in that case you have to do

1:06:24something like this. This dot roll

1:06:26number.

1:06:27This dot name.

1:06:29This dot marks. Now internally it will

1:06:31be something like Kunal dot roll number

1:06:33is equal to RNO. This RNO does not have

1:06:35a this associated to it, so this will

1:06:37not be Kunal dot RNO. This will be the

1:06:39RNO that is passed over here.

1:06:41Now if I print it

1:06:43see what will happen.

1:06:46Works.

1:06:48And this does not have to have to be

1:06:49like this. You can say

1:06:50roll number

1:06:52like roll number. This will also work.

1:06:55Why? Because reference variables.

1:06:58Right? This is also working.

1:06:59This is going to be having some

1:07:01internally if I show you

1:07:03it's just something like this. It's very

1:07:05simple.

1:07:07So internally if I show you something

1:07:09like this

1:07:10so it's like um

1:07:12in the constructor when you pass

1:07:14something like roll

1:07:16so this roll is going to have a value of

1:07:1715 and Kunal

1:07:20dot RNO is going to be equal to roll. So

1:07:23it's just going to say Kunal dot RNO is

1:07:25equal to 15.

1:07:27That's it.

1:07:29That's how it works internally.

1:07:32Okay, but let me tell you a bit more

1:07:33details like I am I still remember like

1:07:35what you were saying. You may be having

1:07:36more confusions about why this this is

1:07:38important when it's working with and

1:07:39without it.

1:07:41Okay, let's see how basically it's a

1:07:43good convention to put this over here.

1:07:45Internally Java is smart. It's assuming

1:07:47that okay, you are saying this only. But

1:07:49imagine the name the new name variable

1:07:52in the local variable name was also

1:07:54name. This will give you an error. Now

1:07:55it does not know which name you're

1:07:57talking about. Are you talking about

1:07:58this name or are you talking about this

1:07:59name?

1:08:00That's why you have to specify.

1:08:04So this this is basically what? This

1:08:06this basically means Kunal.

1:08:08If you do Rahul, so with Rahul you can

1:08:11say roll number is 18

1:08:13name is

1:08:15Rahul Rana

1:08:16and 90.3

1:08:19something like that.

1:08:21Now when I print Rahul dot something, it

1:08:22will print the Rahul's names and stuff

1:08:24like that.

1:08:25So what is this? This is just replacing

1:08:27the

1:08:28these keywords.

1:08:32Okay.

1:08:34Cool. Let me show you one more example.

1:08:35Okay, this is what I was talking about.

1:08:37I used to the the the the the the name

1:08:39example. I was so I I realized my

1:08:40whiteboard was on.

1:08:42So if you don't have this keyword over

1:08:44here then it would be like are you

1:08:45talking about this name or are you

1:08:46talking about this name? Right or no?

1:08:49Kunal dot name set it to whatever name

1:08:51you have passed. This can be anything.

1:08:53Naam

1:08:55naam, no problem.

1:08:58But by convention it's easier to put it

1:09:00as same.

1:09:02Okay.

1:09:03So

Constructor Overloading

1:09:05a hint of like

1:09:06some object oriented principles. When

1:09:08you call a constructor with three

1:09:10values, it will call this thing. When

1:09:12you call it with zero values, it will

1:09:13call this thing.

1:09:16This is known as function overloading.

1:09:18Constructor overloading.

1:09:20So if I create a random person

1:09:22if I create a random person

1:09:24student random is equal to new student

1:09:28It's going to be like, "Hey,

1:09:30it's calling like an empty one. So, do

1:09:32we have an empty one?" It's going to be

1:09:33like, "Yeah, we do. We have an empty

1:09:35one." Let's say you remove it. In that

1:09:37case, you will get an error. It's going

1:09:38to be like, "An empty one does not

1:09:39exist. The three-argument one exists."

1:09:42Okay? Similarly, you cannot just add two

1:09:43arguments or whatever. Now, it makes it

1:09:47mandatory for us to add arguments over

1:09:49here.

1:09:50Okay? I'll cover it in detail how the

1:09:52memory management for this works.

1:09:53Polymorphism, this is known as

1:09:55polymorphism. I'll cover that in the

1:09:57polymorphism video. But, let me say

1:09:59something like this. Let's say that I

1:10:01want to create

1:10:03Let's say I want to create another

1:10:04object another constructor that actually

1:10:06takes value from another object. Okay?

1:10:09So, student it takes values from another

1:10:12type of student. Okay? Other student.

1:10:15And I want my current name to be equal

1:10:18to the previous name.

1:10:20Other student's name. So, here you can

1:10:22say this.

1:10:23name is equal to other.name.

1:10:27You can say this.

1:10:29roll number

1:10:30RNO is equal to other.roll number.

1:10:34And this.

1:10:36marks is equal to other.marks.

1:10:40So, here you can say something like

1:10:42this. Here you can see that if I pass

1:10:45Kunal over here.

1:10:47So, this other will be replaced with

1:10:48Kunal and the this will be replaced with

1:10:50random.

1:10:52Here you will have Kunal. It will

1:10:53literally replace Kunal over here. Here

1:10:55it will replace random. So, random.name

1:10:58will be equal to Kunal.name. Random.RNO

1:11:01will be other.RNO.

1:11:04Okay? Now, if I print

1:11:06random.name

1:11:08or random.name

1:11:10if I print it

1:11:13random.name

1:11:16Let's print it.

1:11:19Kunal, it will give me Kunal Kushwaha.

1:11:25Okay? You can remove it like this as

1:11:26well.

1:11:27But, then good convention is to follow

1:11:30the this naming convention the this.

1:11:33Okay? Because we see it was giving an

1:11:35error over here.

1:11:36Um when we were doing like the recursion

1:11:39sorry, not the recursion. This thing.

1:11:42It was giving error over here.

1:11:44Like right now, it does not know if this

1:11:45was also RNO. This was also RNO. It

1:11:47would not know which one to put. Hence,

1:11:50it would just use the default values of

1:11:51this. That's why

1:11:52this keyword over here is important.

1:11:56I hope you are able to understand what

1:11:57this is.

1:11:58Right? This basically means what object

1:12:02you are referring to in that particular

1:12:04thing.

1:12:04It will make much more sense also when

1:12:06we do static because static forget about

1:12:08it right now. Ignore ignore I said

1:12:10static, okay? We'll cover that later on.

1:12:13Okay? That is basically what the this

1:12:15keyword is and that is basically how you

1:12:16make multiple constructors.

1:12:18Now, you can use this constructor, pass

1:12:21the values, and create as many people as

1:12:23possible.

1:12:24When you say something like

1:12:27student

1:12:29Arpit is equal to

1:12:32new student

1:12:35something like this

1:12:3817

1:12:40Arpit

1:12:4389.7

1:12:48Here

1:12:50this will be replaced

1:12:53with Arpit.

1:12:55Hence, internally it will be something

1:12:56like this. Arpit.roll number is equal to

1:12:58the roll number you have passed.

1:13:00Arpit.name is equal to the name you have

1:13:01passed. Arpit.marks is equal to the

1:13:03marks you have passed.

1:13:05Same thing like we were doing here.

1:13:08This thing. Now, it's just happening

1:13:10internally in a much more streamlined

1:13:12way.

1:13:13This is a correct way of

1:13:15initializing objects.

1:13:18Okay?

1:13:21That is basically it.

1:13:22We got covered about this as well and um

1:13:26yeah.

1:13:27That's it.

1:13:29Let's look into a few more example.

1:13:30Okay? So, that was about like

1:13:31constructors and stuff. They don't like

1:13:32really have a return type because

1:13:34implicitly like uh

1:13:36the the type of the class itself is the

1:13:37return type. So, no no no void or

1:13:40anything you have to write. And the this

1:13:42keyword also, you know, sometimes a

1:13:43method in the class, you know, it would

1:13:45have to refer to the object that invoked

1:13:47it. Right? So, that is why we had the in

1:13:50the method also I showed you the change

1:13:53name one the greeting one. Okay?

1:13:54Sometimes like if you put the same value

1:13:57I was I also showed you that it was

1:13:58giving the not the correct answer. So,

1:14:00this keyword is nothing. It's just

1:14:01replacing the this with the name of the

1:14:04reference variable itself. So, that way

1:14:06it can say Kunal.name Kunal.something.

1:14:09Okay? That is why a general way for

1:14:11putting the name of the object is this

1:14:13because I can't put Kunal in the

1:14:15template of the class. Class is a

1:14:16template. Right? So, for that Java

1:14:19itself defines this this keyword. Okay?

1:14:22So, for it can be used like inside our

1:14:24method or anything to refer to the

1:14:26current object. Okay? It will be

1:14:28replaced with the current object when

1:14:30you call it. And um yeah, that is always

1:14:33like you know, this is always a

1:14:34reference to the object on which the

1:14:36method what was invoked like we did

1:14:39right now.

1:14:40Okay? And constructors will constructors

1:14:42are not done right now because we have

1:14:44to learn about constructors in

1:14:45inheritance as well in like classes and

1:14:48derived derived classes and stuff. But,

1:14:50constructor, you know, in short like we

1:14:52already covered it. Uh once you know, we

1:14:54define a constructor is automatically

1:14:56called when the object is being created

1:14:58and um

1:14:59obviously before the new operator that

1:15:02we have completes. Okay? And

1:15:04constructors uh we talked about their

1:15:06how they work and everything else as

1:15:08well. And um yeah, they create these are

1:15:11the ones that are actually with the new

1:15:13keyword they're actually creating an

1:15:14object in the memory.

1:15:16Okay? So, let's look into a few more

Calling a constructor from another constructor

1:15:19things and then we'll wrap. Okay? One

1:15:20more thing that the this variable can

1:15:22help us in doing is

1:15:23uh you can call a constructor from

1:15:25another constructor as well.

1:15:27Okay? So, you can do that as well. Let's

1:15:29say this is an empty constructor. It

1:15:31does not have anything, let's say. Um

1:15:32I'm just going to remove everything from

1:15:34it. Right? So, let's say this does not

1:15:36have anything. And whenever you let's

1:15:38say call an empty constructor like this

1:15:40let's say whenever you call an empty

1:15:41constructor like this let's say student

1:15:46random two is equal to new student.

1:15:49Let's say whenever you call a random one

1:15:51like this, it should automatically call

1:15:55it should automatically call this

1:15:57default one.

1:15:59Okay? Sometimes that might be possible.

1:16:01So, you can say

1:16:03call another constructor

1:16:05this is how you call a constructor from

1:16:08another constructor

1:16:12using the this keyword. What is this?

1:16:13This is a type that you know, type of

1:16:15that class itself.

1:16:17Like this.

1:16:19So, it will be calling what?

1:16:22Roll number I can add by default let's

1:16:25say 13.

1:16:27Uh string name can be

1:16:30default

1:16:32person

1:16:33and marks can be default let's say 100%

1:16:38or something like that.

1:16:39So, this this basically refers to this

1:16:42constructor.

1:16:43So, when you call this

1:16:45it's actually going to say, "Well, you

1:16:47can't really call the you know, can't

1:16:48really call Kunal like that." Right?

1:16:50Whenever you basically it's not it's

1:16:52just creating a new object. Right? Here

1:16:55it's say creating a new object. So,

1:16:57inside this this is going to be replaced

1:16:59with student.

1:17:01Another constructor. You can't really

1:17:03say random two call. Why? How can you

1:17:05call that? Random two is a reference

1:17:07variable. So, in this case this is

1:17:10actually replaced with the name of the

1:17:12class.

1:17:13So, internally it's something like this.

1:17:17Okay? Internally it's something like

1:17:21internally it's something like new

1:17:24student

1:17:25this thing.

1:17:28Internally it's something like this.

1:17:30Sometimes this is what you want to do.

1:17:32Say some people they pass nothing, but

1:17:35still you don't want to do it. You want

1:17:36to pass some default items. Now, if I

1:17:38print out like uh random two.name

1:17:42it's going to say

1:17:44print random two.name

1:17:46Okay? So, if you print random two.name,

1:17:48it's just going to print this

1:17:51uh default person name. Okay?

1:17:53Default person name. So, that's one more

1:17:56use case of this. You can use it to call

1:17:58constructor from another constructor. As

1:18:00simple as that because you tell me if I

1:18:01tell you that this actually reference to

1:18:03the reference variable, it does in all

1:18:05the other ways when it reference to the

1:18:06reference variable when you're calling

1:18:08it via a reference variable. Like

1:18:10Kunal.name Kunal.greeting Kunal.change

1:18:14name. But, while you are creating an

1:18:16object and then use this, then it

1:18:17replaces it with this thing.

1:18:20As simple as that.

1:18:22Okay?

1:18:24That's it. That's all you need to know

1:18:25about this and we'll be covering it

1:18:27later on as well, but I believe this is

1:18:29clear. Right? Let's move forward. Okay,

Why we don't use "new" keyword for creating primitive data types?

1:18:31let's

1:18:32talk a little bit more about some other

1:18:33things that you may be having a doubt

1:18:35for. So, you may be having a doubt like

1:18:37you might be wondering like why we don't

1:18:39use the new keyword for creating like

1:18:42when we do let's say primitive data

1:18:43types like integers or characters or

1:18:45booleans or floats or something like

1:18:47that. The answer is that in Java the

1:18:49primitive data types are not implemented

1:18:51as objects.

1:18:53I know that objects are stored in the

1:18:54heap memory. I told you this previously

1:18:56as well. Okay? But, in Java primitives

1:19:00are not objects. Hence, they are stored

1:19:02in the stack memory only. Now, you will

1:19:04say, "Hey Kunal, in the introduction to

1:19:06programming course you gave an example

1:19:07of A is equal to 10 and A 10 is pointing

1:19:10in the heap memory and A is pointing in

1:19:12the stack memory." You may say that and

1:19:14I stand by my statement that that was

1:19:15correct. Why? Because in that I

1:19:18specifically mentioned 10 to be an

1:19:20object. That intro to programming video

1:19:23was not specific to Java. For example,

1:19:25in Python, even primitives, there are no

1:19:27primitives in Python. If A is equal to

1:19:2910 is something you're writing in

1:19:30Python, 10 is an object in Python.

1:19:33Okay, so in that video I specifically

1:19:35mentioned that 10 is an object in that

1:19:37case.

1:19:39And that objects are stored in heaps.

1:19:42And in the later video when we did data

1:19:44types in Java, primitive data types in

1:19:45Java, there I also mentioned that

1:19:47primitives are stored in the stack

1:19:48memory.

1:19:50Okay?

1:19:51So, they are implemented like primitives

1:19:53to answer your question why we don't use

1:19:55new

1:19:56why we don't use new keyword. So,

1:19:58because they are implemented like as

1:19:59normal variables, we don't implement as

1:20:02those as like objects.

1:20:04They're just like primitive data types.

1:20:06Why do we do it? Why why does Java do

1:20:08it?

1:20:08To increase efficiency, to make it more

1:20:10fast because putting into the heap

1:20:12memory is have big big objects and

1:20:13stuff, it will be much more slower.

1:20:16That's why Python is also slow language

1:20:17by the way. Python is very slow, which

1:20:19we'll talk more about in the Python

1:20:20bootcamp.

1:20:21Okay. So, we talked about it, but we

1:20:23also talked about like

1:20:25you know how it um

1:20:28allocates memory like during runtime.

1:20:30So,

1:20:32yeah, I mean let's talk a little bit

1:20:33more about that.

Memory Allocation of "new" keyword

1:20:35Just one more thing.

1:20:36One one last thing I want to talk about

1:20:38is how it allocates like memory at like

1:20:40the new keyword. We're talking about at

1:20:42runtime stuff.

1:20:44So, when I write something like this,

1:20:47okay, imagine when I write something

1:20:49like this, when I write something like

1:20:52uh

1:20:53student

1:20:55one is equal to what? New

1:20:58student. Okay, no problem.

1:21:01No problem. Then I write something like

1:21:03this. I can say student

1:21:05two

1:21:07is equal to what? One.

1:21:10Now, obviously both one and two are

1:21:12referring to the same object. Why?

1:21:14Because you created student number one,

1:21:17you created its object in the heap

1:21:18memory, and now it's pointing to the

1:21:20heap memory. Then you created student

1:21:22number two, it's pointing to the object

1:21:24that two is pointing to, hence the same

1:21:26object. We have covered this in detail

1:21:28like when I showed you how two reference

1:21:31variables are pointing to the array list

1:21:32in the recursion also, right? So many

1:21:34reference variables pointing to the same

1:21:35object of the array list. And it's

1:21:37changing the entire stuff. That's

1:21:38exactly what is happening over here. So,

1:21:40this assignment of one and two,

1:21:42it it this assignment that we're doing,

1:21:44it did not allocate any memory or like

1:21:47uh

1:21:48this assignment of two to the variable

1:21:50one, it did not like allocate a memory

1:21:53or like copy any part of like the

1:21:54original object or anything. It is it is

1:21:56just simply saying point it to the same

1:21:59object as the pointer number one is

1:22:01doing like the reference variable number

1:22:03one is doing. Okay. So, obviously as

1:22:06previous example showed, any change

1:22:08might made by one will also lead to

1:22:10changes by two.

1:22:12Okay? So, when you assign one object

1:22:15reference variable to another object

1:22:16reference variable, you are not creating

1:22:17a copy or anything. You're just making a

1:22:20copy of that reference variable only.

1:22:21Let's see it in example. Okay, so an

1:22:23example for this can be something like

1:22:25this like if you make a change by one

1:22:26reference variable to another one, the

1:22:28the normal one will be changed. So, for

1:22:30example, if I make let's say random or I

1:22:32can make one or two only, okay? So,

1:22:34student one is equal to new student.

1:22:38Okay. And then I can say student

1:22:43two is equal to one. Okay, let's make a

1:22:46change in this particular one, okay? I

1:22:49can say one.name is equal to

1:22:53something something.

1:22:55Now, when I print two.name, it will also

1:22:58be changed to something something.

1:23:02Let's see what we'll get.

1:23:05Let's see.

1:23:06Something something. So, I changed the

1:23:08name of one, but the name of two was

1:23:10also changed. Why?

1:23:12Same thing like as I mentioned over

1:23:14here. You make um

1:23:16you make a change by a one, you can say

1:23:18one.name

1:23:20one.name

1:23:22over here is something something. So,

1:23:25then when I access two.name, it will

1:23:26also be something something. But we have

1:23:28covered this before in like the arrays

1:23:29lecture. Let's move forward. Okay, so we

1:23:31talked about like um

1:23:33the

1:23:34primitive data types and stuff. Uh

Wrapper Classes

1:23:36there's also a way to create it using

1:23:39the new keyword. Okay, that is known as

1:23:41wrapper classes. Let me show you how we

1:23:43can do that.

1:23:44So,

1:23:45here you can see if I create another

1:23:47class called let's say wrapper

1:23:50examples.

1:23:52Okay? Something like that. So, if I say

1:23:54public static void main, you can say int

1:23:56A is equal to 10, but you can also do

1:23:58something like this. Integer

1:24:01num is equal to new integer, a

1:24:03constructor you can pass 45.

1:24:06Okay? It's actually not required.

1:24:09Uh you can directly do it like this as

1:24:10well. That will also create like an

1:24:11object. Uh but now the difference is

1:24:14what is the only difference? Now it's

1:24:15being created like as an object. Okay?

1:24:18So, it will be like something like

1:24:20uh like if you do A. you will not get

1:24:23many stuff with it. But if you do num.

1:24:26Now, this num is actually an object.

1:24:28It's not like primitive stuff. It's

1:24:30actually an object of type integer. This

1:24:31is known as a wrapper class. So,

1:24:33obviously it comes with its own prop

1:24:35What is a class? Properties and

1:24:36functions. So, it has so many

1:24:38properties. Byte value, compare to

1:24:40double, equals, float value, hash code,

1:24:42to string, and all these other things

1:24:44you can use. Okay, so now you can see

1:24:46you got so many functions.

1:24:49Okay? For it.

1:24:51That's basically

1:24:53what like the wrapper classes are about.

1:24:55So, basically converting the character

1:24:56into a

1:24:57what? Like an object. Like

1:24:59primitive into like using it as an

1:25:01object. But one more thing you would say

1:25:03is that Kunal, you made a you made a

1:25:05program previously which was known as

1:25:07swap. So, you had something like int A

1:25:09and int B. And when you swap it,

1:25:12A

1:25:14you can say B is equal to

1:25:17int temp is equal to A, A is equal to B,

1:25:20and B is equal to temp. Something like

1:25:23that. You were saying that Kunal, when

1:25:24you call this swap function, if you say

1:25:26int A is equal to 30 and

1:25:29if A is equal to 10, and I can say int B

1:25:31is equal to 20. int B is equal to 20

1:25:34like this. Okay? And when you say like

1:25:36I'm going to I'm going to swap A and B.

1:25:38It will not swap. We have already

1:25:39covered this before. Okay?

1:25:42This has to be static. What is static?

1:25:44We'll cover later on. Uh so, if you swap

1:25:46it, if you say swap A {comma} B, then

1:25:49you print A plus

1:25:52B. It will not swap it. We already know

1:25:54it. Okay, we know that it will not swap.

1:25:57Why? Because these are primitives, and

1:25:58in Java there is no such thing as a pass

1:26:00by reference. Everything is passed by

1:26:01value. So, in primitives, pass by value

1:26:04is just a value. So, A the the pointer

1:26:07to this the reference variable will not

1:26:09be passed. Just 10 will be passed. So,

1:26:11this A is not actually equal to this

1:26:13one.

1:26:14This is an A that is in the scope of

1:26:16this function only. So, it's swapping it

1:26:18inside this function only. It's not

1:26:19actually swapping the original one.

1:26:22I've already covered this before because

1:26:24Java is passed by value. But when you

1:26:26pass objects, the reference value is

1:26:29passed.

1:26:30Reference is value is passed. This is

1:26:32also something we already covered. No

1:26:33problem. So, if you convert it to

1:26:35something like this, integer A and

1:26:38integer B,

1:26:40something like this, and you convert it

1:26:42to something like this.

1:26:43Say I just

1:26:46convert it to something like this.

1:26:49Integer A

1:26:51is equal to 10, integer

1:26:53B is equal to 20. Now, this is object.

1:26:56These are reference variables. Okay,

1:26:57now. So, obviously this will not be like

1:27:00passed by reference. Obviously it will

1:27:01be passed by the reference value. Should

1:27:03it swap now or not? Let's see.

1:27:08It won't. It's still not swapping. Like

1:27:11Kunal, now what is happening?

1:27:14You said this is like passing the

1:27:15reference variable to it.

1:27:17That is true. I can also create a new

1:27:18let's say this one this one can also be

1:27:19of integer type. No problem.

1:27:25Not swapping.

1:27:26Why? Let's see. If we take an example,

1:27:29if we take a look into what the integer

1:27:30class is, you can see it's a final

1:27:32class.

1:27:35Okay? This is the reason. Let's look

1:27:37into final. Let's see what final is, and

1:27:39we'll cover more about classes that are

1:27:41final later on. But this is basically

1:27:43the reason for it. So, basically when

1:27:45you type like integer A something like

1:27:47this, so

1:27:48here it's just saying, "Okay, I created

1:27:50a temp. Temp is going to store the value

1:27:52of A. A is going to point to B. B is

1:27:54going to point to temp or whatever."

1:27:57Here also, it's not actually changing

1:27:59the original values because it's like

1:28:02final and stuff.

1:28:04Okay? Let's see what final is. That will

1:28:06make much more sense. Okay, so what is

"final" keyword

1:28:08final? When we talk about final, so

1:28:09final is basically like we talked about

1:28:12the keyword this keyword. Final is a

1:28:14keyword, and using that keyword, you can

1:28:17basically prevent for

1:28:21your content to be modified.

1:28:23Okay? You can make it like a constant

1:28:24for example. So, some constants can be I

1:28:26can make a random constant like my

1:28:29increase in pay. Okay? So, it's a

1:28:31constant that everyone's pay is increase

1:28:33is going to be something like 2% or

1:28:34something like that. So, integer

1:28:38increase. This is a convention. Okay? If

1:28:41there's a final keyword, make sure it's

1:28:43all capital.

1:28:44is equal to 2% or something.

1:28:47Just add a final keyword over here. This

1:28:49will make sure that you are not able to

1:28:51modify this variable.

1:28:53Okay? So, now this increase the variable

1:28:55cannot be modified.

1:28:58Okay? It cannot be modified. Let's look

1:29:00an example of this.

1:29:03Let's look an example of this.

1:29:08So, here if I have something like a

1:29:10final keyword, I can say something like

1:29:12this. I can say final

1:29:15int

1:29:17uh bonus or something is equal to two.

1:29:21Then when I say bonus is equal to let's

1:29:22say I try to change it to three, it's

1:29:24giving me an error.

1:29:25You can't modify it.

1:29:27You can't modify it. That's basically

1:29:30what it is.

1:29:33One more thing I want to show you is uh

1:29:35that okay, I already mentioned about

1:29:37like the convention and stuff, but what

1:29:38if we have like a final field inside uh

1:29:42when we're talking about um

1:29:44you know, the um

1:29:46the classes and stuff. What if we have a

1:29:48final over there? Let's see.

1:29:52Makes sense like uh to have declaring

1:29:54the class as final, I will teach you

1:29:55that in inheritance because that is the

1:29:57concept that it's related to. But here

1:29:59if I talk about I create a class, I

1:30:01create a class A and it just has a final

1:30:05int

1:30:06uh number is equal to something or I

1:30:09don't have it or let's say whatever. So

1:30:11it's giving me an error. Obviously,

1:30:13because it's saying that it is not

1:30:15initialized.

1:30:16And final variables have to be

1:30:19initialized.

1:30:21Why? Let's see.

1:30:23Because here you can see that um

1:30:26you can't modify it, you can't

1:30:28add it or change it or whatever. That is

1:30:30why you you should always initialize the

1:30:33final variable when it is declared. This

1:30:35is an important point. Okay? So always

1:30:39since you can't modify it or whatever,

1:30:41that's the common sense, always

1:30:43initialize it while declaring it.

1:30:46Always initialize

1:30:49while

1:30:50declaring it.

1:30:53Okay?

1:30:54So that is sort of like the reason

1:30:56because you can't really change it or

1:30:57whatever. Okay? But unfortunately,

1:31:01final guarantees that this immutability

1:31:04that you cannot change it or whatever

1:31:06only when the instance variables are

1:31:08primitive data types and not the

1:31:09reference types of objects and stuff.

1:31:11Why? Because if the instance variable of

1:31:13a reference type has a final modifier

1:31:16behind it, the value of that instance

1:31:19variable will never change.

1:31:22Okay? The reference to the object will

1:31:24never change. It will always refer to

1:31:26the same object. But the value of the

1:31:28object can change.

1:31:31What do I mean by that? Let's see.

1:31:33So what do I mean by that is

1:31:35that this immutability that you cannot

1:31:37change the value okay? This immutability

1:31:40I will mention this in the notes as

1:31:41well. This immutability that you cannot

1:31:43change the value is only holding true

1:31:45for primitive data types.

1:31:47Then what if the object itself is final?

1:31:50Let's say you say final student

1:31:53Kunal

1:31:55is equal to new student.

1:32:00Here you can do Kunal.name if you want

1:32:02to change, you can do it to new name.

1:32:05But now you'll be like, "Hey Kunal, you

1:32:07said that final means you cannot make

1:32:09any changes."

1:32:10Then how are you making a change over

1:32:11here like this? No.

1:32:14It only means that you cannot make the

1:32:15change in the value when it is primitive

1:32:18data type. But if it is not primitive

1:32:20data type, then you can make the change

1:32:22in the value, but you cannot reassign

1:32:25it. You can't do something like this.

1:32:27Kunal is equal to other object

1:32:29or something like that.

1:32:32I'm not mentioning it over here because

1:32:33I'll mention it in my notes.

1:32:35Okay?

1:32:37So that is basically what it means.

1:32:39Let's try to look at an example for this

1:32:41as well.

1:32:42So if I try to look at an example for

1:32:44this as you can see initialization is

1:32:45important. But if I try to look an

1:32:47example for this, let's say I take an

1:32:48example over here. I say something like

1:32:51um

1:32:52you know, I say something like um

1:32:55string name is equal to nothing right

1:32:57now. In IntelliJ, you can also create an

1:33:00object like constructor like

1:33:02alt enter.

1:33:03Something like this, alt enter

1:33:05constructor name.

1:33:07Automatically created a constructor for

1:33:09me. Okay, no problem. So I can say

1:33:11something like

1:33:13not this. I want to say something like

1:33:14let's say

1:33:15student Kunal is equal to new student

1:33:19and I'm going to pass Kunal Kushwaha to

1:33:21it.

1:33:22Okay, Kunal Kushwaha.

1:33:25Okay?

1:33:27Um one more thing I want to share is

1:33:28okay.

1:33:30It should be inside public static void

1:33:31main. What am I doing?

1:33:33Should be inside public static void

1:33:35Okay? So here not student, sorry. A

1:33:38Kunal is equal to new A. A Kunal is

1:33:40equal to new A. Something like that.

1:33:43Okay? Now if I just say

1:33:46if I make it as final,

1:33:47basically I can do like this. Kunal.name

1:33:50is equal to

1:33:52other name or something. But I can't do

1:33:54something like this.

1:33:56Kunal is equal to

1:33:59new A

1:34:02new object.

1:34:04I can't do it.

1:34:05So when a non-primitive, so I will write

1:34:08it down.

1:34:09When a non-primitive

1:34:12is final, you cannot reassign it.

1:34:17You can change the values or whatever,

1:34:19this is allowed, but this is not

1:34:20allowed. You cannot reassign it. It will

1:34:22point to the same object. With that

1:34:24object, you do what you want, but you

1:34:25can't reassign it.

1:34:27That's sort of like the final keyword

1:34:28with this thing.

1:34:30Okay? I also talked about the final

1:34:31keywords over here and outside as well.

1:34:35Okay, let's move forward. So we have

Garbage Collection

1:34:36already talked about garbage collection.

1:34:38When a particular

1:34:41object in the heap, so let's talk about

1:34:42like garbage collection and how it works

1:34:44over here in object-oriented

1:34:45programming.

1:34:46Obviously, we'll cover this in detail

1:34:47when we do it like later on. So if you

1:34:49have something like this and this,

1:34:53A is pointing to some object. Now let's

1:34:54say A is pointing to

1:34:58some other object. Please watch the

1:35:00introduction to programming video. In

1:35:01that we actually covered this in detail.

1:35:03So this will be removed by garbage

1:35:05collector when when it wants to hit it.

1:35:08Okay? So it had to happens in Java

1:35:10automatically. The automatic garbage

1:35:13collection happens. But sometimes when

1:35:16the object is like destroyed with

1:35:18garbage collection water and whatever,

1:35:20you need to perform some sort of an

1:35:21action.

1:35:23To handle such situations, Java provides

1:35:25something known as finalization. So we

1:35:27talked about constructor in C++. You

1:35:29might have heard about destructors.

1:35:31There you free the memory, you create,

1:35:33remove all the objects and stuff

1:35:35manually. In Java, it does it

1:35:37automatically. But it Java does give you

1:35:39a way by performing some actions

1:35:43whenever your object is being destroyed.

1:35:44So for example, if a file object is

1:35:46being destroyed, you might want to close

1:35:47the stream or something. Okay? Whenever

1:35:49some object is destroyed, you might want

1:35:51to display a message or something. Okay?

1:35:53You can't do it manually. But what you

1:35:55can do manually is what? You can specify

1:35:59what to do when the object is destroyed

1:36:01manually.

1:36:02But you cannot destroy the object

1:36:04manually.

1:36:05You will tell Java, "Hey, I know you

1:36:06will not allow me to destroy the object

1:36:08manually. So can you please do these

1:36:10things whenever you decide to destroy

1:36:12the object?" Java will be like, "Okay."

1:36:14You can specify these specific actions

1:36:17that will occur when the object is about

1:36:19to be, you know, taken away by the

1:36:21garbage collector

1:36:22using finalizer. And a finalizer you can

1:36:25add to a class using the finalize

1:36:27method. Similarly, you have constructor,

1:36:29this is known as sort of like a

1:36:30destructor, but it will be called

1:36:32automatically when Java basically

1:36:34finalize method will be called when Java

1:36:36is um

1:36:38doing garbage collection.

1:36:40Okay? That is how it goes.

1:36:43So right before the object is freed from

1:36:45the memory, Java is going to call the

1:36:47garbage collector and the finalize

1:36:49method. Okay? This is how it So here

1:36:51what will happen is you will have

1:36:52finalize

1:36:55finalize

1:36:57like this.

1:36:58Something like this. Here it says that

1:37:00it's uh it's deprecated over here, but

1:37:01this is basically what it does. So if I

1:37:04just do

1:37:05you know,

1:37:06um

1:37:07something like um

1:37:12something like this, I can say something

1:37:14like

1:37:15object is destroyed or something like

1:37:18that.

1:37:20Okay? Object is

1:37:22destroyed. Something like that. Here it

1:37:24says that it's actually like

1:37:25deprecating. Uh so might have to look

1:37:27into like the docs or something, but

1:37:29that is basically what finalize used to

1:37:31do. They might have something different

1:37:32for it, but what is this override thing?

1:37:34What is this throws and throwable? That

1:37:36will be covered later on. So for

1:37:38example, if you have this finalize

1:37:39method inside the class A. Similarly,

1:37:42you can have different finalize methods

1:37:44in different classes. So whenever the

1:37:46object of class A, any object of class A

1:37:49is freed from the memory, this will be

1:37:50called. Let's try it out. Okay, so let's

1:37:52try to see. Let's say we do uh obviously

1:37:55if you put a, you know, print statement

1:37:57over here like object is being created,

1:37:59you can do something like this. Object

1:38:01created, but we only want to see when

1:38:02the object is being destroyed. So let me

1:38:04call a few like uh you know, let me call

1:38:07like something like this. I'm going to

1:38:09say let's say

1:38:10A object. Okay? I'm going to create I'm

1:38:14going to like create some

1:38:15thousand or something objects like this.

1:38:18A lot of objects like this. Okay? And

1:38:20then I'm just going to say object is

1:38:22equal to every time it's creating a new

1:38:24object. Adding some random name over

1:38:27here. So you can see once it will be

1:38:28created, then it will be created again.

1:38:30And you can see it's only using the same

1:38:32reference variable. Hence, so many

1:38:34objects are being created and I know

1:38:36that no more than one object can point

1:38:37to the same reference variable. Hence,

1:38:40it will be happening something like this

1:38:42internally.

1:38:43Internally, it will have like say obj

1:38:47in the stack memory, in the heap memory

1:38:49an object will be created. It will be

1:38:51pointing to that. Then when the for loop

1:38:52runs again, it will create another

1:38:54object. It will be pointing to that. For

1:38:55loop runs again, it will create another

1:38:57object. This will happen something like

1:39:00thousands and thousands of times. How

1:39:01many times I've specified in my

1:39:03um

1:39:04and this will be removed by garbage

1:39:08collector.

1:39:10Okay? Let's try to

1:39:12try to run this thing.

1:39:15Okay. For for less number of ones, uh

1:39:19it's not going to I believe call garbage

1:39:20collector. Okay, let's see.

1:39:22Let's run this.

1:39:24Program was running already. Let's run

1:39:25this again.

1:39:44Okay, so it says that nothing was shown.

1:39:47Okay, if you want to run this again, you

1:39:48can see literally it was not called. So,

1:39:51basically all these 10,000 objects that

1:39:53were created, uh it was not too much

1:39:55load on the memory, so it was like,

1:39:57"Okay, I'm not going to delete any."

1:39:58Let's put some load on the memory like

1:40:00this. Let's create a lot more.

1:40:02Now, it's definitely going to

1:40:04clear the memory with garbage collector.

1:40:07Yeah, object is destroyed. Object is

1:40:09destroyed.

1:40:10Okay, so it is destroying the objects.

1:40:13So, this object destroying thing,

1:40:15whatever thing happens, it will do it on

1:40:17its own. I can't really say something

1:40:19like this. I can't do something like

1:40:22uh object.finalize.

1:40:24Okay? We can't do it manually. Okay?

1:40:27It's saying like, "You you can't do it.

1:40:29You you can't do it manually." It's

1:40:31raising an exception and we'll cover

1:40:32exception handling as well later on.

1:40:34Okay, don't worry about this. Only new

1:40:36thing in this lecture is this thing and

1:40:38this thing that I have not covered.

1:40:40Override and throwable.

1:40:42So, these keywords I will cover later on

1:40:45in future videos. But, that's basically

1:40:47what I meant by garbage collection

1:40:48stuff. So, you can't really free the

1:40:50memory yourself, but you can tell it

1:40:51what to do

1:40:52when the memory is freed.

1:40:54Do check out the new docs for Java like

1:40:56how it deals with that, but it's still

1:40:58like in it's working over here as well,

1:41:00but it's important it is asked in

1:41:01interviews and stuff sometimes like how

1:41:03it deals with garbage collection. That's

1:41:04why I have to tell you.

1:41:06Okay, that was it about this. Uh just an

Outro

1:41:08introductory video. In the next few

1:41:10videos we'll talk about like packages,

1:41:11static, non-static, static, singleton

1:41:13class. We'll talk about properties,

1:41:15polymorphism, overriding, constructor,

1:41:16and all these other things, special

1:41:18methods, inheritance types,

1:41:19encapsulation, abstraction, uh access

1:41:21control, object class, object cleaning,

1:41:23interface, abstract I'm reading like the

1:41:25syllabus that I have. Uh abstract

1:41:27classes, interfaces, generics, except

1:41:29exception handling, uh collection

1:41:31framework, lambda expression, enums,

1:41:32file handling, and fast input output.

1:41:34All of these things will be covered.

1:41:36Okay, in detail. So, please make sure

1:41:37like, share, and subscribe. And one more

1:41:39thing I want you to do is comment.

1:41:40Commenting is important and liking is

1:41:42important. Uh and share about it on

1:41:44socials like object oriented programming

1:41:46started and really excited for the

1:41:48object oriented programming playlist.

1:41:49Please share about it on social. Use the

1:41:50hashtag BSA with Kunal. Tag me. Tag

1:41:52Community Classroom. Share it on

1:41:53LinkedIn and Twitter.

1:41:55We will retweet and sort of like the

1:41:56learning public initiated. So, show your

1:41:59excitement about the object oriented

1:42:00programming playlist and let's, you

1:42:02know,

1:42:03bring this course to as many people as

1:42:04possible. I will teach you literally

1:42:06everything that I just mentioned. And

1:42:07it's going to be great because object

1:42:08oriented programming is very very very

1:42:09very very important for interviews.

1:42:11Make sure you do all these things that I

1:42:13just mentioned. I will see you in the

1:42:14next one. Please make sure you comment

1:42:15down below as well. Like, share, and

1:42:16subscribe and share on socials. And

1:42:18yeah, see you in the next one.

Recently added transcripts

Browse the whole transcript library

This transcript was generated from the captions YouTube publishes for this video. Get the transcript of any YouTube video atfreeyoutubetranscribe.com, free, unlimited, no sign-up.