A Beginners Guide to Java Programming | Vishal Rajput | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

A Beginners Guide to Java Programming

teacher avatar Vishal Rajput, Founder of ThinkX Academy

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

23 Lessons (6h 33m)
    • 1. Welcome to Java Programming Class

      1:29
    • 2. Hello World in Java Bytecode JVM Eclipse IDE

      9:26
    • 3. Data Types and Operators in Java

      14:11
    • 4. Control Statements (if-else)

      15:22
    • 5. Classes and Objects in Java

      16:35
    • 6. Methods and Constructors in Java

      27:57
    • 7. Method Overloading in Java

      23:49
    • 8. Inheritance in Java

      25:33
    • 9. Packages and Access Specifiers in Java

      15:58
    • 10. Abstract Classes in Java

      5:00
    • 11. Interfaces in Java

      6:43
    • 12. Exception Handling in Java | Part-1

      25:20
    • 13. Exception Handling in Java | Part-2

      11:51
    • 14. Java Collections: Linked List & Stacks

      23:34
    • 15. Generic Classes in Java

      14:43
    • 16. Lambda Expression in Java

      17:28
    • 17. Multithreaded Programming in Java

      32:03
    • 18. Programming multiple threads using Runnable Interface in Java

      16:41
    • 19. Thread Priority setpriority() and getpriority() in Java

      22:56
    • 20. Multithreaded Synchronization using synchronized in Java

      27:09
    • 21. Interthread Communication in Java

      14:34
    • 22. Debugging and Breakpoints in Eclipse Java

      12:24
    • 23. Java JUnit Testing in Eclipse

      12:07
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

6

Students

--

Projects

About This Class

The objective of this class is to give you basics of Java Programming and also some advanced concepts of JDBC to connect your Java applications to MySQL Database.

I explain each and every line of code clearly so that you can understand the concepts.

This class contains all the essential topics to help you learn Java starting from basics.

Enjoy Coding :)

Meet Your Teacher

Teacher Profile Image

Vishal Rajput

Founder of ThinkX Academy

Teacher

I am a software developer with 4 years of experience in making products and working for startups.

I am a passionate teacher and educator at ThinkX Academy. I have experience in making good content for students to help them learn programming and get jobs in IT sector or build your own products.

Enroll in my classes to get in love with programming!!

Happy Coding :)

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Welcome to Java Programming Class: Hello everyone, welcome to the Java programming class on Skillshare. I'm the instructor of this course and I'm going to tell you what exactly is the content of this course and why you should consider this class. So what exactly done in this class? We will start from the basics of Java programming that I reteach each and every concept in detail using visitations. And we would also do the coding part in Eclipse IDE. So I would give her line-by-line explanation of what exactly we are coordinating and what is the objective of writing that code. So rake over some more important concepts like classes and objects, which comes under the Object Oriented Programming, which they'd hate you in your programming, could it? We will also study some important concepts like generics. Exception handling will handle the errors that will come out when you are running your applications, which is very important in production by molecule. Then we will also study JDBC. Jdbc is very important. Then we want to connect to Java application, then the database. So moving on, so I'm just kind of data also how to connect to the database and recreate that credit application, which will allow the user to create, read, update, and delete the contents inside of our database. So we'll see you in the next tutorial in the handle Word program, they are going to discuss how to write a Hello World program in Java. 2. Hello World in Java Bytecode JVM Eclipse IDE: So in this tutorial, I'm going to introduce you to the Java programming language. And we will discuss the first hello world program in java. And we will also discuss what are the applications of Java and why do you need to learn Java? What will you be able to program in Java? So Joe is one of the most powerful programming languages right now. And they are being used in a lot of enterprises, or I should say in a lot of companies, where they're developing some of the cool applications. So here are some of the applications in Java programming language you can create. If you know Java, if you have done Java programming language, you can create Android applications and you can even publish them on the Play Store. So they are basically specific for the Android applications. So next you can also create some desktop applications, some web applications for the IV almost site, etc, and the enterprise application. So in Java, in Java series, I'm just going to show you how you can use Java in various applications. So in Java series we are going to discuss data types, functions, and a lot of things. So first, and the most important thing about Java is that it is an object-oriented programming language. So if you have prior knowledge to C plus plus, now the next step is going to be loan Java. So Java is fully object-oriented. It's not like, see, it's fully object-oriented and you can learn the object oriented concepts in our channel. We have uploaded and we have uploaded it. So now I'm going to show you how Java program is compiled and it is run. So every program that here is, you can see that this is a flow diagram. And in the fourth part, you can see that there is a dot Java files. So every program that you are going to create, you will name it as dot Java extension. So you will give the source code to the compiler. And the compiler will convert the source code into bytecode. So let me define bite quartile. So bytecode is an instruction set for the JVM. Jvm is a Java virtual machine. Simply, JVM is just module that is, that just converts the bytecode into the machine code so that it can execute on your machine. So you will have to convert every program, every Java file into a bytecode or an instruction set, your program, your compiler will do that for you. It's just like an assembler. So JVM is a specific module which can run on Windows, Linux, Mac. So it is platform independent. So this makes Java more powerful because it is platform independent. It makes Java platform independent. This means that if you will create a program in Windows, you can run the same program in Linux, because the compiler will just convert your program into the Bitcoin and it will just pass this by a court to do the JVM installed on the Linux. In. Now we're going to study how we are going to see how we can create a helloworld program. For that. We're going to install the Eclipse IDE for. You can search it on Google, Eclipse IDE for Java. And after installing that, we're going to see a screen like this. So now what you have to do is you go on, you will go on this file option then on the Java project. So this will open this dialog box and you will just have to name the name of your project. And so this is our project. Now we're going to finish it. So after finishing it, you can see that in this package explorer, there is a project names test. So we're going to open this and we are concerned with this SRC folder. And every Java program that we are going to write is going to be a glass. So let's right-click on the SRC and then we will go on New, and then we will create a class. So you can see here, there is a class. So this will open this dialog box. So let's suppose I'm going to write the name of the class as x. And then we will have to check this one, public static, void main, because we want our program to have an entry point. As a main function, we know that every program has an entry point from where it starts its execution. So that will be the main function. So let's finish it. So after finishing it, you will see something like this. So I'm going to just make it smaller. And so you can see that this is our dot Java file, our source code. And then I will just run this program. Then the compiler will converted into the bytecode. But at this point we're just concerned that this block of statements, you can see that there this curly brace. So whatever we're going to do is we're going to do everything in this main function. And we will discuss about this whole program in the folder coming to durables. But at this point, there are some things that we should discuss. So every Java file is, since Java is object-oriented, so everything is going to be in the form of classes and objects. So we have created a class, and this class is thinking tax shield. So the name of the class should be, this, should match to this one. And then we have created, made it public in the package. We will see the use of this public keyword later on when we will discuss the access specifiers, then there is a package test. We will also discuss about packages. And this test is basically the package which contains this class. This glass has a function which is a public function. Again, we will discuss these keywords, public and the tutorial. And you can see that this main is the void because it is not returning any value. And the static means that you are not creating an object of this class to access this main function. So by making it static, it will just run in the venue or executing this function. So the simple thing that you need to do is, is to create a line right aligned which the display hello world in the output screen. So the nine is system dot out, dot print, LN, which is print line function, and it with a semicolon. And remember that these dots are very useful. They are very important if you miss out one, so you're going to get a lot of errors. So remember these dots after System.out print line function is used to print something in the output on the console. So let's suppose I want to output Hello World string. So I'm going to write it like this. So system dot out is a Java class and we're going to print it, print this on the output screen. So this is a very simple program and it needs some basic knowledge of object-orientation. All study of object oriented programming, because we are going to use glass and the static keyword. We will discuss about these keywords and we're going to practice more on these objects related concepts. And then we will move as we will move further. So in this tutorial, we have covered only the helloworld program. In the next tutorial, we will cover the datatypes and the operators. So let us run this and see the output. So as you can see in the quantum, we have low world. So that's it. Thanks for watching. 3. Data Types and Operators in Java: So in this tutorial, I will be teaching data types and operators for the, for a specific programming language like Java and C plus plus. So the operators, the data types you have studied in C plus plus are also there in Java. So we're going to cover the datatypes. So as we all know that our computer works on the binary format. So it knows only ones and zeros. So we have a lot of data and they are of different types. For example, we have characters like a, b, c, d. We have integers, which are the numbers only 1234. We have some floating point values like the decimal numbers 2.34.8 and so on. So I'm going to introduce you to some of the data types. So first of all, we have this table and these are the four data types that we're going to discuss. So what we do is whenever we want to assign a value to a data, or if we want to assign data to a variable. What we do is we define what type of data that variable is going to hold. So let's suppose I create a variable. A variable is just 0. You can say that it's just, it can relate int x equals 10. So if you have studied programming language before, you already, you're already familiar with that. So when we create a variable, we assign some data to it because a variable can hold any value. So when we do that, we before doing, before assigning a particular value to that particular variable. What we have to do is we will have to specify the type of data that variable can hold, the four assignment. So let's suppose we have a, we want to say that the variable can hold values ranging from minus 21474836482, this number. So if our integer is in-between this range, then what we're just going to do is we're going to assign the data type of that variable as int. So these are basically the data types and these are also the reserved keywords. So you can't use these, these keywords for any other naming purposes. So let's suppose I choose a number in between this range, and I want to assign it to a variable x. So the data type of that variable should we end? Similarly, these are different ranges. So if you're integer is very large value, if it is holding a very large value, you can. Assign it as long as it is. Taking a smaller values, you can assign short and bite. And the reason why there are different data types and integers for these ranges is because the, the memory problem. So if you want to store smaller digit, you should consider the short datatype because it will take less memory than the integer 1. So the memory is also considered. Here are some more data types are the float, double and so float and double the both handled or decimal numbers. For example, let's suppose I say 2.54.6. And like this, float has a very float has lesser range then the double. So if you have a number which has, which has decimal number up to a lot of places, so you can assign it as double. Next is the character data type. So character data type is just the alphabets, which are the a, b, c, d, x, y, z. So what we're going to do is in the Eclipse IDE, we will assign the datatypes, do the different variables like we will assign flows, we will assign double. And what we're going to do is we're going to perform some operations on the data, which are performed using some operators which we will discuss. So this is also another data type, which is the Boolean data type. And it holds value like true and false. So if anything is, if a condition is true, then the or if a variable is holding either two of the values, our true or false, then we're going to assign its data type as Boolean. So now let's move on to the next topic which is the operators. So now we have specified the datatype of particular variables. So we know that what type of data that, sorry, the computer knows what type of data that specific variable is going to hold and will just, I will just display some results in the Eclipse IDE. So let's discuss some of the operator so that we will be able to apply some operations like addition, multiplication, division, and so on. So you can see that in this table, we have these operators and these are known as the arithmetic operators. So plus means addition. So if you want to add two numbers or two variables with some data types like integer or float. And this operator can add those two values. It is also a urinary plus, which means that it If you write plus after a particular integer, it means that it is a positive number. And similarly there is subtraction multiplication. Multiplication is represented by S derisk. So division then the modulus, modulus gives us the remainder. Remainder by dividing the two variables. So there is a plus plus operator which is increment operator. So if you will, right, plus plus x. And let's suppose the variable x is having the value 10, so the value will become 11. So plus, plus increments the value of a particular a variable by one. Next is the addition assignment, substract, assignment, multiplication assignment and the division assignment, and the modulus assignments. So Indies in all of them, what we're just going to do is, let's suppose we have a variable x and I'm writing it as x plus equals to 10. So this is the same as x is equals to x plus 10. So this is the assignment operator, which means that you are adding a particular value to the variable and then you are assigning, get back to it. And similarly, in case of subtraction, we're going to subtract the value from the variable and then we're going to assign that value to the variable. We will see some of them in the Eclipse IDE. So the next is the relational operators. So here are some relational operators and they are used to compare two conditions or two statements. So let suppose this is the equal to operator. So if the condition on both the sides of this double equals is our true, then the condition will evaluate to be true. And we will use this relational operators in the if-else statements in the flow control part. So even if you will not pay attention to these, just, just remember what these values are. So this is a not equal to operator. This is greater than, less than, greater than equal to or less than equal to. And we will use them in the if-else statements and in a lot of places. So they're relational operators because they compare the two values and they give a particular output as true or false. So next, Our next is this table, which is the Boolean operators. So the Boolean operators gives the two values. It gives either true or false. And you can see that there are some operators like logical. And so this is the logical AND logical OR logical X4. So if you have studied Boolean algebra, you will be familiar with these. All of them. So this is a short-circuit all this is a not. So if you apply unknown. And exclamation after some variable it is going to change or it is going to just revert its value. So let's suppose the value of a variable a is true. And you put an exclamation after that variable so it will make it false. Similarly, here are some more Boolean algebra. This one is the conditional operator. We will also see an example of it as a ternary if, then else. And we will, I will just show it in the Eclipse IDE. So let's do the main task. Let's move on to our Eclipse IDE. And we left off at this point. And this is Java. So what I'm just going to do is we are concerned with this section only. So first let's create some variables. So you create variables like this in Java or in C plus plus. First, you write the name of the variable, like a or B, and then you assign the datatype of it. So the computer will know that a variable a is going to hold value in the range which we have seen in the table before. So now let's assign some values to the integers a and b. So here are the two values that we have assigned. So a has been assigned a value 12, B has been assigned a value 23. So now what we're going to do is we're going to display the addition of these two numbers. So system dot out, dot print LN is used to print the output to our console screen. So I'm going to write here a plus B, which will give us the sum of, which will display the sum of the a and B in the outward screen. So let me just run this program. So this should we print LN? So let's run this program and see its output. So you can see that in this output screen, we have got the 35, which is the sum of these two numbers. And similarly, we can also, you can also try it with all other data types like subtraction division. Or you can even try the modulus operator. So it will just give the remainder after dividing these two values. So let's move on to the assignment operator. Let's do an example of an assignment operator. So let's suppose I want to write here a plus equals n. So what it is going to do is it is going to assign, it is going to first add the value of the variable by 10, and then it is going to assign it to the variable a. So a plus equals 10 is similar to a equals a plus 10. So that's very simple. So I'm going to again display the value of a. So when they're going to do is I'm going to display the value of a. So let's run this program again. So now you can see that it is giving the value 22. So initially the value was equal to 12. But after using this a equals a plus 10, or a plus equals 10, it will add n to this dwelled value, so which gives us 22. And we're displaying the variable. So in this manner, you can use different arithmetic operators. So try it out yourself in the IDE. And we will study some relational operators in the flow control series. So in that we will use a fails, we will use for loops, while loops and a lot of other stuff which will compare two values. And we'll give a specific output. They will also see the conditional operator. So that's it. So thanks for watching. 4. Control Statements (if-else): So in this tutorial, we are going to study the control statements in Java. And control statements are basically used to run a block of statements based upon some conditions. So the most basic control statement is the if-else and the search statement. Both of them are the selection statements. And say by selection, I just mean that if the condition evaluates to true, then it is going to select some block of statements and it will execute them. So first, let's take an example of if-else statement. So you can see here the syntax of the if-else statement. So first you will use the if keyword and make sure that I should be small. Then after this IF keyword, you're going to write the condition. And the condition can be laid a less than b or x should be less than 10, or like that. So you're going to use some conditional operators in this place. So if your condition will be true or if the condition evaluates to true or one. So it is going to run the block of statements inside this, in between these curly braces. And if the condition is false, then it will move on to the else part and then it will run the block of statements in the else part. Moving on to the switch statement, switch statement is based upon the choice. So in switch treatment, choices given and based upon that choice, the program then choose a particular case. And if the case is matched, then it is going to run the block of statements inside that case. So let's suppose that Joyce, what an integer variable and it is equal to 2. So it is matched with the case one. So one is not equal to do so. That's why we have written a break statement here. Break statement is used to break out of this case. So it will now move on to the case two. Case two is the right choice. It will run all the cases. Sorry, it will run this statement. It will print case two, and it will break out of this switch case. So let's implement both of these statements in the Eclipse IDE. So you can see here, this is the void main function. And inside this void main function, I have all ready created an if-else statement. So first, I have created two variables, a and B. And I have initialized them with some values to add 1223. So now we have started the if part. And the condition says that if the value of a is less than b, so we know that the value of a is less than b. So the condition is true. So if the condition is true, it is just going to bring the sum of the two. Numbers, which is a and B. If this condition will be false, then the else part will run. And in the else part, the value of a is incremented by ten, and then it is going to print the value of a. So this is a basic example of an if-else statement. So it's very simple. If condition evaluates to true, run the block of statements inside the if part or else move to the else part. So you can also induce more If-else inside this else part or inside this, which are known as instead FL's. So let's move on to the switch case. So let's suppose the user has entered the choice as three. So in the switch case, we're going to write the switch and we're going to write the choice. So Joyce is three. So you can see here that I have created case 1, case 2. So in case 1, you can see that one, the choice is not equal to one. So it will not run this case. It will move on to another case, which is case 2. Since choice is equals to 3, which is not equal to 2, case 2 will not run, since there is no for the cases remaining. It will run the default case. And in the default case, we're just going to print out, though division of these two variables. And after performing this, we have included the break statement, which breaks out of this switch case. And it is necessary because if he will not write a break statement here, it is going to run all the cases. So this is the switch case and the if-else statements. So now we're going to move on to the iteration statements. So in the iteration statements, we have for loop, while loop and the do-while loop. So we're going to discuss all, all of them one by one. So first let's move on to the for loop. So here is a for loop. So you can see here, FOR loop is just a loop which will run a following statements or a block of statements until and unless there is a condition. And if this condition evaluates to false, then it will stop running these statements. That's why this is known as a loop. If there is no condition, then this, then this statement or the block of statements inside this for loop will run infinitely. So there are three parts of the for loop. One is the initialization, second is the condition, and third one is the updation. And make sure that these three are in the right order. You cannot shuffle them. So in the for loop, what you are going to do is you're going to first initialize a variable that suppose it is I equals to 0. Then this next part, you're going to write a condition. So we're going to run this block of statements until and unless the value of phi becomes greater than 10. So if this condition will be false, if I will be having the value greater than 10, since we have initialized the value of phi at 0, it is always true. So we will have to update the value of phi in such a manner that it will just make this condition false and it will break out of the for loop, or else our for loop will run infinitely. So in the next part we're going to abate the value of phi. So the flow of control in the for loop will be like this. First of all, you're going to initialize the value of phi. Then you are going to check the condition. So I equals to 00 is less than 10. If this condition is true, then the block of statements inside this for loop then move on to the updation part. After running all the statements, update the value of Y by incrementing it by one. So you can see this is an increment operator here. So now the value of phi becomes equals to one. Now after a patient move on to the condition, then we're going to check whether the value of Phi is less than 10. So one is less than 10. Then again run the following statements. Then we will update again, then check the condition, then again, run the following statements. And we're going to do that until and unless this, the value of Phi will be equal to 10, because 10 less than 10 as equals to false. So let's implement this four loop in our IDE, in our Eclipse IDE and see how, what will be the output of this program. Sooner. You can see that this is our for loop. And this is the v. I've initialized the value separated by a semicolon. Do not forget the semicolon, the semicolon, not a comma. Then we have written a condition here and the updation. So let me just write it as read only so that it does not fall magnet to the next line. So let's see the output of this for loop. So you can see here that our for loop has given this output. So 0123456789. So it has actually printed out the value of phi. It has run from 09. Then the value of phi becomes equal to 10. Then this than 10, less than 10 evaluates to be false. And thus it has not displayed. And here, so this is an example of a for loop. So let's move on to the while loop. So while loop is also another form of the for loop. While loop will also run a block of statements under learn list the condition. Given inside loop evaluates to be false. And in order to make this condition false, we will have to update the value just as we are doing at hill. So in this manner, we are going to run the block of statements in the loop. If you're not updating the values you in this for-loop, and sorry, this while loop will run for the infinite time. Similarly in the do-while loop, you can see here that this is a while then condition. And these are the block of statements. But see, you can see here this is a dual here. So when you have written a do-while loop, then these block of statements in the do-while loop will run at least once. It doesn't matter whether this condition is true or false, it is not going to check it many trends for the first time. So first time it will just check, it will not check the condition. It will just run these knockoff statements. Then it will move on to the bile part. Then it will check the condition, and then it will again move to the do part. And this goes on and on until and unless this condition evaluates to false. So let's just implement while and do-while loop in our ID, in our Eclipse ID. So you can see here first initialized the value of j as 0. So this is our while loop. You can see here that I have ever done a condition here, j should be less than nine. And then I have printed the value of j. And then we're updating the value of j. Symbol that you give will have to abate the value of j. If you will forget to abate this value, then this while loop will run for in finite number of times. So you can see in the IDE, it has displayed 012345678. So it has, it is running below eight because we have written nine here. So when the value of j becomes equals to nine, so nine less than 19 evaluates to false. So it has come out of this while loop. So now let's move on to this do-while loop. You can see that first we have initialized the value of k as 11 in the do loop, do-while loop. First we will run this block of statements. So first we are displaying the value of k, Then we have incremented the value of k. And then in the while, we have written the condition. So this statement will run at least once, even if this condition evaluates to false. So you can see here. So initially the value was 11. Initially that k is equals to learn. So it has been around 11. After that, the value of 11 is incremented by one. So now k is equal to 12. So in the while condition you can see it is false. So it will come out of this do while loop. So you can see that the benefit of a loop is that it runs the following statements at least once without checking the condition inside the while loop. So these are the four while and do-while loop. Now we are going to study the continent statement and the labels. So these are all some of the jump statements which are used to jump to a particular part of a program. So let's suppose we have a for loop like this. So you can see that in the for loop it will run n times, sorry, nine times. It will run from 0 till nine. So it will first display the value of I plus a space. Then it will print out the value of ordered. If the value of phi modulus 2, or if the value is even, which means that the remainder will be equal to 0. And that's why it is divisible by two. So then we're going to continue to the label order. You can apply a label by writing order with the following Smith following a colon here. So this is an label. So I'm going to write here labeled. So continue, skip all the statements after this if part. And it will start continuing the execution of the program from this label, which is the order. You can use this label in case of break also. So the main reason of using a continuity is that it will just continue with this for loop. If you will right, break here, then it will come out of this for loop. So these are the jump statements, the brake on the new and the labels. So we have discussed the control statements. In the next tutorial, we are going to discuss, we're going to introduce classes and methods in Java. So thanks for watching. 5. Classes and Objects in Java: So in this tutorial, I'm going to introduce you to the classes concept in Java. And basically classes is nothing but a datatype. So let me give you a situation here and we will see how that situation can be resolved using classes. So let's suppose we have a class in a school and it has over 50 students in it. Now want, is, I want you to create a program that will save the record of the students. England has some low number assigned to it. I'm going to write her role lumber, and also age and gender. So the basic approach is you will create three variables for each student. So you will create these three parameters or these three variables for student 1 fostered into phosphorus and three and similarly are going to do it for 50 students. So this is going, so you're going to create 50 into three, which is 150 variables, which is a very big amount. So if you will create a program like this, it will, it is going to take a lot of memory space. And, and accessing these values will be a lot difficult. So what we're just going to do is we're going to create a class data type with named student. In order to create a class in Java, you will have to use the class keyword, make sure that C is smaller. So what we're just going to do is we're going to create a class student. And in this class we will create its block of statements. And then what we're just going to do is we're going to write these three variables in this class. So let's suppose Romer is integer value, so end row lumber, H is also an integer. And let's suppose the gender is a character value as female or male. So character genders. So we have created a class data type student which has these three members. Or you can say these three are the data types or the data values of this glass student. So if you want to create 50 records of fifth phase two, what we're just going to do is you're going to create an object, student one. Then you are going to create another objects to do. And what you were just going to do is you're going to create the students the objects of this class. And these objects will refer to all the data types, sorry, all the members of this class. So instead of creating 150 variables are a lot of variables. We have just created a simple class. And now we're going to create objects or instance of this class. Objects or instance of this class. So now what you can do is you can use this object and you can assign the values of this object, which we will see in the program. So classes are very powerful and they are just basically object oriented. So they make Java object-oriented. You will see that in real life objects, real-life objects have some properties and some behavior. So let's suppose we have an orange. It has properties like it has a shape, sphere, it has color orange, it has some taste. And let's suppose we have another object, car. So it has some behavior also, which is its speed. And a lot of things are there. So an object can have properties which are just the variables, and can also have behavior. And behavior is represented using functions or methods. In this tutorial, we're just going where could just concerned with the properties. In the next tutorial, we will also create methods to define the properties of, of this class. So let's move on to the Eclipse IDE and create a class student and an object of that class. So here is our Eclipse IDE. And what I'm just going to do here is I'm going to create a class, and I will name that class as student. And I will create three variables of this class. One is the row number, one is the a, and then gender. So I've created three members of this glass student, right? So what I'm just going to do is in the main function, and I'm to create some objects of this class. In order to do so, you will have to use the name of the datatype, the datatype of the class, the student. And let's suppose I wanted to say that. The object is student one. So if you're writing this nine, it means, so I'm going to write a comment here. It means that you are referencing to a glass student. So what now, what you're doing is you're creating a student object, which is just referencing to this class. But now we have to allocate this object in the memory so that we can assign values of Rho, lumber, age, and gender. The memory. In order to do so, we will have to use a new operator or a new keyword, which is used to allocate the members of this class in the main memory. So what I'm just going to do here is I will write student one. Student, one is equals to new student. So this should be n. Gets some typing mistakes. So now you have created an object, you have created a reference to a plus student. And what you're now going to do is you're just assigning this student one. You're allocating it to the membrane. So allocating it in memory. So that's what basically we are going to do in this tutorial. Instead of writing these two lines, you can merge it in a single line. You can write students through N1 equals to new student, like this. So you can see here that it is giving us some warning here. So the warning is no enclosing in stance of the class thinks, okay, So this class should be outside, it should be here. So now we have done two things. One is we have created a class student which has some member variables in it. And now we have created a student one using a reference, and we have now allocated it in memory. So let's create another student. Is student too. And in the same line, we're going to allocate, we're going to allocate it in the memory like this. So now you can see that instead of reading a lot of variables and instead of making our program more complex, we have, may have made it more compact. So you can see that now it is easier to maintain the variables of each of these objects. So how can you assign the roll number, age and gender of a particular student? Or in general, how can you assign the members of a class to the object? So how can you initialize an object of a class? So what we're just going to do is in order to initialize, or I'm writing here, assigning values to student one. So if you want to assign values to those two next one, what you are just going to do is you're going to access the student one object. Then we're going to use the dot operator, which is used to access the member functions or the members of this class. So you can see here that after writing dot edx has, it is already mentioning these three members, all the properties. So let's suppose we are first going to assign the row number. So student one, a low number. Let's suppose the rule number on this student is 25. So now we have a student, student one which has low number 25. So let's suppose I want to assign student do a low number as 15. So it's symbol. You will just have to create a class. So you will have to create Last, create some members and net and then used to allocate the object in the membrane and also dereference it to the class student. So now we have assigned role number 2, the student one and student two. So you can see here that instead of creating a lot of variables for different students, we are just using the objects of different students and we're assigning rule number 2 then. So this has made our program more compact and more efficient. Now, it is easier to understand. You can see that student to row number is 50, so it is very easier to access the members. You will see the power of classes in the upcoming tutorials. Then you will explore inheritance also. So now what I'm just going to do is I'm going to assign some more. I, student one dot a. Let's suppose the ages 16. And let's suppose the age of student 2 is 17. Now, what I'm just going to do is I will assign the gender also, Sudan one dot gender. And since it is a character value, I will have to write it like this. So let's suppose the first student is male. So I'm going to write m here. And this should be student one. Like this. Then student to dot gender. As you go through, let's suppose the student two is female. So this is how you are going to assign the values to the object of this class. Some people, some programmers also use in stance as a replacement for objects. So they might say that we have created an instance of this class. So student one is the instance of this class and student 2 is another instance of this class. And we have seen how we can assign the values to them. Now what I'm just going to do is if you want to display. So now I'm going to display the age. I am going to display the age of student one. So in order to do so, age of student one, since we have already initialized the age value of the student one, you can access the age member using student one dot h, separated by a semicolon. So now this program will bring the age or student one. So let's run this program and see the output of this program. So you can clearly see here that entity output, it is representing age of 30 and one is equals to 16. So before printing out any values of this class or an object, you will first have to assign the values, do it. In the next tutorial, we're going to create functions that will, that will just calculate some monks of these students. So let's create another field here. And max, max of 01 and marks of student do. So let me just assign the marks of student one as 95. And similarly I'm going to assign the marks of student as lead, suppose API. So in the next tutorial, we are going to create a function in this, in this glass, which will just print out the sum of the values or the sum of the marks of student one and student two. We're also going to assign the values first, and then we're going to display it using a function, void display. So void display function, we'll just display the details of student one and student two. In this tutorial, we have just covered the members. In the next one, we're going to introduce you to methods or functions in classes. So thanks for watching. 6. Methods and Constructors in Java: In this tutorial, we're going to talk about, I'm going to introduce you to the methods and constructors in Java. So methods and constructors, they both are the functions. So let's just discuss about the methods first. Some methods are basically functions. So I can write here methods or functions. So function is used to perform some operations based upon some input, and then it gets some output. So if you give some input to function, it is more likely to give you an output like this. So we're going to create a function in Java and the unit we have seen in the previous tutorial via seen an example of a class student. We have created a class student. And in this tutorial, we're going to create a function in this class. And the function can have some return type, which is just the data type of the output. So it is just the data type of the output. So a function can have some return type and a name, and it can also have some input values. So we're going to create a function void display in this tutorial. So a function is defined in this manner. So this one is the return type of the function. And since the display function is not going to return anything, then I'm just marking it as void. But in some cases there will be functions which will be, which will return some values, like some integer value or some character value. So we're going to change this according to that. In the district function, you can see these two circular braces. Inside these braces, you can optionally write some input parameters. Like you can write int x comma y, like this. So you can write some input values in this band. This is, and this is the block of statements of the display function block of statements. So let's go to our Eclipse IDE and implement this function void display. So in the Eclipse IDE, we will create a function called display and we will use the object of this class. We will use the object of class student. And we will display the Details of this particular student using this by calling this function. So let's move on to the Eclipse IDE. So this is the program which we have written previously. So now what I'm just going to do is I'm going to create a function in this glass student. So I'm going to command here creating nerd. And the name of the method is void display. So the return type is void. And there are no parameters inside this function display. So this is not a parametrized function. So now what I'm just going to do is I will just display the details of the student. So I will first display the role. And similarly I will display the rest of the details. Lake, age, gender, age, gender. And finally marks. So this is the function which are all the methods I've created in the class Dunant. And we've already assigned some values do student one and student do. You can see the values that we have already assigned to these objects. So in order to call a function, in order to call this void function, we will have to use, oh, an object of this class. So here we have created two objects, student one and student do. So if you want to display all the values on, on all the members of this student to object. Then you will have to use this object to call this function display. So here is how you can do that. So now I'm going to write here display values of student a student one. So I will use the object student one, and then I will use the dot operator, and then I will call the display function. And since display function does not have any parameters, I believe these fantasies as empty. So this is how you're going to call the function display. So if you will call it using student one, then it will automatically display the values assigned to the student one object. So let's run this program and see the output of this program. So you can see here that the row number is 25, then Marx, 95, then 16, and male. So it has displayed the values or the members of this student one object. Similarly, you can also call this display function for these students do. And this is how we create methods in this, in our class. So let's create one more function. But this time, it is a parametrized function and it is used to calculate the sum, offload number and mux. So although it, this is a useless function, I'm just going to, I'm writing it because I want to display you some. I want to display how parametrized functions work. So this is a batter met arised function. So that we'd have to use a different name here, like INO. And here also will have to write some different variables Like MR gain. So what I'm just going to do here is I will create another variable, int sum, and I will assign it the value of our NO plus m alkane, like this. And this function will return the sum of these two variables. So what I'm just going to do is I'm going to on the student do, sorry, the display function using the student do. And this time we're going to go on the calculate function. So calculate function is called. And remember that you cannot leave the balances like this because it is returning some value and it has two inputs, so you will have to give two inputs, do it. So let me give the lowest number. As Guelph comma max will be like 75 or 70. 75. So I've given two values to this function calculate, which will just give the some awful membrane marks and it will store it in this sum variable and it will return the sum variable. So if it is returning the sum variable, I will have distorted at n some other variables. So let me just do this in some temporary variable. And then the next line, I'm just going to display the temporary variable. Like this. So let's run this program and see whether it is calculating or not. So you can see here that it has calculated the sum of wealth plus MD5, which is 87. So this is how our parametrized functions work. So let's suppose I want, do I want to give it the row number and marks assigned to the student do variable. So let's see how we can achieve that thing. So now I'm going to create a function which is used to update the details of one or of any student. So it will be a wide update function. Update details function like this. And I will give the newer values do this update function. So we will use the student one dot update details. And let's provide some values, newer values. So let's suppose the rule number will be 36 months, they'll be 50. Gender is since student one has gender as male. You can see here, it's order it to female. And finally, let's write the age of this student as 15. So these are newer values that we are going to. So we're going to update our student function, sorry deal. We're going to abate the members of the student for an object. So we're passing it to this function of date details. In the update details function, we will create variables row number. And we're going to use the same variables as we have used here. So I'm just going to paste all of them here. And instead of semi-colon and I will use a comma. So here I've created a function of date details which is used to update the details of the student object that is calling this function. And we're using the same variables as we have defined here. And when you're using the same variables, we will use this keyword. And this keyword is used to access the object that has called this method. So this keyword is used to access. The object that invoked this method. So we know that this method is invoked by the object student one. So really use this to assign it with some other value, which is given at the time of function call. So low number is 36. Then we're going to write this dot, dot, dot h equals h. And similarly I will write the gender and also the marks of the student. So here we have used this keyword which is used to access the object, object that invoke this particular method. And it is useful because we may have more than one object. So in order to initialize or to update the details of a particular student, we're going to use this operator which will access the student one object. Because spent one object has called this function void. Void update details that you can see here. So this is actually a function call. So we have called the function of date details using the student one object. We can create functions that can return some values. They can be used to update the details of a particular object. So this is how methods are created in Java. So now let's move on to the constructors past. So constructors are used to construct an object. So as you can see here, in this part, we're assigning the values to the student and the student to object. So let's suppose the programmer forgets to assign the values to a particular object. So let's create another object, which is Student 3 this time. And now vf, this from the programmer forgot to assign some values. The row number marks age and gender to this particular object. So this will make our program a little bit buggy because whenever we will, we will try to access the row number max, age, or gender or this object. It is going to give us the null values, which is going to crash our program because we have not assigned any values to the student one and student two. So in that case, you can. Leave the assigning. So these are, this is an important point, so I will write it here. So in case of constructors. So you can leave, you can leave the initialization, the initialization of an object at the time of its creation, and the buying of a creation. So basically, a constructor is a function that has same name as of the glass. As glass. So this is the first in constructors. And the second is that a constructor is, or I should say, a constructor is automatically coiled, then it's object is created. So what I'm just going to do here is I'm going to create N method here. And it is not a simple method, but it is known as constructor because it has the same name as that of our class. So the name of the class we'll student. And I'm going to create a constructor. And I'm not going to pass any battery does to it. So a constructor is called automatically add the time of object creation. So this basically means that whenever you will write this line, this line, it will invoke the constructor and the student constructor, since there are no parameters in these balances, it is going to invoke the student constructor that has no fat in meters in it. So you can also get some parameters to this function, which will make it more easier than assigning it like this. So let's suppose the programmer forgets to write some or to initialize some values to the student. So what we're just going to do is it will, this object creation will automatically call this constructor student. And it will initialize the members or the variables. To some default value like this. So gender is made. So these are the default values that will be assigned to our students in three object. So even if the programmer forgets to assign some values to it, it will automatically call this student function and it will assign the values to this object. You can use this operator to automatically so that you can reference it to the, to this place. So this constructor will be coiled whenever a particular object is created. So this is the main use of constructors. You can also initialize it with some values. So you can write some integer values here and here also. So it will match with the same number of parameters. So it will find a constructor with same number of parameters and then it will execute that particular constructor. So that's the main use of constructors in Java. So it's very useful to create a constructor in case when you want to write some default values to a particular object. So let's now display, Let's now try to display the details of this student object. So you can see that I have not assigned any values to this student three object you can see here, I've not exist any values, but since it will invoke the student constructor, it will automatically assign rule number max, age and gender to this student tree object. And then I will call the display function. It will automatically display all of these details. So let's see. So you can see here that it has a 100100, male and a 100. So this is basically how you create constructors. Or you can even create parametrized constructor. So you can actually pass values here as we have done in this case. So it basically works like a function. The difference is that it has the same name as that of the class. You can see here the class name was also at the student. So this is how you will create constructors and methods in Java. Next thing that we're going to study is a concept known as the Garbage Collection. Garbage collection. So let's suppose you have created a particular object or you have instantiated an object. So let's focus on this line, on this one. So in this line or this line, vf created an object student too. And we have used a new operator. And you can see that when you're using the new operator, it is allocating the object in the memory. So what will happen if our whole program will stop its execution? Then ideally this new operator, since it is, it has allocated student one in the memory, it is not going to release it until and unless you use some delete operator as they were using in the C plus plus. But in case of Java and Java will automatically destroyed. So that is known as the garbage collection. It is collecting the garbage. So it will. So Java automatically deallocates, automatically, deallocates an object allocated any memory, allocated in memory after program execution. So you do not need to use any code or anything. Java does it for you automatically, which is known as the garbage collection. So what if a programmer wants to lead? What if, let's say a programmer wants to run some code before Java automatically deallocates that object. So you can use a finalize keyword. I'm going to use def keyword. So this is basically a finalized function which is basically protected and void. And it is an L build function. It is not a user-defined function. So you can write this function protected void finalize. So whenever you, whenever Java will automatically try to de-allocate an object. So before de-allocating, it will run this function, run this mattered before de-allocating an object. So this is how Java, we can use the finalized function, which is an L bell function. We have seen the garbage collection, we have seen the constructors and functions. In the next tutorial, we are going to see the, we're going to take a closer look at the methods and constructors. We will cover some of the most important topics like we will see overloading, overloaded methods, then overloaded constructors. We will also see the final keyword, the use of the final keyword, pages of the static keyword, and the use of the access specifiers like public, protected and private. So thanks for watching. 7. Method Overloading in Java: So in this tutorial, we are going to talk about three concepts that are very important in Java. For swan is the method overloading or the constructor overloading. Then the second one is the use of static keyword and the yields of final keyword. We have already used static keyword and the public main function. It was static. And now in this tutorial, we will discover why we're making it as static, and we will also study about the final keyword. So first of all, let's take a look at the method overloading first. So first of all, I'm going to give you an example in which case you're going to use method overloading. So let's suppose we want to calculate area. I'm going to write here calculate area. So if you want to calculate area of anything, of any shape, then there are different possibilities. So I'm going to do, I'm going to draw some shapes here for which you will have to create a function that will calculate the area of that shape. So here are the three shapes. So the first one is a square, the second one is a circle, and the last one is a rectangle. So let's suppose that we, we want to calculate the area of these three different shapes. You can see that these shapes will have different parameters. For example, size of the length of this query is a. So the area of the square is given by the formula a square or a into a. If you take an example of a circle, we're given a radius. And to calculate the area of this circle, we will use the formula bi into R, into Our in case of a rectangle, we have length and the breadth. So the area of rectangle will become length into breadth. So you can see that all of these formulas are calculating there basically calculating the area of the given shape. But they, all, these shapes are using different parameters. As you can see here, one is using radius. Then this one takes two parameters, length and breadth. And this will take only one kilometer. So we will have to solve this problem. So. What we're just going to do is let suppose there are two programmers. So let's suppose this as a programmer one I'm going to write P1. And there is another programmer which is P2. So the programmer P1 does not know what is method overloading. So he does not know what is method overloading, but the programmer to knows the approach of method overloading. So let's see what the programmer one were you. So the programmer one will create, first, we will create a class shape. And then what heals going to do is he's going to create three different functions with three different names. For example, for the to calculate the area of square, you will create a function void area squared. And since squared is taking only one parameter, he's going to write here in a. And then he will perform the operation in this place. Then for the shapes circle is going to create another function with a different name as void circle. And in the case of silicon, we will have to AIG radius. So let's suppose it is a float value. So float radius. And then similarly he is going to perform the calculation here. So in the case of rectangle, you will again create a function which is void ADL rectangle. And again, he's going to pass two parameters, which is the length and the breadth. And then he will perform the operation here. And then in the main function here will create an object of this shape class and he will call, and the user can now call a specific function to calculate the area of square or circle or rectangle. But the programmer to, since he knows the usage of method overloading, he says that we can reduce this much complexity of using different names for a function which is doing the same thing and the same thing as calculating the ADL. So you can see that all these functions are, they're actually calculating the area. So the programmer to say that, okay, what I'm just going to do is I will also create a class shape. But instead of creating different name functions, I'm going to create a same function with the name wide area. So let's suppose you want to calculate the area of square. You will write a in this function. And then you will evaluate the area of square here. So if you want to calculate the area of circle, you will give the parameter float radius. I'm going to write it here. And then you will perform the calculation here. And similarly for calculating the area of rectangle, we are again going to call the same function area, but they are going to use two parameters in this case. So you can see the two approaches on this side. This is the approach which is done by programmer one which does not know what is the method of overloading, but the programmer to nose method overloading. So what he did is he used the same name in order to reduce the complexity and using different names and handling them in calling. So he used the same name, but he's calling them using different parameters. So that's the basic idea of method overloading, which says use the same name but with different parameters. So there are two ways in making these parameters different. False is the number of parameters. As you can see that in these, in this program, in this function, we are assigning two parameters. Whereas in this function we are assigning only one parameter. So now the compiler will be able to distinguish between these two functions. So this is the first way, is the number of parameters. But you can see that in these two functions, which is this one and this one, the number of parameters are same. So how will the user distinguish these two, these two functions? So let's suppose the user creates an object S of this glass. And now he wants to calculate the area of the circle. And so the radius is given. So let's suppose the radius is 5.39, ending with a semicolon. So now the programmer one ACE program I do away important quotient. He says that okay, you're going to create the same, you're going to assign the same name but different parameters. But how will the compiler or how will the user be able to distinguish between all these function? So the answer is that the compiler will check. The compiler will start doing the matching work at the time of the function call. So this is the function call. So this is the function call. And remember that the function call is done in the main function. So I'm just going to write here main function. So in the main function, whenever you will use the object of this glass shape and you will call this area function using one parameter. Then it is going to match it with that function which has the same manometer, the same number of parameters. So now in this case you can see that the number of parameter is one. So there are two different, there are two different functions, but they have the same number of parameters, which is one. So how will the compiler distinguish between these two function? Then answer is that it will match it using the data type of the parameters. So data type of parameters. So you can see here that in this case, we're passing the value 5.39, which is a float value. So what the compiler is going to do is he's going to match the area function which is accepting the parameter as float. So the compiler will call this function. It will reject these two functions. So this is the basic idea behind method overloading. We're going to use the same name to get because they have the same purpose of calculating the area. But we are going to use different parameters, different number of parameters, or different data types of parameters to call a particular version of that function. So you can, you can implement this in the IDE Eclipse ID and see how the compiler will call the particular function. So you can also create some more function calls like as dot area. In this time you can provide it with two values, night 79. So it will call this function. Because this function is accepting do batter meters. And they both are of integer types. So now it will calculate the area of the rectangle. So that is known as method overloading. So since I have already created the program in Eclipse ID, so let's take a look and let's run it. So here is the program. You can see that we have created a class shape. And these are the three functions that we have created. One is to calculate the area of a triangle, then squared, then circle. And you can see that all of them are having same name. But different number of parameters and also different data types of the parameters in the function. You can see here I've created an object of this glass shape. So now that's called. So let's pull the humans to calculate the area of a square. So you can see that when I will write, as it is already showing me that you can call any of these three OR functions you can call the area of load radius function, which we'll call this one. And you can similarly call the area in a, which will calculate the area of the square. And similarly, this function will calculate the rectangle. So this is the basic idea behind method overloading. So now let's suppose the user wants to calculate the area of circle. So we'll select this one. So I can write here. And let's give it a float value. So I've graded function call using this. So let's make it 3 first. So let's calculate the ADL squared. So you can see that there are three areas of functions, but now it will call the function ADR, which has Item 1 number of parameters, and the data type should be int, so it will call this function. So let's run this program and see the output of this program. So you can see that the output is area or square is nine. So it has called this, this one. So this is how function overloading works. Similarly, if we want to calculate the area of the circle, Let's decimal value. And since this is a double value, so I'm going to write here. So now it will call this function also. And similarly you can also call the area function that do different parameters. So let me just call the function also. So that said, so let's run the final program in C, what will be the output? So as you can see that oiled the respective functions. So that's how function overloading works. You can apply the same concept in the Constructor overloading, since constructor is also a method, but the difference is that you do not need to call it like this. It will be called automatically when it's object will be called. So what you will have to do is in order to distinguish different constructors with the same name. You're going to write these parameters or the different types of parameters in this, in these braces. So it will just, so the compiler will just match it with the respective constructor. And it will call that constructor. So the same idea. So let's now talk about this static keyword. So you can see that this static, it has been applied to this main function. So we have discussed the most basic thing, which is that if you want to call a function, you first have to create an object of this class. And then you will have to use the dot operator to call that particular function. You can see that this function is called because we have created an object of that class. And we have used a dot operator to call that method. But you can see that this is also a class think X and it has a main function. Main function is also same function as atria or like that. It is also a method, right? So you can see that we're not creating any object of this class, thank x. And we're not even calling this main function. It is called automatically. So it is called automatically because of this static keyword. So static keyword is used when you want to call a particular function or when you want to allocate the particular function in main memory so that it can start executing. So you will apply static in front of it. So if you will apply static in front of a function, you do not need to create the object of this class and you do not need to call it like this. Writing static in front of four method will just, will, the compiler will just executed. Without even you do not need to create the object or you do not need to call that function. Similarly, if you will apply static in front of any variable. So let's suppose you write, you will create a variable static and a equals n. So then you will apply static in front of a variable. Then this means that this variable a will be assigned in the main memory without, even, without even calling this the object of this class. So you can see here that in this class, area function five. Alright, very able. Indeed, this shape function, let's suppose I create int b comma c. So if I've created two variables in this glass shape. Then remember that these variables will not be assigned. So I will write here not located in main memory or in the memory. So if you will not create an object of this globe us shape, then it will not allocate. There's these two variables in the main memory. So if you want to allocate a particular variable in the main membrane and the need to allocate. Why do we need to allocate a particular variable in the main memory? And the reason is that without allocating a particular variable in the main memory, your program cannot use that variable. So since we're not creating a new object of this class, thank x, we have applied static in front of this variable int a. So whenever the compiler will run this class, it will automatically assign the very variable as 10, and it will allocate it in the main memory. And we know that for every program to continue its execution, the variables and the function should be in the main memory, then only you can execute it. So that's the main use of the static keyword. The next keyword is the final keyword. So I will write it here. So let's suppose I create a variable, int b equals 20. And you can see that I have written final here. So final basically has two functions. In this case, it makes very able table D as constant. So using final keyword is just using CONST, which is the constant keyword in C plus plus. So it will make this variable final. That means it is constant, its value can not be changed. So this is one major important use of this final keyword. Another function which is, which we will, we're going to discuss it and the chapter inheritance. So if you will Live final in front of a variable, as of now, you can just understand it as it will be evident as a constant. The second US comes in inheritance. And in the next tutorial we're going to cover the inheritance topic. And we will discuss how we can inherit a particular class. We will see how this object oriented approach of inheritance is implemented in Java. And you can see that this method overloading is using same name, but they are performing different function is an object oriented approach which is known as polymorphism. So I'm going to write here dimorphism using same name but different parameters so that I can have different, this is polymorphism. So basically polymorphism is method overloading. Or you can implement the polymorphism Object-oriented approach using method overloading. So that's it. Thanks for watching. 8. Inheritance in Java: So in this tutorial, we're going to talk about inheritance in Java. And we're going to study very important concepts in Java. We're going to study the basics of inheritance. Liberals, Jedi, member access specifiers, then method overriding than using Final with inheritance, then the constructor's execution. So first of all, I will give you a very good example of an interview. And we're going to use that example to show all of these features of inheritance. So first of all, the cushion that comes in our mind is, why do we need inheritance? So why inheritance? So here is the example. So let's suppose there is an interview and we have an interviewer. So here is an interviewer. And let's suppose there are two programmers that have come for the interview. One is the programmer P1, and another one is the programmer P2. So let us consider that the programmer p When does not have knowledge of inheritance, whereas the program or P2 is with the knowledge of inheritance. So the interviewer starts asking a question from the programmer. One, he gave a, he, he gives a docx to the programmer one. He says that let's suppose we have class student defined like this. So you can see that this class student has some member variables. You can see that here are four variables, which is row number, marks, gender, address, and a function display, which displays a detail of the student. So the interviewer says, it gives a talk, he gives a docx to the programmer, one that you are given this plus Dunant. And now you will have to create a class, DO N'T amends. And this class that will have all the variables. And this function display also. So this class to know them and we'll have all of these members and member variables of the class student. Plus it will have, it will have some more features like it will have one extra variable which is N2 domains, and one extra function which is void, calculate donor Mains. So the programmer one is given that this does, so what he does is he simply write all of these functions as it is here. And he adds the extra feature, do it. So let me just write the whole program here. So as you can see in this code, we have all the members which were also a label in this class student. And we have an additional member end moments here. We have the void display function, which was also there in the student class. But in this class we have an extra function which is scapulae t. So the interviewer gives a remark, do the programmer one. He says that you have already initialized all of these variables in this class. And now you're again initializing it Hill. So this is basically a duplicate copy or you can say the duplicate code. So this duplicate, this gold that is having multiple instances. Or you can say that this is a duplicate code, so it is going to take a lot of memory space. You can see that if this function display is a very long function, you will have to create that function again in this class. So this poses a problem because you're initializing them again in this class. If it is already there here, you can see that these members and this function is already available in the class student. You're again doing that. You're again creating them in this class. This makes your code duplicate. The programmer to is given the same task and he knows the concept of inheritance. He explains the concept of inheritance to the interviewer. He says that let's suppose we have a parent class, or let's suppose we have a parent. So in inheritance, let's suppose we have a payment which has some features. And features are basically the members or the member functions. So now if we want to create a child, or let's suppose this parent has a child or let suppose it has two children. Now what these two children, two children will have some features that they have inherited from the class or from the parent. So let's suppose if we have a patent which has some hair color, skin color, then the child has some of the features of the parent plus some more features of their own. So this concept of real life inheritance can be applied into programming, which is an oops concept. So it comes into an oops concept. So you can apply this inheritance. You can see that a here I've explained the inheritance and that's what programmer P2 has explained to the interview. The programmer to can easily see that in this case, this class student is acting as a parent class. And this class is acting as a subclass or the derived class or the child class. So this class student has features, some features, you can see that they have some variables and some member functions. Similarly, there is class two moments and it has some additional functions and additional member variables. So basically, so basically this class tournament is a child class which is going to inherit some of the problems of some or all of the properties of this class Student, and it will add some more features to it. So what the programmer P2 does is it uses inheritance. And if you want to inherit the members of a particular class, but you will have to do is you will have to use a reserved keyword, which is extend. So extends basically means that you are going to extend the features of the student class. So now you're going to write the student class here. So what basically we are doing here is we are inheriting the properties of student. So whenever you will create an object of this class, students of this class do Nimitz in the main function. You can use that object to access these functions and variables. So you do not need to redefine a rewrite them in this two nodes class. What you can do is just write the additional features in this class. So what I'm just going to do here, I'm going to write here and two moments. And I'm going to define the soul void calculate functional. This is wide. Calculate two elements. And here you will display the two domains. So you can see here that we have made the code more compact by just extending our base class. So the parent can also be known as a base class or a superclass. And the child can be known as a derived class or a subclass. So here you can see that instead of writing all of these members and redefining all the functions again and again. In other classes, you can extend the features of a particular class using this extends keyword. So the programmer to has made the code more compact and efficient. So now the interviewer has, I guess programmer to another dusk. He says that I do not want that this mocks field. You can see that this is the max. So let's suppose we have a main function here. So let's suppose this is the main function. And inside this main function, let's suppose I create an object of this class. B equals to new domains. So what you can do is let suppose you want to call or you want to call this display function. So the easiest way is to just use the object of this class and user dot operator and then display function. So you can see that we have not even created this display function in this class. But since it is extended, it extends the properties of the class student, it is visible in this class do not emit. So you can actually use the object of this subclass or the child class to call the member functions or the members of those super or the parent class. So now the interview interviewer gives a task to program it to. He says that if you can access the members of the superclass, then that means that you can also access the marks of the student class. So you can do something like this. So p dot max equals to, let's say 20. So you can use the object of the subclass be to assign the marks of this student. But the interviewer says that it should not be like this. He says to the programmer to that you will have to do something so that the outsider or the user who is using this whole program will not be able to assign or acts as this max variable using the object of this subclass. So then there comes the member access specifiers. So the programmer P2 is well-worth with the member access specifiers. There are basically three types of member access specifiers. They are also known as the visibility mods. So I'm going to write here three specifiers, which is public, private, and protected. So you can use these any one of these, these are also the reserved keywords followed by a semicolon to make a particular variable as private, public, or protected. So let us discuss about each of them. So if you have declared any particular variable as public, let's suppose I have written public int row number. So this means that this rule number variable is accessible inside this class. It is also accessible in this class also you, so you can access this row number using the object of the inherited class or the child class. So the programmer, since it has given, it is being given a task to hide this member, this member Marks. So what he's going to do is he's going to make it private. So once you have made this as private, it cannot be accessed by any of the subclasses. So if you have made this variable as private. You cannot assign using the object of the subclass. So this is, this will just give you an error. So in this manner, you can also specify or you can restrict the object of this class to, or the object of the class to avoid using or assigning values to the variables of the superclass. So this basically means that if you will assign any member as private, you cannot use it outside this class. So it is accessible to the functions. Let's suppose this is a void display function so it can access this match variable, but the two numberings class cannot access that variable. So let us discuss about the protected one. So let's suppose I have made the gender as protected. So whenever you declare a variable as protected, then this means that you cannot access this variable outside this class. You cannot use the object of this class to access the gender variable, but you can inherit this variable inside the subclasses. So that means that the two nm and class can make use of this gender class, but in its own function. So if you have made any particular member protected, then means that it can be, it can be inherited. But it becomes private in the subclass. Private in the subclass. So in this manner, if you will, right? If you will declare any variable as protected, using the protected member access specifier or the visibility mode protected, then you cannot use that variable outside this class. That is actually a private also. But you can inherit this variable in this subclass. So you cannot inherit the private members. You cannot inherit the private members. So these are the public, private and protected member access specifiers. Now, the interviewer gives another task to P2 or the programmer to. He says that I want to define a function display in the class do limits. But this function display. Has its own implementation. That means that this function will display low number, Mach, gender, and address, which this function is also doing. But in this Fung, in this class, in the subclass, it will also display max, sorry, the number of donuts that a student has played. So we want to create a function which has the same name as it has in the, in the superclass or the parent class. But we want have a different implementation. So the interviewer asked me to that if he will write a function here that whether its own implementation in this class. So what would happen if you will call the display function like this? Which display function is this line going to call it? Is it going to call this function? Or is it going to call the function that is there in the parent class? Because you can access this Baden class function also because you are extending the properties of the student. So the answer by programmer to is method overriding. So whenever you will create a function called like this, it will overwrite the base class. It will overwrite the base class. And it will run the function which is displayed by the object of this class. So you can see that the two laments class has object D. And whenever I'm using the dot display function, it is not going to take this display function into consideration. It is going to override this. And it will display, and it will just call this one, which is in the subclass. So this is known as method overriding, and it is a runtime polymorphism. So I am going to write your runtime polymorphism. And we know that the concept of polymorphism is very simple. You are going to use the same name, but with different implementations. And it is runtime because this overriding of function of the base class happens when your program is actually running. Because the calling of the function will happen only when you are running your program. So that's, that's the answer from programmer to now the interviewer asks a problem or ask a particular question from programmer one. He asked that what will happen if you are not going to extend this class? So let's suppose we're not extending this class, right? So what the programmer one has done is. Programmer one has done this part. So he says that let suppose in this code I want to change the address of the student. So the programmer one says that if you want to change the address of student, you can access, you can create an object of the student class, and then you can change it using a call like this. But there is a problem because String address is also there in the class do limits. You can see here. So this is another problem. If you're not using inheritance. You will have to abate each of the variable or the members in all the classes. But if you are inheriting your AKS, you can, you just need to update this variable and it is visible in all the subclasses. So now the programmer, now the interviewer asked another problem from the programmer to, he says that now I do not want this function to run. He says that the display function defined in the class do moments. I don't want this function to run. I want the function which is, I want the display function which is in the superclass to L. So how can it do that? The programmer says that you can use a final keyword. So if you will use the final keyword in front of this function, then add the runtime. Runtime, this function will not override, so you will be able to run this program. And so no implementation will be there in the subclass. So once we have written final infant also a member, you can make it constant. If you're making, if you're making a function as final, that means you cannot override it. That means it cannot have further implementations in the subclasses. So this is the second use of finite the 30 years of file. You can write final in front of a class. So then you will write final in front of a class. You cannot extend its feature. So you can use Final Four, three things. One, you can use it to make a particular member as a constant so that no other code can change its value. Then you can use it in front of four methods so that it will not get override at the time of execution. As you can see here. The third usage. If you do not want a particular class to get inherited or two, if you want to prevent inheritance, you can write final in front of it. So that's the use of final keyword. Next the programmer to, as given another task by the interviewer. The interviewer says that, okay, you have created, Let's suppose we have created some constructors in the class student. And we have also created some constructors in the class do limits. So if I will create an object of this class tournaments, it should be like this. So if you create an object of this class to an image, then which constructor will be called first? The constructor which is there in the class and the subclass, or the constructor which is there in the superclass, which will be called first, then the, then the programmer to gives a very intuitive example to the interview. He says that let's consider a construction of a house. So if you want to construct a house, you will first have to construct the lowest floor, then you have to construct the next row, and then you go up like this. So the construction works like this. So similarly, you can apply this concept in computer programming if you want to construct the objects. Or let's suppose you want to create a constructor in class student. So the constructor of the base class is called first. And then the constructor of the derived classes will be called one-by-one. So that's how constructors execution takes place. So in this tutorial, we have covered inheritance concept, some of the very important concepts which is use of final keyword method overriding, use of member access specifiers. So I will suggest you to write this code and play around with these method or the member access specifiers. Try method overriding and use for, try to use final keyword and see what will happen, which function will run first. And in this manner, you will be able to master the concept of inheritance. So thanks for watching. 9. Packages and Access Specifiers in Java: So in this tutorial, I'm going to D2 packages in Java. And so we're going to learn why packages are useful in Java and how packages provide some of the feet. Here's do Java. So basically a package is a container for pluses. So I'm going to write here a package is a container for classes. So let's suppose you have created your own Java project in the Eclipse IDE. So let me just show you here. So you can see that this is our Eclipse IDE and we have created a project named S2. So in this project you can see here that this is our final thank x dot Java. And you can see that in the top of it we have written that this belongs to the back gauge test. So if you explore this test module in the source, then you can see that there is a test package here. You can see the symbol is for package. So if you just write their care. So you can see here that this symbol is for package. So basically a package is just a folder that contains all of your Java files or all of your classes. So basically a package is a container, or you can say, you can even say that a package is basically, it's basically it encapsulates your glasses. So you can also say packages encapsulates classes. So you can know that classes also encapsulates data and the gold similarly packages and gaps relates classes. So someone can ask like, what is the use of package then? Why do we need to have a container for all these classes and why do we need to encapsulate classes? So the answer is basically, there are two reasons why we need packages. One is to resolve naming conflict. Resolve naming conflict. So let's discuss this first one here. Then we will move on to the second. So let's suppose we have created a project, and in the project we have created a file. Let's suppose the name of the file is student file. Or I will write student dot Java. So let's suppose I've created this file in our project, and let's suppose it does not belong to any package, right? So. If you have created a file named as student dot Java. So let's suppose there is another project created by some other programmer. And incidentally, he has also created a file with the same name. He might have created a student dot Java. And let's take it to the Internet level. So let's suppose on the Internet there is a programmer who creates a file student dot Java. And similarly from the other side of the world, there is another programmer who grades of file with the same name. So that is going to create a naming conflict. So packages basically create a namespace, namespace of classes. So basically namespace means that package has the names of all the classes. So packages are unique for each and every project. So if you want to create a file in a particular package with this name, you cannot create the same file under this package name. So let's suppose the package was test. So this means that our file belongs to the package test. And so you cannot create another file with the same name. But let suppose there is some other package that belongs to some other programmer, which is named as, let's suppose it's Test 2. So it can have class or a file with the same name. So you can consider package as a folder that contains a file with this name. And you know that you cannot create two files of the same name in the same folder. If you want to create the same file, you will have to create another folder for it. So that's the main use of package. That's the first one which is to resolve a naming conflict. So the second one is to introduce access control. So as we know that in classes there are members and the member functions. So if we want to restrict some other object of some other class to access a particular member, we can use a private variable in front of it. So we have actually covered the member access specifiers, which are the public, private, and protected. So I'm going to write it here. Public private. And predicted. So you see that you are. Java basically provides a security feature to a class so that no one outside of the class can access a particular member if it is private, right? So similarly, Java introduces packages because it wants that some other programmer or some other package which has a particular file should not be able to access the contents or the data of this other file. So let's suppose the student dot Java has some members like int a, b, c, or d, like this. So we do not want the, some other package or some other project to access this particular file because it will be a security breach. So that's why packages introduce, introduces access control. So if you will write public, private, or protected in front of some variable or a function, or in front of a class. So let's see what will be the impact on that particular variable function class in, with respect to packages. So let's see, I'm going to draw here a table. So I'm going to illustrate you how these member access specifiers are used to basically prevent some security breaches inside a particular package. So here is the table. So as you can see here that I have divided this table in some columns where I have mentioned the member access specifiers, which is the private, public protected and default. So basically the default cases, the case where we're not actually writing any one of these member access specifiers. So that does make a difference from the public access specifier. So let's see. So in the first two rows, you can see here in these two rows, we are considering the same package. And in these three rows, we're considering the different packages. Sorry, in these three rows we are considering the same package. And in these two we're considering the different package. So let's take a look what will happen if I will write private in front of a variable or a function, or a class. So let's suppose I am writing your private end a equals 10. So in the same class, you can access this variable. You can even change the value of this variable. So you can actually access this variable a if it is in the same class. So if you have a same package, but you have an extended class or a subclass, you cannot access the private member in that subclass. And in the non subclass also, you cannot access this private variable. So you can access the private member access specifier only if it is in the same class. If it is outside the class, outside the class, you cannot access it. So that's the case with the same package. So let's suppose there is another package or another program and it has a subclass. So in that subclass also you cannot access the private member access specifier or the private variable function or class. Similarly, if you have a different package or a different program magnet has a non subclass or simply a class, then also you can not access this private variables. So you can see that if you are assigning private in front of a particular variable, you are actually restricting it to the same class. So that's basically a good security feature. So let's move on to the public access specifier. So we know that we have we're using this public access specifier since the beginning. We're using it in front of the main function. So when we're using public in front of the main function, we can access that function in the same class. Considering that it is in the same package, we, you can also access this main function in the subclass and the non subclass also. So if you have created some other class, you can access the main function because it is public. So even if I write a variable public and x equals x equals 12. So you can access it in the same class, subclass or non subclass. And you can even access these variables and functions even from outside this package. That means that some other program can also access these variables or functions. So the question arises here, why we are using public in front of the main function? The answer is that the JVM is an external program or external machine. It's an external environment. And if you want to access a particular function which is in some package from outside the package. So you will have to make it public. That's the main reason we are using public in front of the main function. So let's move on to the predicted one. And you can see here that protected is different from the private. You can access the predicted variable in the same class, in the same subclass, which is the extended class or the derived class. You can also access it in the non subclasses. You can even access it in the subclass of different package. But you cannot access a protected variable in the non subclass off some different packets. So let's suppose this different package has some another class, which is not a derived class, so you can not access this protected variable from that. So that's the main use of protected variable. So let's move on to the default case. A case where we're not assigning any value to a variable function or a class. So all these concepts apply on variables and functions and class. It's same. You will have to, you don't even need to remember this table. It's obvious, and when you will use this, you will use a programming job. You use Java in your career, you will get to know when to use private, public, protected, or when to use a default case. So let's suppose in the default case I assign a variable y as 13. So you can see that I have not mentioned any number access specifiers. Or in that case, you can access this variable in the same class of the same package. You can access it in the same subclass, same package. You can even access it in some other class, all of this, some non subclasses of the same package. But you cannot access this variable Y, which is the default case, and the different packages. So remember, there is a difference between using public and not using any specifier. So if you're using the default case, you will not be able to access the particular function or a particular variable or a particular class from outside this program. That's why it is necessary that you write public in front of the main function so that the JVM can access that a main function and when your program. So in this tutorial, we have seen the main use of packages. We have seen how it resolves a naming conflicts and how it provides some security features so that some other programs can not indirectly or directly make a security breach in your programs. So in the next tutorial, we're going to cover the interfaces. And in that tutorial we will learn how interfaces provide features in Java programming language. So that's for it. Thanks for watching. 10. Abstract Classes in Java: So in this tutorial, we're going to learn about the concept of abstract classes in Java. So here you can see in the egrets ID, I have created an example here. You can see here that this is a glass shape and it had a function which is the wide area function. I do not know at this moment when the shape will be. We don't know how to implement this function. So I will just write here area not defined. So if you will see here that I've created a class rectangle, square and a circle class. And all these glass, all these glasses inherits properties from the ship class. So we're going to do here is I'm going to overwrite this function area and all these glasses, or I should say, in all of these subclasses. So the problem arises here. So let's suppose the programmer forgets to implement this function area. Or I should say the programmer forgets to override this function area in a particular function. So even if you will run this program at this moment. So whenever you will create the object of the tangled glass, and then you will call the area function. Then it will, even if it does not have the area of function, it is going to call the area of function of its superclass. So what we want here is we want to feature that will just, that, we'll just restrict this rectangle. Two must implement or do must override this moiety of function. Because you see that this class will have no use if it cannot calculate the area of the rectangle. So what I'm just going to do is I'm going to use the concept of abstract classes. And abstract classes are basically classes that have at least one abstract method. An abstract method is a method that does not have any implementation. So that basically means that an abstract class is made to be extended. So it is made to be extended. And the method, the abstract method inside the abstract class, is made to be overridden or to be implemented by the subclasses. And if there's any subclass folders to override the abstract method, then it will throw an error inhale. So let's make this class abstract by using the keyword abstract, like this. And you can see here that if you want to make any class abstract, adjust it as just an abstract class has just been simple glass, but it must have an abstract method. So let's make this area function as abstract. And remember that this area function cannot have any implementation. So I'm going to write it like this. So now you can see here that I've created an abstract class and we're extending this abstract class. And when your class make, it will be mandatory for the subclass to implement the stride third and C plus plus. This method is known as virtual method or a pure virtual function. In this, in Java, it is an abstract method. So you can see here that it is showing an error here. And you will have to implement this or you will have to override this function area. So if the programmer now forgets to implement or to override this function area, then his program won't run. So that's the main use of abstract classes in Java. If we want to make sure that each and every subclass of the superclass. So let's suppose there is a superclass here and it has some subclasses, just like in this example. So if you will create in, you will have to read abstract method in this abstract class. And on the subclasses, it becomes mandatory for all the subclasses to overwrite this function area. So that's, that's the main concept behind the abstract classes. In the next video, we're going to cover interfaces in Java. And we will see how interfaces provide a very high speed or in Java. 11. Interfaces in Java: So in this tutorial, we are going to study about interfaces in Java. So basically we have already heard this word interface. So GUI is graphical user interface. So basically interface is something which connects to different environments or which allows one employment to connect with another one. So if we take example of GUI, which is graphical user interface, then there is an interface that connects user do the graphics. So if the user will implement graphics or if it implements the interface, it, he will be able to access the graphics of the computer system. So similarly, interfaces in Java also provides some functions which are abstract by default. So if you want to create an interface, you will have to use this keyword interface, followed by the name of interface. And this interface view. You can only create abstract methods. No need to write strapped in front of them because they are abstract. My default. One interface, basically the use of interfaces for, let's say, we want a particular class or a particular subclass. So let me create a glass rectangle. And I want to say that this rectangle must implement some of the features or some of the functions in this interface. So let's suppose there is a programmer who forgets to create some area function inside this rectangle class. So he wants to avoid that and that thing. So what he does is he created an interface and he says that this class will implement this interface. And implementing this interface, all the functions or all the abstract functions inside this interface will be overridden here. So if you, if any, glass months to implement a particular interface, you will have to use the keyword implements, followed by the name of the interface. And now you can see here that rectangle, there is a red line over a panel here because we have not overridden this white area function, since this function is abstract and also the public by default. So we're going to create, or we're going to override this method public, void function. So this method is public. So this means that any other class or in some other package can also implement this interface. So now I will just, I can just perform some operations here. But at this moment we're just going to bring in that it is the area of function. In interface. So that's basically the use of interface. So if you want to add some more functions in a particular class, so you can do this. So by implementing an interface. So an interface you cannot create any instance variables. You cannot even create an object of this interface. So let's create an object of this class rectangle like this. So if I recall the area of function, it will just display the area of functioning in interface in the output screen. So the basic difference between abstract glass and interfaces that in an abstract class you can create. You can even create functions that are concrete. And concrete functions are those functions which have implementation. But in interphase you can not do that. So if you tried to do some thing like this, an interface, it will go and they're system.out.print. Hello. So you can see that it is showing that there is an error because your interface cannot create concrete functions, but an abstract law, glass can do that. In interface, these functions are abstract by default, no need to specify the abstract keyword and they are also public in nature. You can create an instance variable. And since in this shape interface, that's one major difference. And there is one more difference. So if you want, if you have gradient and abstract glass and you have extended it, or you have derived some more classes through it, then all the classes will have to override the functions of the abstract class. But in interfaces we can selectively do that. So even if there is a subclass, so let's create a super shape. So I'm just creating this class super shape. And let's suppose this is a subclass. So I can create a subclass of this super shaped glass. And you can see that you can do the inheritance and implementation of the interface at the same time. So you can selectively just implements these functions inside these interface by just writing this, these towards elements and the name of the shape. So that's the main use of the interfaces in Java. And you can use it in a lot of ways. So that's it for this tutorial. In the next tutorial we are going to study exception handling. So thanks for watching. 12. Exception Handling in Java | Part-1: So in this tutorial, we are going to examine Java's exception handling mechanism. So let me first define what an exception is. So an exception is an abnormal condition. So exception is an abnormal condition that arises in our code sequence. That arises in our code sequence or that arises in our gold at runtime. So there are two things that you should focus in this definition. One is the abnormal condition and the second word is the runtime. So basically an exception. In other words, an exemption is a runtime error. So it is a runtime error in short warps. So in this tutorial, we are going to study how Java handle the exceptions which are encountered at runtime. So in most computer languages that do not support exception handling, errors must be checked and handled manually, typically through the use of the error codes. And, and this is a cumbersome or a troublesome procedures. So Java's exception handling or moist these problems. And in the process brings runtime error management into the object oriented world. So basically you, what is a Java exception? A Java exception. A Java exception is an object. So Java exemption is an object that describes that describes that abnormal condition. So if you have an Java exception, it means that it is an object that will particularly describe that exceptional condition. Exceptional condition. So let's suppose you create a program and it will run at the compile time. First, it will be compiled by your compiler. And that code at compile time does not yield any type of error. But at the runtime, let's suppose the program is taking some user inputs. And let's suppose the user at the runtime gives some inputs that will yield some error in your program, then that will be an exception because it is occurring at the runtime. And so Java exception handling, or Java manages these runtime errors using these keywords. We will write our program and we will see what these keywords actually do. So they are the try, catch, throw, then throws. And then there is finally. So in this lesson we're going to cover, try and catch. In the next part we're going to overthrow throws. And finally, so basically an object that represents that whenever an exception condition arises at the runtime, and object will represent that exception, right? So what Java does is whenever it encounters a condition, an abnormal condition or an exception on the runtime, it creates an object that represents that exception. And that object particularly describes what that exception condition is. So exception can regenerate it by the Java runtime or can be manually generated by our code using dry and catch. So briefly, here's how it works. So if you have some program statements, so let's suppose you have a program and you have written some statements in it. Let's suppose you have written five statements in it. So when you have return statements in a program and you want to monitor that whether these statements will contain some exception in it. What you are simply going to do is you're going to put them in a dry statement. So dry and followed by the curly braces. So now we want to monitor these statements, whether these statements contain an exception or not. So if these programs statements can behave abnormally at the runtime, then what we're going to do is we're going to catch that exception using the catch keyword, followed by two circular braces. And in these two circular basis, we're going to mention the exception name. So in Java, by each and each and every exception has a name associated with it. And since it will be a class with the exception name, you're going to create an object. And then you can perform some operation in the statement. Right, So we're going to, we will write a program in a later bit. We'll run it and we will catch an exception also. So we will see this in this, in the Eclipse IDE. But before that, I want to show you a very important top-level DOM Level exception hierarchy. So I wanted to show you the top-level exception hierarchy. So while exception types are the subclasses of abate in class throwable. So there is a superclass through eval and it contains all the exception that can, that can occur in your program, right? So this superclass contains the names of all those exception. And does this throwable is at the top of the exception class hierarchy. And immediately below this table there are two subclasses that partition exception into distinct branches like this. So on one branch there is the error which is encountered at the compile time. And on this side there is an exception which is encountered at the runtime. Right? So these are the two branches. One branch is headed by exception and this class is used to for exceptional condition that the user programs should catch. And this, since this is a runtime exception, there is a subclass of the runtime exception. And there are similarly a lot of classes subclasses of this exception superclass that contains all the exceptions. And we're going to refer them in here. Right? So now we know what an exception is. We know the exceptional heirarchy. So now let's take an example of knowledge. Program it and let's see how a program can encounter an exception or an error in the runtime. So let's move on to the Eclipse IDE now. So let's create two variables in here, a and B. And what I'm just going to do is I'm going to write this program where it takes input values from the user to input value is basically one is the A and another one is the B. And when it will take these two values, then we're going to print the way we're going to divide these two values, right? So Endo value a. And then we're going to write enter value b. So if you want to take an input from the user, you will have to use the Scanner class. And to use the Scanner class, you will have to import java dot util dot scanner. And now you will have to create an object of this scanner class like this. And in this, you will have to provide it with the constructor, that is system dot n. So now we have created a scanner class which will take input from the user. So we're going to assign this input to the value a. So we're going to use the scan object dot next int. So next it in function is basically used to convert the, whatever you're going to write in the console. It will convert it into an integer, since a is also an integer. So we are going to do the same with the value b. So you can see that in this program we have not done anything much. We have just create data Scanner class, which is taking the input from the user at the runtime, right? So after taking what we're just going to do this, we're going to print the division of the two values here, like this. So let's run this program. When we will run this program, I will also write, I will input two values and let's see, oh, well this program run correctly or violate some error. So you can see that this program correctly. We have in Word, we have taken the value of a as 34 and BS do. So it gives an output of 4, 17. So now what I'm just going to do is you can see that this program runs fine in the little diamond by time. Now, I'm just going to do is let suppose there is a user who impulse the value of b as 0. And we know that anything divided by 0 is not defined, right? So let me just run this program again and input value 0. And then we will see how this exception, because this will happen in the runtime here. So let's see how Java will automatically handle this exception. So you can see here that when you enter the value of b as 0, it gives, it throws an exception in the thread mean, which is an edit medical exemption. And it says division by 0. Exception name is division by 0, sorry, exception name is arithmetic exception. And this is an object which describes that exception as division by 0. And you can see that it also describes where this exception is occurring. It is appearing in the test package. Think x is the name of the glass, main is the name of function. And on 1914, which is this line, this error and this exception has occurred. So this is how Java will report the error in the, in the runtime. But now what we want to do is you can see that this looks ugly. It villages. If you will run this program in the production line, it will look very ugly because the user will not understand this exception, right? Because VR programmers, we know what this exception means, but maybe the user will not know that. So what I'm just going to do is since we want to monitor these statements, I'm going to put all of them in the dry block. So you can see I've put all of them in the try block. All these dates are in try block and we're going to monitor all of them. So whenever this, we know that this line number 15 as giving the exception, arithmetic exception is the name of the exception. So what we're going to do is the unknown when do gadget manually. And we know the name is the edit and take exception. So the name of the glasses arithmetic exception. And we're going to assign it to, we're going to take a reference E. And this line, we're going to take that value of b. So let's run this program and see how it will work. We are monitoring these statements in the try block and whenever the JVM will encounter any exception, it will throw that exception. And whenever it throws that exception, this catch block will catch it, and it will store it in the exception e. So you can even add this exception e in here. It's a value of 0, cannot be, value of b cannot be 0. So let's run this. So you can see here it says value of b cannot be 0. And it also E is pointing is the description of this exception and it says division by 0. So this is how you dry and you catch exceptions. So let's suppose in the state miss, there is some more exceptions. So you can also gadget more than one exception by introducing all gas statements after this. So let me just create an example here. So what I'm going to do is I'm going to create and adding. And I'm going to assign some values in this array, one comma two or three or more for like this. And I'm going to make an editor here. We known that I'm going to assign the value at index position 7 as 34. We know that we have created an array which has four elements in it. So the index positions are from 0 to three. So there is no index position as seven here. So this will be an runtime exception. So let's see what will be the type of this exception. Let's run this program. So you can see, okay, so it does not guard that exemption. So let me write it like this. So what I'm going to do here is instead of writing this in the dry date and I'm going to write it off than the dry gas statement. So let's run this program and see. So now you can see here that it has got an exception in the main class which says add a index out of bounds exception. So now we know that it is at a index out of bounds glass. So it describes that index seven is our phones for loop. But as you can see here, the ADA was having length as four and you're accessing the seventh position, which is index out of bounds 0. So what I'm just going to do is I'm now going to replace it in the try statement, and now I'm going to catch this exception. Name of the exception is the egg. At a index out of bounds exception. We're going to store the reference in the e. So here I'm going to write, I'm a man, I'm manually gathering this exception and I will write here. Add a index, or I should write here. Cannot. Axis was Zetian, Wu Zetian seven. And after this, I will display the exception here. So you can see here we are now catching exceptions in the same try block. So in the try block, you have some problem statements which can have more than one exception. So this is all you're going to catch these exceptions. So let's run this program and see the what will happen in the output. So now you can see that it is same value of b cannot be 0. So it has not caught this exception. Maybe because I have not closed these scan, not goals will have to close it. So you can see here that it is only displaying the arithmetic exception. And the reason is that it will break at this line. It will not even come to this line. So a signal, there's arithmetic exception in the output. So now you can see here that when, whenever you encounter an exception, the JVM will not run the statements after this statement. So now you can see here since there is no error, no exception in runtime on this line, it will come to this nine and it will catch this exception, The ones. So this is how you can catch more than one exception in the try-block one. Another way of doing this is you can also create a nested Dr. Block here. And what you will have to do is you will have to Gadget after this exception right here. So you can create a nested dry gas statement also. So it will dry this also. So let's run this. Okay, so we will have to write it. So yeah, it is same, but it is nested. So it will be easier for us for any programmer to know which part of the problem statement is actually yielding some exceptions. So this is our, we try and catch exceptions in Java. And it is very important to hand these Egan's because you know, these exception will occur at the runtime. So if the user is running your program and he, and there is any error at the runtime, it will lead to more crashes. So this is how we have done that Brian catch statement blocks. And now in the next part we're going to learn three more keywords, which is the true throws. And finally, they all are very important, so that will govern them. Using our program. We will use a program in the Eclipse IDE and we will run it. So thanks for watching. 13. Exception Handling in Java | Part-2: So this is the part of exception handling tutorial. And in this tutorial we're going to cover three keyboards, which is throw, then throws, and the keyword. Finally. So we're going to cover these three key words in this tutorial. In the previous tutorial, we have already created this program which make use of dry and catch blocks. And you can see that there are, there are nested try and catch blocks. And in this tutorial, we have already discussed how we can actually catch a particular exception which was captured by Java runtime, right? So we know how Java runtime creates an object or an instance of that exception and throws it, and we will catch that exception in the Gas statement block. So let's first study about this finally keyword. So let's suppose now your program has executed this whole dry block. It has found some exception, and now it has run this catch block also. So let's suppose now you want to run some few for programming statements are some more statements. After all the try catch blocks have been executed. So after all the try catch blocks have been executed. So now you can mention here the finally block. Finally means that it is going to run for the final time, right? So after all the pride gas statement, if you read finally keyword followed by Earth's own block of statements. You can run some block of statements after all the try catch blocks. So this will be useful in a lot of cases, right? So this is the final run. So now let's take a look at the toe keywords. So we know that whenever there is an exception, exception which is basically n, which is basically a runtime error. So whenever there is, there is any runtime mirror, our job, our JVM, I'll just throw that error rate. So JVM will only throw an error if it exist. So a programmer can throw an error by creating an instance of that particular exception by using this throw keyword. So this can be done explicitly. So let's suppose after the, after in this strike statement. Let's suppose I want to do. Explicitly throw an illegal state exception. So first I will create an instance of it. Illegal excess exception like this. So now after these whole dry gans, after all these statements, now it will, it will just throw this illegal access statement, sorry, exception. And let's write some information of this exception in here. Like this. So talky what is used to explicitly through a particular type of exception for that you will have to use the new keyword so that you will, so that this, the object that this description will be allocated in the memory. So let me just write this after here. Okay, so since we are throwing it explicitly, vivid also have the gadget. That's why it is showing red line here. So we will have to catch this exception and we know that this is an illegal access access exception, e, like this. And I'm going to just vent out using the system.out.print line. I'm going to write here illegal access exception thrown explicitly by programmer, right? So a programmer can explicitly throw a particular exception even if that exist, even if that exception does not exist. So you will have to catch that exception also using the catch block. So this is the manuals of throw. So now let's move on to the last keyword which is throws. So throws keyword is basically used to mention whether a function is throwing a burglar exception. So what I'm just going to do here is let's create a function. It is a static wide net say, test function. So we have made this functions that leg because we're not creating an instance of this class. So static, the compiler will run this Destiel function as soon as this program is run. So static void function will throw this null pointer exception. Right? So I'm going to Create an instance of this null pointer exception. And let's describe this as null pointer demo. Right? So you can see that this function is a Destiel function. And the grammar is just throwing this node pointer exception using the throws keyword. So now let's suppose there is this function mean makes a call to this function. So next, make a call to the function. Since the desk tool function is throwing an exception. So you can see that this is our main class and main class is calling this Destiel function. So what you will have to do is you will have to explicitly, you will have Dan function is throwing an error because this function does not know how to handle this error. So it will just write villagers going to use throws keyword. But just to mention that this particular function is throwing or is get baby bubble of throwing a Baltic. Let x n is a null pointer exception. Right? So this desk drawer function is throwing the null pointer exception. So now what you will have to do is this is the function. You have to create a Dr Block for this Destiel function. And you can now catch this null dot x and in the gash block. So now you can see that how we have used the three key words through throws. And finally we have used finally so that it can run after all the dry gas statement. We have used throw keyword hill to create an instance of a particular exception and to throw it explicitly even if the exception does not exist, right? So an using Clause, which we are using here of VR mentioning that this, if any function, we'll call this desk drawer function. So again, see that main function is calling this Destiel function. So this function would have to specify that this function is throwing a NullPointerException. You can see that here. So now the main function, and we'll be able to handle it using the try catch block. And so what we're going to do is we're going to display. So let's say this is an E. So I'm going to write down this. So let's go and see the output of this program. Okay, so there are some errors in this program. Okay? So there is an extra, okay, so here we have made a semicolon. That was the era. So now you can see that at first throws the index out of bounds exception, which is here. Then, since we have all this illegal access exception demo. So you can see here that a legal access exception is thrown implicitly by programmer. And you can see that after all the try-catch block, the finally block as R1. And after that, we are calling the NullPointerException. And you can see here that it has displayed this NullPointerException. So that's the main use of throw throws. And finally, so in the next tutorial, we are going to study multi-threaded programming, which is a very important concept. And we will use this concept of multi-threaded programming in creating applets. So thanks for watching. 14. Java Collections: Linked List & Stacks: So in this tutorial, we're going to talk about Java collections framework, which is a very good framework and a very useful framework. And then this framework, this collections framework, Java provides some of the implementation of the data structures. For example, linked lists, stacks, queues, maps, HashSet, HashSets, hash tables, T-shirts, etc. So in this tutorial we are going to focus on LinkedList and stacks only. And in the folder coming parts we will discuss about some more of them. So in Java collections framework. And basically if you don't know about what exactly data structure is, so let me just tell you what data structure exactly is. So let's suppose you have some data that suppose you have some integers. So let me take some random integers, 4, 5, 10, 13, 11. So you have these indigenous and these are basically data. So if you want to store this data, you might need to perform some actions on that data, right? So you might need to get some values from this data structure. So a data structure basically holds data in a specific, particular pattern or a manner. So we're going to talk about two data structures in this tutorial, which is linked list and stacks. So basically we're going to use the Java collections framework to implement LinkedList and stacks. You can also implement these two data structures using just memory references. But says that is a complex task, and we have already done that using C plus plus. So now we're going to see and explore how you can very easily create and perform some operations in these two data structures. So let me first define the first one which is linked list. So basically LinkedList is a collection of data or the data is known as notes. So let's suppose you have these integers. So now you want to represent it as a form of a linked list. So as the name suggests, it is a list of data linked together. So we are going to link these nodes using memory references. In C plus plus, we create pointers, which is the next pointer, and it points to the next node like this. So basically in any other, but if you're not using the collections framework, you will have to create memory references and you will have to walk around a lot of stuff. Like if you want to add some value to this node, you will have to find the accurate position for it where you want to add it and you will have to manage a lot of stuff. But if you use the collections framework and we are going to use the Java dot. Util dot asterisk. So we're going to import this util package, which basically provides, which has already implemented and these data structures. And what we're just going to do is we're just going to make some calls, some function calls, and the work will be done. So let's move on to the Eclipse IDE and implement our first data structure using the collections framework, which is linked list. So here we're going to demonstrate linked lists stacks using collection framework. So first thing first, import java dot util dot class. So we're going to import this hole. You can say this package. So now what we're going to do is if you want to create a linked list, you will. Since vf imported this util package. Now you can easily create a linked list class like this. So let's name it as L, L using new operator, link list integer. So you can see that this is basically a lead. This linked list is basically a class. And you can see that the difference here is, you might notice that we have written this extra thing in danger in these two arrows. So basically this is a generic class. Generic class and gender clauses. Basically, glass, which, which you can use to implement data structures which have different data types. So let's suppose you want to create a linked list of integers. You can actually specify that this linked list can only add individual nodes in the linked list. So this is basically a generic class. So you will have to specify n, these two symbols. So very simple. So now when you write this line, it will create an empty linked list. So what now i'm, I'm going to do is I will add some nodes in our LinkedList. So in order to add the link list, you do not need to create any membrane offenses. Or if you do not need to do any stuff. Because it has already been done by the Java programmers. And now we're just importing those packages, those glasses. And now we're just going to make some function calls to add some values to these linked list or stacks to these data structures. So first, we're going to use the dot operator. And you can see that when you will write dot after that, you will see there are a lot of recommendation here. Like get our ad and there are a lot of recommendation that it is showing us remove. So basically this linked list generic class provides a lot of inbuilt functions which have been already defined. And what you are going to do is you're just going to call that function and Julie will have to provide it with suitable batter meters. And it just works like magic. Your LinkedList will be created without even managing anything. So let me add some integers using the this add function. So you can see here that I have inserted for in our LinkedList using the add function, which is a predefined function. So I'm going to right, you're adding elements in our linked list, right? So now we have added of four in our linked list. And you can see here that I have not implemented this add function anywhere. I have not implemented the membrane area fences. I have not done any complex tasks because that has been already done in this linked list grass, which is placed in this java.util package. So I'm just going to call this add function to add some more elements inside this inside of a data structure which is linked list. So let me add some values, right? So I've inserted five values to this linked list and then you will add these elements in the LinkedList, then they are added and the form of mainList, that means that if you are on for node or you are on a node which is having the value four. So the next pointer will be pointing to the next node, which is 5. So they are in order, right? So four isn't inserted first, then five, then ten, then 11, and then 13. So if you want to display this linked list, or if you want to display contents, or I should say nodes of LinkedList, then it is very simple. You do not need to create any function or anything. You'll just have to write system.out.print l n and inside you will just have to pass the linked list, which is L, L in our case. So you can see here I have created a LinkedList. I've added some values to it and now I'm printing it. So let's see, Let's run this program and see the output. So you can see here that it has created a linked list which has values for five, 10, 11, and 13. So we have actually implemented a data structure linked list without even writing any functions on, without even doing any implementation of these functions or doing any complex dots. So collections framework is basically a very intuitive framework which helps programmers save a lot of time. So, so the programmer will not be not doing the most of the time. They will not have to implement these functions. They have already been implemented and since these are the most basic data structures. So that's why they have been added and the collections framework. So let me show you some more functions that you can implement. So let's implement this ad first function here. So this ad first function, as the name suggest, it will add the node at the first position. So let's say I want to add 12 at the first position. So I'm going to write it like this. And let's implement one more function which is add last function. And let's add a d seven at the end of our LinkedList. So let's run the program and see the output. So you can see here that 12 has been added to the as the first node of our LinkedList than 45 10111387 is added on the last node. So you can use a lot of functions too. You don't need to implement this function and you can see you and I have not implemented the ad first and add last function. They are already present there, so you can use them. So let's suppose I want to get the value of a specific node. So let me create a variable a. And let's suppose I want to get the value of the third node, right? So I'm going, you can use the dot get function to get the value at the third node. Remember to write the LLC, which is our object or an instance to the LinkedList generic class. So now we will just display the value of a. So let's run the program and see. So you can see here at the third position, the value of the node is 10, which is 0, 1, 2, and 3. So in this manner, you can get a particular node. You can even remove a particular node from the linked list using the remove function. And there are similarly more functions than that you can implement. So this is basically our example of link list data structure. Now we're going to move on to our next data structure, which is stacks. All right, so let me first give you an overview of what exactly a stack is. So now we're going to discuss about stacks. So let me give you a real life example of how the stack data structure looks like. So let's suppose you have a tube or a container like this, which is open from one side and it is closed from the other side. And let, I will give you some balls which have numbers on it. Let's suppose the balls are 4510, then 13, and 11. So no more I'm just going to do is I'm going to give you a task of putting or do insert these balls inside this tube. So let's say that this is a stack. So what you are now going to do is you're going to first insert four, then you are going to insert five. Similarly, you will insert 10, then 11, and then 13, right? So one by one, you are inserting into this stack. Insertion in a stack is known as push. So there is a push function which you will have to implement. But since we are using the collections framework and we do not need to implement this push function. We're just going to make a call to the stack generic class function push. And then it will automatically push the value given inside this function push, and it will insert it into the stack. So let's suppose I say now I gave you a task of removing this fifth ball, right? So I am saying that you will have to remove this ball of which has number 51 it. So you know that if you want, you will have to remove this ball. And you will first have to remove the balls 13, 11, and 10. So then only you can remove this fifth ball. So this removal or deletion from one stack is known as a pop. And we're going to call the pop function in our collections framework. To just remove a particular data or a particular node from our stack. So you can see that Bob function pops out or removes the element which is at the last position of our stack, which is, you can see here in this case 13. So if I will just call the pop function, it will remove 13. Then it will. Then elements remaining in the stack will be 4, 5, 10, and 11. Again, if you will call the pop function, it will pop out 11, and this goes on and on. So a stack is basically a data structure which follows this rule, which says first in, last out, right? So the element that you have inserted first is going to become the last element to come out of the stack. So if you have inserted for first, it is going to become the last element to come out of the stack. Right? So what we're going to do is we're going to use the collections framework. And we will add, sorry, we will add these data or these nodes in the stack. And then we will call the push function and pop function. And we will see how this actually works in the Eclipse ID. Okay, so in order to create a stack data structure, It's very easy just create a stack generic class. So let's now provided with some other data type that this stack data structure can hold. So ever done string here. So stack, let's say SDC k equals new stack. Being a generic class, I will write here string. So now you can see I've created a stack, generate glass, so I'm going to write here that it is January glass. So it provides a general mechanism or a template so that the particular data structure can be implemented using this datatype, right, so let's add some value in this stack. So in order to add, if you want to add some dimension to this tag, you will have to remember one thing, which is if you want to insert something into the stack, you would have to use the push function. There is no ADH function. We are going to use push function, which pushes an item into the stack. So let's say we're going to insert hello as the first string item. So let's push some more items into our stack. Hello, world. So I've just inserted these are just pushed these two strings in the stack. So if you want to print out the contents of the edge is very simple. You just have to write this thing. Islands. So I'm going to write here display contents of the stack, right? So here we are adding elements. So I'm going to write here pushing elements into the stack. So now let's run this program and see the output of this program. So you can see here it has created a stack with values hello and world. So this is how a bush function books push function is basically the insert or the ad function. And you can see here, even in this data structure, I have not implemented the push function because the collections framework which exist in this yielded java.util package has been already implemented. So you can see how much Compact our program becomes and how much easier it becomes for any programmer to implement data structures without worrying or implementing these insert functions or push function or pop function. So let's create the pop function. Let's call the pop function and see what will be the output. So then you calls even you will call the bump function. It will have to bop out any element from the stack and inserted in the last. So now the result should be Hello inside the stack. So let's see the output. So you can see here that now it is saying just hello because we have called the pop function. So if your stack is MB and then you try to call the function, it is just going to display on NullPointerException, so you can handle that exception here. So in this tutorial, we have covered the java.util dot collections framework, and we have covered LinkedList and stacks. In the next tutorial we are going to cover kills, HashMaps, HashSets, do research and some more basic Fung, some more keywords like iterators and split graders. So thanks for watching. 15. Generic Classes in Java: So in this tutorial, we are going to study generics in Java. So basically, you can use generics. Using generics, you can define an allegory term or a data structure once. And then you can independently specify the data type that, that particular algorithm or data structure well-implemented. So using generics, you're going to find an algorithm or data structure ones independently of any specific datatype, and then implement them, do a wide variety of data types without writing the code again and again for different data types, does it reducing the gourd and NZ or deeply guessing? So here in this tutorial, we are going to implement generics in Java. And we will see how VK can implement a particular glass, the DLT. Not even in one, using one data type, but we will buzz different data types and we will run the program to see how we can handle a particular glass and by busing different data types. So that basically says a lot of, um, a lot of gold. So let's implement of glass. So let's first create a glass. And let's name this as generic glass in generic. And we're going to write it like this. So you can see here that I have written these two arrows and is t root n in it. So basically this d is the reference type which we will pass. Then we will create an object of this class. So let's create an object of this class. And we will create an object of this generic glass of generic class. Right? So in order to create particular object, we are going to use generic. And inside this, we're going to pass this battle may be an individual. And then we will create the object using new operator, then generic, and then again in digital. So we will also create a constructor and we will pass some number in that constructor. So you can see here that I have created a generic class. And this generic class, we have ridden in danger inside these two arrows which specifies that this glass they'll run using the integer reference type. Please note here that we are not using primitive data types here. Primitive datatypes our end. But here we're using the integer class. So basically Integer class is a class. So I will just show you what integer does. So you can see here java.lang.Object deja. So this integer class wraps or value of the primitive data type end in an object. And we are going to pass this object to our generic class. So this integer keyword is basically it is a class and it wraps the primitive type int in an object so you cannot directly pass an integer value. You can not pass primitive types here, right? So we are going to use these integer, integer, which is known as the reference types, and we're going to pass it and the reference will get stored NP. So now we're going, we're going to create a reference to this integer value. Remember that this individual class cannot, you cannot create an object of this integer glass. You can only create a reference object of type int. Right, so let's now define a constructor and our gender class, generic. So I'm going to write here DO, which means that the object is 18, the reference is 18, and D is basically integer. So what I'm just going to do here is I will just assign or B as o. And then I will create another function which basically returns for B. So OB is basically our reference object of IPE end. So now we are going to return it. The return type is said. As B. Let's name it does get OB. And here I'm just going to write simply object OB. So you can see here this, we will call this function. It basically returns our OB reference, which is assigned as all. And all is given at the time of object creation. And this generic object, sorry, this Generate Constructor will be coined, which will assign or B as o. And this get OB function will return the OB reference state. So now we're going to create another function which is the show type of the function. So this function basically displays the type of see whether it is integer string. So here I'm just going to brand of B. And in order to get the type of B we will use, or B dot get class, dot, get name function, get name function. So if you want to know the data type of a or what is the type of b, then you can just call the dark glass dot getName function which returns the type of p. That is whether it is a integer reference type on a string reference type or maybe offload reference type. So now we're going to all the functions here. So first I will make a coil to the short-time function, which will show me the type of the object that we have created of the generic class. So it will return java.lang integer because the type of end. So now what I'm just going to do here is I'm going to assign a value as o b, j dot get OB. So get OB function is used to return the type or the reference, which is Basque, add their time of object creation. So it is a deed in this case. This means that the value will be equal to ADA. So if you want to perform some operations inside this glass in this generic class, you cannot do it directly here. You will have to return the reference type. And then you will have to create operations here, which means that you cannot directly. Oh, just display or directly add two integers. Do integer reference type of objects in this generic class. So very simple. You can see here we have just written one single chord, one single class, and we are just passing the reference type at the time of object creation. And by passing the reference by VR, actually saving a lot of code duplication. So we have used the show type function to display the type of our object. And then we will display the value that this function get OB, the leg down. So I'm going to print the value here. So let me just take another example where we're going to display the value. There. We are going to pass the reference type as a string. So what I'm just going to do here is I am going to create an object, generate glass. And this is of type string, right? So this was of type integer. So I will just create an object of this generic class passing the type which is string, in this case. And here I will just create an object OBJ. And in the constructor, I'm just going to pass the string, which is helloworld. So I will now use this object of this generic glass to display the type of this object one night. So all be J1, NOT gate or B, which will return the string. And we're just going to stall this string in a string variable. Let's call it as value one. And now we're just going to display the value one. Right? So a very simple example to implement generics. You will use generics in data structures while implementing data structures in Java. Because if you create a single database on a single algorithm, you will just have to create one glass effect. And by calling this single class using the generic, the reference type, you will just pass the reference type and that particular glass val implement itself using that particular datatype. So a very efficient program, and it makes our program more compact. And that's the main reason why we use engender x in Java. Remember, in order to prove perform any operations inside this direct class, you will have to. You cannot call it till after return the value and display it inhale in the main function. So I've already run the program and see the output. So you can see here the output is type of D is integer in our first case, and it has returned a date. And then the second time it's string and it has red done string. So this is how generics work in Java. 16. Lambda Expression in Java: Let us start with the lambda expressions in Java. So basically lambda expressions, it is a feature which was introduced in Java version and it was so much demanding that most of the programming languages will have lambda expressions in building C plus the C-sharp, anyone biotin has lambda expressions. So today we're going to see the significance alpha lambda expressions in the Java programming language. And how the lambda expressions heads or making our program a concise and little bit more understandable. So the core concept of Lambda ex-patients is basically do override the abstract method defined in an NGO phase. So before starting the lambda expressions, if it is crucial to one less than what is interface. So if you don't know what exactly an interface in Java, you can check out the video that I've created and I've given the link to that video in the description below. So first of all, we are going to create an interface, and I'm going to call this name of this interface as executable. Now, this interface is going to have an abstract method. And basically an abstract method is a method which does not have any implementation edge, just abstract, right? So I'm going to Command K. Packer does an abstract method. So this means that whenever we are going to create an instance or basically a reference to this interface, we will have to implement this function execute, because here it does not have its implementation. Now, this interface, since it is having only one abstract method. That's why it is known as a functional interface. And lambda expressions are basically applied to functional interfaces. That means the interface should contain only one execute function. All right, so now let's create a class. We're going to create a class, and let's name this glass has and running the US. And inside of this glass, I'm going to create a method which is the random method. And inside the run method I'm going to pass the interface, which is executable. You can see executable e is actually a reference to this interface. So inside of this function, I'm going to call the execute function, something like this. So if we will create an object on this glass running. Let's just make it public. All right, so if you will create an object of this class running and you will try to call this function run from by using the object dot run statement. If you do that, you can see it will call the execute method. Now remember the execute method in the interface. It's not defined. So at the time of function coil, using the object, you will have to pass the implementation of this method. So that's what I'm going to do in the main method or in the main function. So first of all, I'm going to create an object of the running class. Let's call it as one new running. All right, so now we're going to call the run function. So you can see if you will not pass any pattern, we don't do it. It will show you that there are some actual fixes for this. So it is asking you to pass the executable interface. Their friends do that, right? So you can see that this function will take a bottom middle. Now inside that, you have to give the battery meter and you will also have to give it the implementation of the execute method, since this is an abstract method. So the way to do here is by just writing the new executable. And then you will write this, this. What you just need to do is after this new execute David. And then you will have to just hover on this executable keyword. And you can see it says one quick fix available and just click on the Add unimplemented methods. So on clicking this, you can see that it is, it has actually created and it is overriding the execute function. So we will have to provide the implementation here, right? So let's write the implementation. We will call that execute. All right, so in very simple words, and we have naughtier to use the lambda expressions plus we're going to try it without the lambda expressions. Then we will see how we can reduce this program, do a lambda expression. Now, focus on this, this whole block of statements. You can see in the run function, I am actually giving it an instance of the executables interface. And I have the overnight this execute function because x unimplemented. All right, so now if I run this program, Let's try to run this program. And you can see in the output it just says execute, execute function call, right? So it's working now. Now the problem that this program is, that this whole thing which we are bossing is really, you will have to get a lot of things. For example, you will have to write the names, all these functions. Then add these keywords and edge, sorry, the headache grapes. So we can actually use lambda expressions instead of this. So now we're going to use lambda expression, which will reduce our world of defining or implementing the abstract method defined in the interface very easily. So let's see how we can do that. Again, I'm going to call the run function. Now, instead of writing the whole thing, we're going to use a lambda expression. Lambda expression looks something like this. What do you just need to do is use do circular brackets, then a dash, and then a symbol. This is the greater than symbol. And here you will just have to write whatever you want to implement. And since it's a single line, we're not going to use semicolon. And here I'm going to write lambda expression. Now let's understand what this whole thing is. Now you can see that instead of writing this, which is new execute, execute, what lambda expression does is lambda expression straightforward. It just says that, okay, this is the function, an unnamed function. So this is actually going to call the execute function. This is a lambda expression which will drive to implement the execute function. So it's like we are willing to start just from hill, right? Without providing all this information, we are just starting from here. And then since the function is going to give some output or implement some block of codes that you can see here. We're going to do that after using this operator. As you can see I've done here. So you can see that using a lambda expression, this is indeed an expression. Using this expression, I can implement unimplemented methods, abstract methods of any interface. And it's so much easy to do that. Rather than this, you would prefer to do and write something like this. Now, one more thing is that you should remember that you will have to use a function in cafes. So you should have only one abstract method. If there are more than one abstract method. Using a, you cannot use the lambda expression. Now, we're going to play out with some pedometers. So let's suppose you have this execute function, but instead of wide, it is returning some integer, right? And let's say we are also giving an integer inside the input to this function. And in the run function, since we are, we're going to pass some, indeed a value to this abstract method. Let's pass to us. All right, so you can see here now, it shows that though and execute method must override or implement an edge because you have not returned. Now it's taking some parameter int a, and instead of this, we will have to write N. And we will also have to return something, right, Let's returning. So let's come to our Lambda function. Now you can see that this is not going to work because now we are taking, we are considering the bottom meters and the return type is also there, so we will have to return it, right? So what I'm going to do is I'm going to just faced this line here as a comment that we have implemented this lambda expression. So inside of this, what I'm going to do is I'm going to pass the baton meter. And then I'm going to return something like this and it should be in pain right. Now, you can see that VR actually passing a VR actually using this whole thing here. Now you can see here it says it is not applicable for the arguments. So we will have to create some, you have to do some tinnitus, right? So instead of using this, I'm going to use a block of code. You can also use a block of statements. So if there is implemented, if you want to implement the execute function and it has multiple lines, you can actually use a block of statements inside the lambda expression. So now let's try to print Lambda. And since this lambda is going to return something, I'm going to write down something like this. And now you can see that our code is perfect and we are passing the baton meters here. And we're also returning the pattern we behave. Now, let's see if we can just write a here. If you will try to write a, then it will, again. It will also work at this time also. So let's try to run this and see the output. They can see it says lambda used. And this is what we're going to pass. Now, instead of writing return a, we also have one more choice. We can actually use a literal or a constant. Instead of this, I can simply write eight, right? So what will happen is this lambda expression. It basically says that you are giving a as the input parameter and the return type is eight, or the returning ready constant is a dried. So this is our another implementation of the lambda expression. I'm going to base your tail. And let us try to run this again. Okay, So it, it is actually returning it, but it is not displaying it. Because of our implementation. We're returning it, but we're not displaying it. So now this is also a lambda expression and we can see that inside this began actually mastered like this. Now, remember if you have a single input, you can actually do something like this, right? So it's really great. You can see that instead of using this much of code, you can use this single line of code. So if you have single parameter, single input parameter, you do not need to use the circular parenthesis. If you have multiple parameters, it will definitely have to use those circular brackets. So these are the lambda expressions. Now, let's say you want to pass and drive to do something like this. And this time we will definitely right Gulf of statement. Here I'm going to write lambda. And since we are using multiple statements that we'll have to use the semicolon now. So now we're given two input parameters. So here also I will change it to two meters. You, if you have multiple parameters, you can do that. Now, We're just trying out with the individual parameters in the input. You can also give some object reference on some other battling meters like load also. So instead of doing this, Let's pass some more values. And now we can see we will have to return dual variables. Let's return for Palmer. Fine. Alright, so we can't do it like this. A comma b. All right, you should remember that the output is actually a single variable. So it should be a plus b. All right, so this is how you can use the lambda expressions. And remember, if you are actually going to use these expressions, it is going to definitely make a difference in your program. So that's all for this tutorial. Thanks for watching. 17. Multithreaded Programming in Java: In this tutorial, we are going to learn about multi-threaded programming. So we will discuss about how we can actually perform multithreaded programming in Java. So this tutorial is basically for multi-threaded programming in Java. So we will discuss some of the dome still needed don'ts for the implementation of the multi-threaded programming. So we will just discuss in this part of tutorial. So this is the first part of this tutorial. And in this one we will just discuss about multi-threaded programming. In the next tutorial, we are going to start our, our programming part. And we would program this multi-threaded programming in Java. So let's start with the formal definition of multi-threaded programming. So basically multi-threaded programming is, it consists of the word, so if you break this whole dome, it consist of two terms, mighty and threaded. So we're going to see what exactly is a thread. So first of all, let me write down here what exactly is multithreaded programming. So the definition of multi threaded programming says that a multi-threaded program of my be traded program contains two or more parts, contains two or more parts that can run, can guarentee, that can run concurrently. So what does this means? So a multi-threaded program than Danes, two or more parts that can run concurrently and concurrently also means simultaneously. So I will write down here simultaneously. So let's understand this whole definition. So let's suppose we have a program and I'm going to represent it as a box. So let's suppose we have a program and it has some lines of code in it. So I will draw some lines in it. So let's suppose this program contains six lines of gold. So what we know about this program, that if this is a single program, the execution of this program x from the first line, do the last line. So first-line will get executed and then the second line will get executed and it moves on sequentially. So this is the general flow of execution in a program. So if I want do, if I will say that. If I want to divide this program into parts, Let's suppose the first part take three lines, and the second part takes the remaining three lines. So now this program contains two parts. This is the Part 1 and this is the part two. So these are basically parts of a program. So now we know what is multi-threaded programming. It contains two or more parts, but the condition is that they can run concurrently or simultaneously. This means that these three lines of this part and the three lines of this part can run simultaneously. So what does this mean? This means that if the first line, so if I will say that this one is the first line, if the first line of this part is executing, then the first line of this program can also execute at the same time. At the same time. So this is basically multitasking. You can see that two parts of the same program are running simultaneously. That means that we can actually run two tasks at the same time. So they can actually run concurrently or simultaneously. So people most oftenly they'd get confused the term multi-threading with multitasking. So basically, multitasking is basically running two tasks at the same time. So multitasking is of two types. One is the trait-based and one is the trade based, and another one is the process-based. So we need to understand all the terms that we're going to define in this tutorial because these are actually needed to write our program. We will have to know the technical details behind the multi-threaded programming. So multitasking is of two types. One is trade based on, one is process-based. Process-based means. It contains they're dumb process. So let's take an example from our real lives. So we know that we have mobile phones. And in mobile phones, when we go on to the recent apps, we will be able to see that we can run more than one app simultaneously. We can run more than one app or application simultaneously. So we can actually switch to any app. And they are actually running simultaneously. So this is multitasking, but this is process-based. This means that a process is the whole program. So process is basically a program in execution. So this means when two or more programs are running simultaneously, this is known as the process-based. But if the bars of a program are running simultaneously, it is known as trade based. So these are entirely two different things. So let's see which of them is most preferable and why we are using multi-threaded programming and not a process-based multitasking. The reason behind this is that when we do process-based multitasking, when we do process-based multitasking, it takes some overhead because the communication between the processes, we'll be very, will take a lot of resources and we'll discuss it in more details when we will discuss about processes in operating systems. So I'm not going into the details of what exactly intercommunication between the processes. So let's move on to the next important thing that why multithreading. So the question that comes to our mind at this point is why do we need to implement my day trading? What are the advantageous? So the advantage of multi-threading RAM is to write efficient program is due, right? Effecient programs. Efficient programs that make maximum use of that makes maximum use of processing power of a label in the system. Processing power every label in the system. So as we all know that the systems, which are the computer systems or any other system like mobile phones, we know that they have a good processing power nowadays. So if you run a whole program in a single execution, it will not make use of the whole process in power. So our systems now support multiple course. This is again, another dome which we do not need her, but I will still give you knowledge of it because it will make you understand more about multi-threaded programming. So systems contains multiple cores. You might have heard that the Intel processor is of quiet goal or alcohol. Quad-core means that it has four cores. Octagon means that it has eight goals. And it basically means that it can harden four threads at the same time. So if a system can actually perform so many threads at the same time, why not make use of this processing power? So we can actually use this processing power to make our programs effecient. And again, it really depends that whenever you are writing a program, we will have to be careful in which system you are deploying your program. Because if your program does not support multi-threaded, you cannot use multi-threaded programming that scenario. So this is just the technical details behind the multi-threaded programming. So let's discuss another important thing about threads. So now whenever I will speak this dome thread or whenever you, you will hear this dome thread, you knows that the person is actually talking about the part of a program. It's not the whole program, it's just a part of a program. So we know that two or more, there are two threads. One will run this apart and another one we'll learn this might end. Both can run at the same time. So let's discuss a very important topic at this point, which is the thread states. So I'm going to write here thread states or the state of our thread. So a trait can have multiple states and stages basically means what is the current position of the thread or what is the execution of the thread. So trade is a part of a program. So we have some v can actually specify the state of a thread. So let's discuss the basic trade stage. The first one is the running state. Running state. And running state basically means that the trade, which is the part of the program, is currently running. So we see that the thread is in a running state. Then there are some more states. One is the suspended state, means that the program, sorry, the thread has been suspended by the CPU or by some other program. So it is in a suspended state. Similarly, there is a resume state. Resume state means that the threat or the part of the program has been resumed. So it is not actually executing, so it isn't a resume straight, but it can come into a running state after making some calls. All right, So this is, there is one more state which is known as the blocked state. And blocked state means that thread cannot, actually, it is blocked. And a trade can be blogged when waiting for a resource. So I'm going to write L. Then a thread is waiting for a resource. And resource basically means memory. So whenever you heard this word resource, it basically means memory because the resource for a program is memory. So when o particular thread or wants a resource, when it is waiting for a resource, you can actually block it. And when you block it, it will no more be waiting for them. For that particular resource. So now we're going to discuss another technical term, which is the thread priorities. Thread priorities. And the reason why we are discussing these terms is because we are willing to use these terms in the program so we can actually change the state of a thread whenever we want to, because there are cases when we actually need to stop a trade or I spend it or to resume it. So we will have to discuss about the states then only we will be able to implement it in Java. Similarly, it holds true for the trade priorities. You will have to know about what is a thread priority then only you will be able to use it. So what is its fate priority? So Java assigns thread priority, which basically determines, so a trade priority basically determines how that thread, how that thread should be treated, should be treated with respect to v, with respect to others. For example, in our daily lives, we use the word priority. We say that we're going to give priority to over steadies, over gaming or something like that. Which basically means that I'm saying that I am giving much priority to the steadies than or with respect to others means that if there is some other part of the program or threads executing, I can actually assign a priority, do that trade. So this will be useful in a lot of cases because if a program contains some parts, we might need to, we might need to assign the priority. So a higher priority means that that thread will be executed before any other trait. So this is known as the thread priorities. So as an absolute value or priority is basically meaningless. So a higher priority thread does not run any faster than a lower priority thread if it is the only thread running. So this means that if there is only one single thread running, if there are not multiple threads running. So a high priority thread is basically meaningless. So now let's discuss one more important term which is known as context switch. And this term is used in a lot of places context, which is also an important term in operating systems, and it is mostly used in multitasking. All right, so what is context switch? So whenever we assign a priority to a thread, so abstract priority is basically used to decide then to switch from one thread to the next. So I'm going to write here Ven, do switch from one thread to the other, from one thread, do other. So the reason why we are actually assigning priorities is because we can actually use the priority queue which we have assigned to a thread to switch from one thread to another. What this means is, for example, there is a thread in a program and there is another thread in a program. And I say that this part of the program should, this thread, that thread which is running, this part of the program is having a high priority. Which means that this trend will post it has higher priority and this one has a lower priority. And this browning simultaneously, but this one has a higher priority. That means it will get more resource on Edwin. It will be assigned the memory or resource first by the CBO. So let's suppose I say that now, I don't want to run this red, I want to run the thread to simply, what I can do is I will assign this thread as a low priority and I will assign this as a higher priority. So see, we have used the priorities, the thread priorities to actually switch from one thread to another. And this procedure is known as context switch or context switching. So when do you switch from one to two other? We can make use of the priorities and we will implement all of this in our Java programs. So the rules, there are some rules obviously that determine when a context switch takes place. So as defined, those rules. So there are rules. Then context switch, ben context switch takes place. So basically this means that we want to know when our contexts which should take place. So there should be some rules that define this switching. And we know how to switch from one context to another by using, by making use of priorities. Okay, so the first, the first rule in context switching is that a thread can modelling very relinquish control. So a thread. This is the first rule a thread can voluntarily relinquish control. So what is the meaning of this whole line? So let me explain you this. So how can our thread voluntarily relinquish control? Control means now the program, sorry, the thread is having the control. And a thread can actually voluntary means that it can actually control, then it should run. And you can actually control the whole priority thing by itself. So let's see how this is done by explicitly sleeping or blocking on the banding IO. So how can thread relinquish control one and relinquish control? This means how can a thread control whether it should execute or not. So it can do so this can be done, this is done explicitly by explicitly my sleeping or by blocking on banding. I O, i o means input, output. So let's see the dean and the whole working behind this line, what this means. So we have studied about the thread states here you can see there is a suspended state running state, resume, state, block Sade. There is also this suspended state. So there is also a sleep state. Sleep state as actually it's basically a resume state. And which means that thread is in a sleeping, which means that it is not actually using any resource. It is waiting for some input outward. It is waiting for a resource. Sorry, not for resource. It is waiting for an input outward input. Output means. Now, let's suppose I say that a part of the program, Let's suppose this part is the trend and this thread needs some input from some function. And then only it can proceed its execution. At that point. It is waiting for the inputs, and then it is waiting for the inputs. It is actually holding the resource. This is a very important line. I will repeat that again. So if a thread is waiting for inputs from inputs, and it waits from the inputs from the IO stream, which is the input output stream. So effort thread is waiting for the inputs. It is actually holding a resource. So at that point, it should do something to do the context switch so that it will not hold the resource anymore. And other threads can also run. So what this means is that how can it do that? It can change a state from explicitly by sleeping. And we will see how we can actually change the state of a thread by sleeping it or by blocking it on pending input outputs. So whenever we identify that data, trade now needs an input to actually do something. We will sleep, it, will assign the sleep state to it. And when it is sleep, you can see that it has control over it. It can voluntarily relinquish. Control means veiling, which means deny control. So it can actually say that, okay, Now I'm waiting for the input output. So I am sleeping right now. So this is what this means. So a context switch will take place, can take place when a thread is sleeping. So even if there is a high priority thread, and now it needs input to do further executions. It will sleep and it will. And when it sleeps, it has actually had relinquished, denied control. It means that it will not execute. So the next one is a very simple one. A thread can be preempted by a higher priority thread. Thread can be preemptive. Preempted by a higher priority thread. So context switch can also take place when a lower priority thread that does not heal the processor is simply printed no matter what it is actually doing. And it is being paid by a high priority thread, for example, let's say this is a higher priority thread here. And now we know that it is not doing any much work. So what we can do is we can actually create another thread and we will assign it a much higher priority than this. A much higher priority. And this means that it has actually preempted a threat, which is this one is actually preempted by the higher Brian effect. So in that case also the context switch takes place. So this, and these are the technical terms which we have used context switch, thread, stage thread priorities. And we have seen how no multithreaded programming works. So another thing that we should, should know is this dome, which is known as Synchronization. Again, an important though. So because multithreading introduces and asynchronous behavior, do your programs. There must be some way for you to enforce sing grown in cities so that whenever you need it you can use it. So what does this mean? For example, if you want to threads two thirds me to part of the program, to communicate or to share a complicated data structure. Let's suppose we have two threads here and they want to communicate with the e, with each other. And communication means that they want to share something with each other. Let's suppose it does some data structure like linked list or an array. So when they want to do it, such as LinkedList or data structure, you need some way to ensure that they do not conflict with each other. So it's a very important node. Two threads must not on flaked each other. So this means that you will have to do multithreading very carefully. Because if you will create a thread, if you will create two threads and they actually conflict each other. For example, let's take an example. So let's suppose there is a thread and suppose this one thread, this thread, this is the Thread 1, and I will mark it as a thread 2. And we know that they belong to the same program. They are just the different parts. So let's suppose this program and this part of a thread does writing data. So let's suppose it as writing some data. In this data structure. For example, I wanted to say I have an array in this manner. And now I want to insert, I will also assign some index positions to this array. And I say that now I want to write five at the first position. So when I'm writing five to the first position, this part of the program. So let's suppose there is a programmer who say that. Okay, what I will do is I will create two parts of the program. One will write the data and the another one will read the data from this data structure. So this, when these two threads are conflicting because they are going to happen at the same time. Reading and writing can have, cannot happen at the same time because they will conflict each other. So let's suppose we're writing 52 this data. And before writing it at the same time you're reading their data. So actually there is no data to read before writing. So writing should take place first. Writing should take place first, and reading should take place at the second time. They cannot happen at the same time. So this is the exact meaning of the conflict between two threads. So three words must not depend on each other. They should not do something. So because they will have to run at the same time. This is the main reason why they should not conflict each other. So you will have to be careful in writing threads that if you are creating multiple threads, they are not conflicting each other. So this is basically the whole synchronization problem. Synchronization basically means that when you are writing the data at the same time, you cannot actually read that data. So the, these two processes need to be synchronized. So this is the exact meaning of synchronization. So the two thread should not conflict each other. So now we have a good knowledge of context switch, synchronization, threads, States, thread priorities, and we now have a good knowledge of multi-threaded programming. So in the next part, in the second part, VR going to write our first program in which we will create a single thread. And we will see how we can do that in Java programming language. So that's all for this tutorial. 18. Programming multiple threads using Runnable Interface in Java: In this video, we're going to discuss how to create a thread in Java. So in the previous tutorial, we have already covered how to, we have already covered a multithreaded programming in Java, and we have already seen a lot of concepts like thread priorities, VM, or discussed all of them. And now it's time that we start implementing multi-threaded programming in Java. So in this tutorial, we're going to create a thread in Java using a Runnable interface. So before starting this tutorial, I will, I will just recommend you to just go through the previous video, which is the multi-threaded programming. I will give the link to the same in the description below. All right, So before, so they start with the implementation. So we are going to do a multitasking. Multitasking. And basically in this program, what we're going to do is let suppose this is our Java program. So let's suppose this is our Java program. What we're going to do is we're going to create two threads. Let's say this is thread one. And we're going to create another thread which is 32. So we know that these two trends, basically you can run simultaneously so they can perform operations simultaneously. So what we're going to do is we're going to create these two threads and we will define what both of them will implement. And they will implement at the same time simultaneously. So now let's start by implementing the trade. So the first important thing that I wanted to say here is that we're going to create a thread using Goal Runnable interface. Runnable interface. And you should know what exactly is interfaces in Java. And if you don't have any idea about that, I will. I'm also created a video on this on interfaces in Java. So you can also check that out. I will give the description, the link in the description. So let's start by creating our first thread program. So what I'm just going to do here is I will create a class and I will name it as trade one. And this class is basically going to implement the runnable interface. And the Runnable interface is basically you do. You can see here there's interface should be implemented by a class. And it actually enables you to create a thread and Executor thread. All right, so now what I'm going to do is I'm going to create a reference to thread class. Reference to thread class. Now, I'm going to create a, I'm going to create a constructor here. Very simple constructor. So. Whenever we will create an instance of this glass trade one, it will, it will call this constructor. And inside the constructor, what I'm going to do here is I'm going to create, I'm going to create an object of the thread class and basically be a group. And we're using this thread glass, which is an inbuilt glass, and it is used to create threads. And it expects two arguments. One is the reference, which is this, and second is the name of the thread. You can actually provide the name of the thread. So let's say this is thread do or I should write dy du. So now you can see here that this is a very simple line. We're creating an object of the thread class by passing two arguments. One is the reference and another one is the name of the thread. All right, so now we will display the thread which is running by using our same old bend Elon command. And here I'm just going to specifying the trade which is currently running, and the thread which is running as D. All right, so now it's time that we start our thread. For that we're going to use the start function. So start function is basically, it causes the thread to begin execution. So many would we want to start the execution of a thread in the program, we will call this start function. So remember that says runnable is an interface and it has, it's basically a functional interface. It has a, it has a abstract method, which is the run method. So you can see here. Here, you can see if you all and this glass, it moves up or down that pipe thread one must implement the inherited abstract method. Rene will dart runs, it will run. Function is actually the abstract method inside the interface that we need do. All right, so we will have to overwrite because we would have been implemented since this thread one class is implementing the runnable interface. So Glick here add unimplemented methods and it will quickly, it will quickly create a run function here. So what I'm going to do here, some BS insider AND function, I will create a for loop. And this for loop is basically used to display, Let's see, forest five, fours, five natural numbers. So this program will actually display the fullest five natural numbers. I'm going to write system out. Print line command. And we're going to display I here. So you can see that this for loop is going to run. But now what we're going to do is we're going to display every value after some duration. So we can actually use trade dot sleep function. And basically sleep function is used. It just stops the execution or you can say it sleeps the execution of a thread for some time and you will have to give the time inside this batter meter, and it is basically in milliseconds. So if I will write here 1000, it means that it is 1 second. And similarly. So you can see here the Threadless leap year. Now, if you will, who were here, he would be able to see that it says that unhandled exception type interrupted exception. So one important thing to note here is that a thread can also get interrupted due to some reason like maybe the thread is not able to get proper CPU resources or something like that. So in any case, we're going to use a try-catch block. And we will catch this interrupted interrupt and exception. So whenever another tradable get interrupted, we will just blend that out here. We will write here trade interrupted. Alright. So now we have seen how to implement the thread, but this is just a single thread. And what this thread is going to do here is after every 1 second or let me just increase the time to do seconds. So I will also commenters leaps or two seconds. So when will this thread will sleep for two seconds? After every two seconds, it will display the number i. So let me just run this program to show you the output. So let me first result this editor here. Alright, so there was an early backed, it was missing years and that's like we're showing data. So let me just run this program. And now you can see in the output screen here that after every two seconds, it will display the next number. It will take two seconds and then next, and then five and then it will dominate. You can see you douse dominated null. So after two seconds it will display the number. So now what we're going to do is let's just increase this time 23. And now it will decrease seconds. An alphabet every 30 seconds it is going to call the run function. It will sleep and it will then call the run function again. And now what we're going to do if you're going to create another thread. And this time we'll call this trade as Trent do. And what I'm going to do here is I'm just going to copy this thread one. And I'm going to do based at her and I'm just going to rename it to tread. Do. The constructor should be thread, do. This should be d one. And here what I'm going to do here is I will write seven. And let's try it any random number, Let's write what in here. So there's broke down VL. Let's change the name to 1 second. So after every 1 second, after every 1 second, this run function will be coiled and this has basically inside the thread 2. So in this program would be unburned as we have created do threads, thread 1 and thread 2. One thread will execute this section, offer every three seconds. And the, another thread will execute this on this whole code after every 1 second. So you can see that we have implemented multitasking. This is the same program, but these two are different tasks that are running. So do different goals are running at two different times? So let's see what will happen if we run this program. So when we run this program, it will first read thread 1. So it will display 0, 1, and then it will wait for three seconds. And meanwhile, this thread, which is the Thread 2, will display the number 789 after every 1 second, and then it will display nine. It has already been three seconds. So then it will display the number which is due. And it will go on like this. So let's run this program and see the execution. So here you can see in this plane one and then add waited for three seconds. All right, so now the program has been dominated. So let see this output pair and let's analyze the output. And then we will be able to understand the multitasking. So for us, the thread which will run is our d1 and d2 thread will run. Then the Thread 1 runs, it will display one. As you can see here in this gourd. This one function will start execution and it will display one, and then it will wait for three seconds. So. Meanwhile, we have all trade do. And thread 2 will display this cord. It will execute this code. So it will display seven. And then it will wait for 1 second. And after 1 second, it will display it. And after and other ones that are going to display nine. So now you can see that this is actually thread do. And after three seconds, the thread 1 will execute. So after one it will display two. Then again, it will read for three seconds. So v times this Gordon will run, and then it will display. And then the third one will again display this chord, which is three. And then after three seconds it will display all of the results. So you can run this program by yourself. And I will also provide this Bill Graham inside the Android application which you end up with. Lord. I've given the link in the description below. And please make sure to subscribe the channel and like this video and also share it with your friends so that we will get some more support domain these interesting videos. Now, we have implemented a thread. We have seen how we can create two threads and we can execute more the threads at that same time by changing the number. So basically changing the time using the trend honestly function. So we are also willing to discuss them in more tutorials. We will discuss a lot important topics like deadlocks, synchronization, et cetera. And basically in this example we have seen, we will also discuss about Lord important concepts like deadlocks and deport them coming tutorials and synchronization. So in this program you can see that what these threads, the thread 1 and thread 2, are independent. And this means that the thread, any of the threads does not depend on the output from the another thread. So the Thread 1 is independently running on itself and thread do with independently running on it. So they do not depend on each other or they do not depend on the output that is coming from them. So in that case, we don't have any problems. But there are cases when we are doing multitasking. And when we're doing multitasking, then we, then there are case has been, some thread might depend on an output. It might depend on the output that is coming from some other trade. Let's say you do, in that case is some deadlocks may occur and b will have to do some synchronization work. And that's what we're going to see in the upcoming tutorials. So that's all for this tutorial. Thanks for watching. 19. Thread Priority setpriority() and getpriority() in Java: In this video, we're going to discuss about thread priorities in Java. So in this video, you'll begin to understand a lot of good and very important concepts in multi-threaded programming. And these are the concepts that are actually very useful in coding interviews. And they're oftenly asked in coding interviews because they expect that if a programmer has some knowledge about multi-threaded programming, then he actually knows how the threads are working with the CPU and the operating system. So what I'm going to do here is instead of giving you some boring exam, boring definitions of each and every, What is it terms. I'm going to, I've created a very good example here. And what's good about this example is that this example is used and it will be used in a lot of different areas of computer science. This same example is used in operating systems and in a lot of other subjects like operational research too. So let's start with the example and after I will be able to explain you, or I will explain you this example. This is a real life example. Then what I will do is I will relate it to the terms in Java or in multithreaded programming. All right, so let's get started. So now first fall, let's say we have a cinema hall. We have a cinema hall which has some seats available inside this hall. And let's say there are a 100 seats available. So now there is a manager of this cinema hall. And this manager has a wants to fill the cinema hall and wants to fill these seeds in the cinema hall. So what he does is he hires a person. And let's say that the name of the person is person a. So manager tells this person, do build a logic and the system in a way that all the seats of this cinema hall can get occupied without causing any much problem. Or you should say, in efficient manner. So what this person does, he employs two more people. He builds up or are you should say he deploys two counters. Let's say these are the ticket counters. I'm going to write a ticket counters and he employs two people in here. Now what will happen whenever the customers will apply will come in these two counters. Now the customers will come and they will buy the ticket. And in that way they will be allowed to enter the cinema hall. Now, let's say this person a, what he does is he is actually a ticket checker. So whenever any person has received that dictate what this person is going to do, he's going to check whether the ticket is there or not. He will also check the identity of this. Of all the customers who are coming, and then only he will allow the customer to enter into the cinema hall. So now what he does is he says that both of the HIV will open both of the ticket counters at the same time. So I'm going to write here that this is a situation one. This is a situation one. And it is a situation where two counters are running simultaneously. So both of these counters will run simultaneously. And this means that both of these counters will deliver the ticket to the customer and at the same time because tumor will go to the ticket checker. But certainly this person a, saw a problem in here. He says that I cannot check Do person at the same instance because this person is, this person a is not a multi-tasking person, let's say. And he says that I cannot actually changed the tickets of two person at the same time. So he applies some living. And in this logic he says that OK, what he will do is he says that I'm going to do tell the person employed at this ticket counter to wait for, let's say two seconds. So what will happen is first the person add this ticket counter will deliver the ticket to this person and as soon as he gets verified, at the same time, this person is going to wait for two seconds and four seconds. Then after Dorothy against the person, we're going to be able to enter this slot and the ticket checker will check his ticket and then he will get allowed in this in the cinema. Now let's study another situation, which is Situation number 2. Same example. The cinema, there is a cinema hall, same cinema hall with same number of seats available. But now there is a different manager. So there's different manager hires another person. Let's say this is a person b. So this manager also asked Person B to find out some logic and a system in which he will be able to deliver the ticket to bill people so that they can enter into this e-mail. So what this person leaders, he also creates two ticket counters. Just like in the previous case. We'll create two ticket counters. And similarly, these will also have some customers here, which are standing in a line. And this ticket counter will also have some people standing here in the line. Now this person B also experiences the same problem. But he says that instead of making this person, this person who is assigned this counter, to wait for two seconds. What I'm going to do here is I'm going to assign a higher priority to this person and this counter. A higher priority. Do this counter. Now, what, why he is doing that? Now he will assign a lower priority to this one. Now why is he doing this? Why he's using a different approach? The reason is that if this person is here for two seconds, he will charge some money from the ticket checker. So this person B is really smart, I should say. And what he does, he says then, okay, though, for this ticket counter, will will be will give the tickets to all the customers standing in this line. And after this, after this whole ticket counter will will be done selling the tickets. Then this ticket counter will be will come and into play and the person will come to this ticket counter and then he will walk. So what is happening is this is actually another situation to where these two counters are not working simultaneously. So not working simultaneously. So these are the two situations and this is the example. Now let's relate it with thread or multi-threaded programming in Java. You see this person here, which is the manager, is actually the operating system. He is the operating system. And this operating system hires this person. And let's say that this is actually, this person is a thread scheduler. Now, different operating systems have they employ different strategies to do the multitasking processing. So I'm going to write here multitasking. So in order to achieve multitasking, there are different operating systems. So if you consider Java programming language, we know that Java is platform independent programming language. But a platform independent does not mean that it can actually change the architecture of any operating system. It will have to work according to the operating system to achieve multitasking. It cannot change the architecture in any way. So now this, these persons are actually thread scheduler. B is also a thread scheduler. So let's understand what is the thread scheduler. Thread scheduler is a thread share dealer assigns priority. The word thread, priority to thread. Here you can see these two people in these two counters, in both the situations, these people are actually threads. They are actually threads. So what the thread scheduler does, he assigns a priority to a thread. In the first case, since both of these were running at the same time, so both were assigned a same priority. So I'm going to write here in situation one, both threads. More the prayers have the same priority. So a priority is actually has a range from one to 10. N means the maximum priority and one is the minimum priority or the lowest priority, and five is actually on normal priority. So let's suppose in this case, in this situation one, we're nowhere, the two threads running at the same time, what the thread scheduler will do, it will assign both these threats the same priority. So in order to see what is the priority of a thread, we can actually use a get priority function. And we will call this function on the instance of the thread class. So let's suppose I want to see what is the priority of the thread 1. Let's suppose this first counter is D1 and the second counter is T2. So we can actually call the get priority function here. Now, this D2 thread is actually waiting for two seconds. And by the time it waits for two seconds, this thread one will actually be able to process. So this is similar to a program. We have very, we have two threads. One is D1 and one is T2. T1 is working, whereas D2 will sleep. We're going to use the sleep function and wait for two seconds. We're going to specify a 100, sorry, do 1000. So we can actually wait or sleep a thread to do the multitasking. Now, you can see we can do another way to lead this thread wait is to get some user input. So that at the time when this thread is actually taking some input, or let's say some cash from these guys or some ID. At that time, the ticket counter one will be able to send a person to the ticket checker. So now let's see the situation number two, where we, we are not where this thread scheduler, this spreadsheet dealer is not assigning the priorities as same to them. And what we can do is as a program where we can actually control this. If you will not specify the priority of four thread, the thread scheduler will automatically set it to some default priority. But if we want to act like this, act like a person B, what we can do is we can use a dot. I'm going to write here situation too. For situation two. Let's read, we're going to use a set priority function. P is capital here. And let's say this is thread one, and this one is Thread 2. So let's say we want to assign a higher priority to the sphere so we can actually do it using this function. And we're going to specify an end here. If you don't remember that these numbers you can actually use. Keyword, which is the max underscore priority. Like this. Now if you want to assign T2 a lower priority, what you're going to do is you're going to write p2 dot said priority. And you're going to assign one. Or you can use the keyword Min priority. And in case you want to actually assign the default priority, you can use the keyword nom, which is the normal priority. So now you can see here, in this situation one, we have two threads running simultaneously and one thread is waiting for two seconds. And in this one, the higher priority thread is running first. First, it will execute, and then the lower priority thread will execute. Now I explained you that in this situation, in this situation number two, this higher priority thread will run first. And this means that the person is going to come into the ticket counter only after this thread will finish its execution. This means that this thread will take the counter. We have assumed that the ticket counter is actually a CPU. So the thread will enter into a CPU only when it will, only when the higher priority thread has completed its execution. So now you can see that after this, it will enter CPU time. Now, in theory, higher priority thread takes higher CPU time than a lower priority thread. So if you can even see this from this example, it's clear already that I have priority thread will take a lot more CPU time than the lower priority thread. Now, I'm going to explain you a very important concept now, which is both of these iterations, there is a limitation. So let's say there is a person C, who isn't much experienced person, and he has already seen these two situations. Now what he says is what his ideas. He says that in situation one, if you will implement the situation one, you are actually consuming too much of CPU. And let's say the manager does not have that much money or let's say the manager does not have that much money to employ. Doo, doo. Doo implies do to get counter cashiers. So in that case, this will have a limitation and will be a waste of money. And money. In this case, it will be the CPU time. And here in this case, what we can do is first this thread will execute and then this person can only come here. And he will try to execute this. So here the limitation is the higher CPU time, because both on the threads are running at the same time, they are allocating the CPU. Here, there is a limitation. And if you have observed this limitation, it's really good. And the limitation is that, for example, this higher priority, this one is running. And only after this will execute and finish its execution, then only this lower priority thread will be able to run. And this will cause a problem in case when we have a lot of customers in the first ticket counter. So let's suppose there are thousands. Or let's say there are so many customers. And it will take a lot of time for this thread to complete the execution. And in some cases it is observed that this thread will be never, will never get a chance to run. So this means that this thread will be never be able to. I'm going to write here very important term, relinquish control. So this thread will become almost a dead thread because it will not be able to relinquish the control over the situation. Because the higher priority thread is taking so much time to execute, to just execute its operation. In that case, this person C has analyzed more than lizards region and he says, What if we do something like this? What if we say that the higher priority thread will run? But after some time, after, let's say some time we're going to wait for some time, let's say five seconds. And this higher priority thread will wait for five seconds. And at that time we're going to give or we're going to give all relinquish control to the lower priority thread. And meanwhile, this thread will wait for five seconds, which means that this person will wait for, we'll leave the CPU for five seconds or the ticket counter for five seconds. And he will come here and we will start giving tickets to these people. Now what will happen is after five seconds, this Q has, again a higher priority thread. Now says this has a higher priority thread. Now this person will have to move again back to here. Now this movement is actually known as bringing option. This is also again, a very important term. Print. Print means that you are actually stopping or you're leaving a particular thread and you are going into another. So I'm going to write her a very important line here, which says that a higher priority thread, a higher priority thread, can relinquish control over control over lower priority thread. And I hope this makes sense from this example, you can see that if we will assign higher priority, we can just wait for five seconds. And for that five seconds, who are giving lower priority a chance to just get the control and starts its execution or to start getting the tickets in this example. So after some time, the thread priority will again become alive. And then this person, this thread will have to relinquish control. And the higher priority thread will stop. Or bringing the lower priority thread 2 starts its execution. So we have understood, we have, this example is really useful and it will be used a lot in the future to explain multitasking. And if you give this example to the interviewer, it will be really good and it's also good for attention power also, you will always remember this, these, all these concepts using this simple example. So what we have studied is we now know the working of thread scheduler. We know that different operating systems handle different, has different ways to handle multitasking. And we have two situations in which, in the first situation both threads are running simultaneously. And in this one we are actually assigning a priority. And this smart person C is making a combination of both of them. And he's actually making this higher priority thread wait for some time. And in that way, it will be this low priority thread will be able to execute the code. So this is actually a good programming or multi-threaded programming strategy that you will assign a higher bracket red, and a lower priority thread. And at the same time you also create a code which will wait for some time. If you're assigning a higher priority thread. You can even use some by taking some user input. And at that time the control will go to the lower priority thread. So these are all very important concepts and it was related to multi-threaded programming. In the next tutorial, we're going to study a very important concept, again, a very important concept which is known as synchronization. We're going to study this topic synchronization, and we will also be able to implement this in Java. So that's all for this tutorial. Thanks for watching. 20. Multithreaded Synchronization using synchronized in Java: In this video, we're going to discuss about the thread synchronization. And we're going to implement threads synchronization in Java. So what I'm going to do is I will first give you an intuitive example, a popular example of producer and consumer example. So let me write here producer, consumer example. And first I'm going to explain you what this producer-consumer example is. And then we're going to implement this example in Java. First, we will implement this producer-consumer example without using synchronization. And then we're going to observe the output in that case. Then we are going to run the same example using synchronization. And then again, we're going to observe the outward. Then we will be able to understand why actually, no, we need synchronization between threads. So let's now start with this example, the producer-consumer example, which is a popular example. So let's try to understand this. So let's suppose we have a producer here. So I'm going to say that this is a producer. And what a producer does, he creates or produces, let's say n number of items. So I'm going to create a variable n. So let me just, let's just initialize this value n as 0 initially. So after the producer will produce an item it for, he will increment the value of n by 1. So n becomes n plus one. And there is a consumer which consumes this product. So I'm going to write here that this is the consumer. So since a consumer is going to consume this product, he's going to decrement the value of n by one. So now we have a producer and the consumer. And let's say n is the number of items. Let's say n is the number of items like this. So the producer will increment the value of n by one. And what we're going to do is we're going to, for the sake of simplicity, we are going to produce and consume 50 items in this tutorial. So we're going to consume and produce five items. All right, so let's suppose there is a program or who wants to implement a production line. And what the programmer wants is that the producer should first produce these five items, these five items. And at each step, the value of n is incremented to one. So what will happen is the producer and the first step, he will increment the value of n by one. Then he will increment the value of n by two, and then three, and then four. And then finally. So after the producer on, you can say after the production line has completed the production of all the fiber items, now the consumer will start consuming these items. So now consumer what the zooming is going to do is, so this is the flow of producer and the consumer is going to be increment. The value is off end by minus one. So now consumer will start consuming this product from Hill. So now this is the ideal situation where we want a producer and a consumer to produce and consume these products. But the case is that producers should first produce all these five items and then only the consumer can consume these items. Now in a multithreaded programming, what we are going to say is that this producer, And let suppose this is a thread. This is a thread and this consumer is also a thread. And you can see that bolt off these threads are actually manipulating the values of n. So this means that thread producer is changing the values of n and consumer is also changing the value of n. And such type of variables known as shared variables. Or you can say that this is a shared resource because this resource is being shared by both the producer and the museum where both are sharing or both are changing the values of these, this item. Now, what we would have to do is we say that we live in a multitasking environment. In a multitasking environment, we want the thread producer to first do the all, do all the production. And then the consumer will trade, will start doing the consumption of Molly's. But what happens when we create a program, when we create a thread or these two thread. Now you can see that these two thread are dependent on each other and because they have a shared resource. Now what happens when I will initialize both of these threads and the same time and the same time. So let's suppose these two threads on the producer and the consumer. They both started production and consumption at the same time. So what will happen is, let's say that the thread producers stocks of production first. So the thread producer will increment the value of n by one. And the first step, and the second step, the producer will increase the value of n by two. But what happens is that since this is a multitasking environment, the thread consumer can also simultaneously do the consumption of this resource, which is n. So what happens is when we create both of these threads and since they are running simultaneously. And this point, the consumer thread will start decrementing the value of n by 1. So n will get equal to one. And then again, let's say the consumer will decremented by one. And let's say now the producer, it has started producing this item by incrementing the values. And this will go on so and so. So what the program would initially wanted is that the production line or the thread producer Come, bleeps, completes production. Odd. Complete its execution. And only after this trade producer will finish executing, then only this thread consumer will start the consumption or the execution. But we have seen that this is the problem. That if we are running in a multitasking environment, both of these threads will start simultaneously. So let's say this produces types first. It may happen that the thread consumer starts in-between. And what will happen is it will start corrupting the values of n or changing the values of n, which is not desired by the programmer, which is not desired output. So what are we going to do here is we're going to introduce synchronization. Now, if you want a thread to access a shared resource and change its value, and while it is doing that, you want all the other threads in your program do not access this resource. You have to introduce synchronization. So let's see what we actually want to achieve. We want the thread producer, the producer, to access the shared resource and equals to 0. And let's suppose the producer is calling a function. Let's say the function name is update items. I'm going to write here, um, data items. So let's suppose the producer is accessing this data items, which actually changes the value of n as n equals two, n plus one if the producer is calling this function. So what we want to do is we want to stop the consumer to access this function, right? Producer is executing. So if the thread producer, if one thread. So I'm going to say a very important line here that if one thread is accessing a shared resource, then I want that all the other threads in the program, if they are trying to access this resource, I will have to stop them. So this is the DAS that we need to do. So how are we going to do that is whenever a thread starts accessing any particular resource, it will create a lock. So it will create a log. So I'm going to draw here. Yesterday presentation of a lock. And this is actually known as mutually exclusive lock. Mutually exclusive lock. So what is the function of this mutually exclusive lock is that if any thread is trying to access a resource auto function, and it will develop mutually exclusive lock, which means that this shared resource is exclusively available only for this particular thread. So if any other thread tries to enter into this shared resource or tried to access this shared resource, this law will not allow it to do. So. Now let's see how Java can actually implement this lock. Will this lock mutually exclusive lock? In short, it is also known as a mutex lock. A mutex lock, which is really an important concept and it is also asked in a lot of coding interviews. So now what we want to do is let's see how in Java implements this mutually exclusive lock. And how as a programmer, as a Java program where I can implement this mutually exclusive lock. So let's take a look. So what Java introduces is Java introduces a keyword known as synchronized. Synchronized. And this key word. Then we will introduce this keyword to walk function in front of a function. So what I'm going to do, I'm going to write this reserved keyword in front of this function update items. And what it will do, it will create a mutually exclusive law. So if the thread that is trying to access this shared resource will be only the thread in the whole program to access this resource. And all the remaining threads will not access that. All right, so now we're going to see, now I'm going to write a program which is a producer consumer program. And we're not going to use synchronization first, and we will observe the output. And then we're going to use the synchronized keyword and we will see how the production is actually working first and then the consumption will start or vice versa for the consumption is starting and then after the consumption has entered, the production has started. Let's try to implement that in Eclipse. So now you can see here in this is the main function in this one, and this is our main class. So now we're going to first create a class update items. And we will create a function update items. And we know that this function is going to be the shared resource in our example. So we're going to create a glass producer and a glass consumer. And on the basis of this, we're going to create an implement the producer and consumer. So now you can see these both classes implements the Runnable interface. So we're going to create a thread off a producer class and a consumer class and bought the threads will try to what is a reference or call this function update items. And we're not going to use synchronization at first. And then we will check what will happen if we will use synchronized in front of this function. All right, So let's begin. So what I'm going to do here is I'm going to create an object of the update items. And after creating the object of data items, I'm going to pass that object to the two, both of these glasses, which is the consumer and the producer class. And what I will again do is, you can see here I've created a constructor. And basically this is a parametrized constructor which takes the reference to the update items. And in this constructor, we will create a thread that will implement the trait that will further call this function and bad items. So let's start with this. So let's first create an object of the items. And I'm going to say that this will be the update items. Items is equals to new update items. So now I have created an object of the data item class. So let's now call the constructor of the producer class, which will further create a thread, which is a, which will be the producer thread. And here we will have to pass the reference of the update items because it had, it was a parametrized constructor. And the same thing is what we're going to do with the consumer because and both of these are actually trying to do the same implementation. So now you can see this is the consumer. It has the same chord. And you can see the run function inside this, which we are going to override. And you can see there is a for loop which will run five times. So I'm going to write six here that it will run five times. And similarly in the producer also it will run for five times. And since this will run five times, we would have five cases or five productions of items and consumption of items. So let's start with the producer. So I'm going to create a thread reference, which is, which I will call as the trend because there's the producer thread. And what I'm going to do is I'm going to create a reference, do the update items. And I recall it as be temporary because we will need this reference to call this function, which is the update items. We need a reference to this. And since we are bringing to, you can see in the producer, we are going to, in the constructor of this producer, what we're going to do is we're going to assign this temporary reference. Equals to B items, which means that in the main, in the main class you can see I've created an instance, an object of the update items glass. So you can see that the same object is being passed to both of these classes. But since the constructor will have two different references, as you can see here, it will have two different references. So we're going to initialize this reference to the reference of p temp. Because we are going to use this reference to call the function which is the dot update items function, which is this one. And in the data items I'm going to pass a value because this will be able to recognize which of which trade has that function. Because on the basis of that, we're going to increment the value of n or the number of items and, or decrement the value if the consumer third is calling that function. Alright, so far we have done this. Now here we will have to create the thread. So what I'm going to do is I'm going to initialize the thread now, laying this, pass the reference, which is this. And let's say the name of this red is producer. Alright, so now I'm going to start this trip. So let's start this tread using the thoughts dark function like this. So now this function, stock function would just run this function, which is the run function and it will learn four or five times and every time we're going to call this update items. And again, I'm bailing that we are using the beef and because it is the difference off update items glass, same thing as what we're going to do here. We're going to create a thread like this. And again, we're going to create a reference of the update items glass. And this time I will call is C dim, like this. And these are two references and two references are calling the same function so that doing the main function. So there is an instance of this glass, a bait items in the consumer class and also in a producer class. So then we will use synchronization. The synchronized function will be able to know that the different references are being used to call this function of data items. So it will be able to create a mutually exclusive lock for it. All right, so in here in the consumer constructor. Well, I'm going to do is, I'm going to first create a reference, an object like this, which is initializing the thread. And this time we're going to say that this is the name of the thread, like this. All right, so after this, we are going to assign the reference seat them as C items. And there should be underscore. So the C items is actually the reference which was passed by the main function and we have assigned it to see damp. And we're doing this because we now want to use this reference to call the function. So c, m dot update items and we're going to pass to this bank as an integer. So if the value is two, that means we have garnered the consumer thread is calling this function, which is the embed items. So I'm going to write here that if c h k, which is checked as equals to equals to one, it means that producer is calling it. So we're going to increment the value of number of items by one. And before that we will create an integer, which is the number of items. It's there, it's n. And I will assign it as 0 because initially there are no items. And if the producer calls this so that the variable which you can see this one is the shared resource. It will be coiled. So if the producer is calling the function, so it will eject, will be pulled to one and we're going to increment. Else. We're going to decrement the value by one because the consumer is calling it. And after implementing what we will do is we will display, we will display the number of items and also the value. So if we are incrementing the value by one, this means we are actually producing. So I'm going to write here producing like this. And I will print n here. And similarly, I'm going to do this with D consumer case. So I'm going to write here consuming. So now what will happen is you can see that these two are the actual, These two are actually calling the the producer and the consumer thread with a reference to the items. And both of these classes have their own reference to these items. So when different references will try to access the same function, v, The main function, we'll be able to know that too and synchronize. If I've arrived synchronized in front of this function, then it will work because that's how Java JVM will be able to know that two references on trying to access the shared resource. All right, so this is a program, and let's try to run this program and see what is the output. So let me just pause, remove some errors if there are any. So there are no errors yet, is trying to see the output. All right, So it's saying producing one. Okay, so we have done a mistake here. Alright, so this is happening because I have not called the dot start function. I will have to call this function thread dot stock function. Then only it is going to start the consumer. So let's run this again and see what happens. All right, it says producing one and this. So actually it is not incrementing the value by one. All right, so I think that since the thread is calling this function of beta items again and again, it will always initialize int ending close to 0. So you will have to place this line above here so that it does not obtain the value of n as 0 every time this function is being called. So let's run this program again. I, so it's a producing one, producing two, then consuming to consuming one. And you can see here that at the time it was trying to produce the items that consuming tread on, just bopped out and it started decrementing the value by one. So you can see this is the unexpected output undesired awkward because the program is not synchronized. So you can see here that after Penzias coming again, the producing thread pops out and it starts producing by incrementing the value by one. But that is something which the programmer does not want. And since this is not a synchronized method, which is the update items, it is not synchronized. And that's why it does not prevent the consuming thread to just start the execution. So now what we're going to do is we're just going to write synchronized here and write new using this keyword synchronized in this function will be able to create a mutually exclusive lock. And this will just enable a particular thread. If it is producing through it, it will execute first and then some other thread which is consuming in our case, is going to execute. So let's run this program again. So now you can see here a forest produces all the items and then it, it will reduce or it will try to conceal those items. You can see that this use of synchronized keyword has enabled us to lock up particular thread. 21. Interthread Communication in Java: In this tutorial, we're going to understand the concept integrate communication in Java. In the previous tutorial, we have already colored synchronization. And that we do, we have, I've just explained a consumer and a producer problem briefly in that program. And we've also create, implemented that program in Java. In this video, we're going to use the same program and modify it a little bit. And we will see what problems will it solve it. We will enable the communication between the threads. So empathetic communication is essentially the communication among traits. So if we have multiple trips in our program, we can actually make them, make a communication between them so that we will be able to get a much desired program and we will see what that desired program is. So let's discuss the synchronization problem first. So first, we have created a producer or a producer trade like this. And we have also created a consumer trend in our program in the previous video. If you haven't watched the video, you will have to watch it. I've given the link to that video in the description below, or you can click the I button here. It is also available in our Android application. All right, so we have created a variable x, which represents a shared resource, and we created a function of data items, um, data items function. And this function was synchronized because they use the word synchronized in front of it. So this was a synchronized. So what we do is the producer will access this function to update the value of x by incrementing its value by one. And the consumer thread consumes the value of x by decrementing the value of x by one. But since we have synchronized the program, only one trade will be able to update an item. And then if one thread is actually executing this function, no other thread in the program will be able to access this function. And we saw that this was actually able to enable it by Java internally as a mutex lock. A producer trade is basically what it was doing Is it was incrementing the value of x by 1. And since we enabled for loop which was running five times so that we can observe for the next by vibrations. We can see the value of x gets incremented by one. Now after the producer trade has completed its execution, it will come out of the monitor. And the consumer thread will enter the monitor. And the thread which is inside the monitor is only able to execute this function. So what now consumer thread will do? It will start decrementing the value of x. By one. Like this, it will go on so like this. So you have seen that VM already implemented this whole program in Java in the previous tutorial. So if the producer trade is trying to access this, the consumer theory, it will not be able to access it. So if the consumer is not able to access it, what is going to happen with the fit isn't going to stop. What is going to happen with this trend? We're concerned with that. Now we know that we have initialized both of these threads, but consumer and producer trend. And the same in stance and the same time or simultaneously. Know since these two threads are running, they were, we have created our initialize these two threads at the same time. It means that both of these threads are actually consuming CPU. So I'm going to write here consuming CPU resources. So even though the consumer trade is not executing this function, it is actually consuming the CPU because it is actually running inside the program. And what is doing it is sitting either the consumer trend is sitting idle for five vibrations and then it is going to come into action. Now, this is not a design program because CPU resources really crucial to us. If the program becomes, I'm very big. If we are actually handling millions of products, then we will have to do something such that the producer and the consumer trade, both of these threads will work hand in hand. They are going to act simultaneously. And since we want to make these, both of these threads work simultaneously, there has to be some communication among them. And what are our objective is, let me just first write the objective. The objective of intercalate communication. And in this example is that producer should produce, produce, produce an item. And immediately, immediately consumer will consume it. Consumer consume it. Now this is what act and what is the objective of our program. So what we want is when a producer produces an item. Although we know it is going to produce five items, since we want to enable multitasking, what we're doing is we're saying that as soon as the x becomes equals to one because of the producer in the first titration, it's the I equals to one. What the consumer will do. The consumer will decrement the value of a by 1 at the same time. So again, the value of x has become 0. Then again, the producer will produce one more item and again the consumer will consume it. And this will happen for phi, right? Because of our for loop. And 45 times the consumer is going to consume it. Now there is a question that arises in the situation then, how will the consumer know that the producer has produced an item? Or how a producer will know that a consumer thread has consumed an item. So this is enabled, this, these cushions and answered by introducing the communication among threads. And basically communication in threads is enabled using dual very important inbuilt functions, which are vague and notify functions. We're going to see them, them inaction in Java. But I'm going to explain you these to me and very easy, don't. So now let's suppose in the first titration, the producer creates an item. Now when the producer will do, since the producer has created an item or produced an item, it has implemented the value by one. Now it will wait for some time. And that time, in that time, the consumer thread would have to start its execution. So how will the consumer will know that the producer wants to execute? So what the producer trade, it is the responsibility of the producer thread to notify the consumer thread to actually decrementing the value of x by one. So after calling the weight function, it is going to call the notify function. So what does notify function will do, it will trigger the thread which is actually trying to access this sync nice function. Remember this function is synchronized in both the cases. So now what we're going to do, now the consumer knows that the producer has no defined that. Okay, I'm reading for some time in that you can, meanwhile, you can just leave him in the value of x by one. So the consumer will decrement the value of x by 1. And then it will wait for some time. And then it is going to notify. Again, going to call the notify function, which will notify the producer than the consumer has changed the values. And then produce a real, again increment the value of x by one. And again it will call these two functions. And this will go on and on till five times. And this is how we enable communication among threads in Java. Now we're going to implement into their communication in Java. So what we're going to do is we have the same program as we had in the synchronization in Java video. If you've watched that video, we are just. Using the same program, you can see this is the same program. And you can see here we have initialized do func, do threads, the producer and the consumer threads. And they were both running at the same time. Now you can see these are the consumer thread class, and this is the producer thread class as we have created before. And this is the class of data items. Now what we're going to do is we're going to use notifying and weight functions. We're going to just modify this function of data items, do this one. And then we will be able to create intranet communication in Java. Now let's see how this works. Now, if the value of check is equals to, equals to one. So you can see check is given as a parameter to this update items by the producer thread, which is here you can see in the run function, it is calling the update items and the value given as one. So if the value is one, then it means that we are inside the producer thread. So I am going to write here inside the producer trade. So now one sphere inside the producer trade. And what can we do is the producer will increment the value of n, which was essentially the number of items. And it will increment the value of n by one. After doing that, it will call the notify function, which notifies the consumer trend. It notifies the consumer tree to come into action. And it will display. Here you can see it will display the value of n, reducing end. And after displaying this value, it will wait for some time. So we have all the weight function here. And remember that this wave function has to be in the try-catch block because this weight function Maital interrupted exception. So we will have to handle it too. Now you can see our producer will increment the value of n, then it will notify the consumer thread, and then it will wait for some time, as we have seen in the concept. Now, here comes the inside of the, inside the consumer trend. All right, so inside the consumer, we have written a condition here. If n equals three equals to 0. Because if the value of n is 0, it means that the producer has not produced anything. So if the producer has not produced anything, we're just going to wait. So we have all the weight function. And again it is in the Craig Ashkenazi because it will have to handle this interrupted exception thrown by the way, function. Now what would we do if the value of n is not equal to 0? We will decrement the value of n by one. So we will decrement the value of n by one. And we will reach this line only if it is not waiting. If it is equal to 0, it will keep on reading and it will not come at this point. So if the value of n is not equal to 0, it will come at this point and it will leave him in the number of items might want. After decrementing what it will do, it will simply notify the producer fed. It notifies the producer 10. Now that it notifies the radius of that trade, that now the producer should start producing and then it will simply display consuming. So let's see the output of this program. All right, so you can see here, as soon as the producer produces a single item, immediately the consumer is consuming it. And this is going to happen five times. You can see this is happening five times because of our for loop here, which was running from I equals 0, I less than six, meaning 59. So in this video we have when two things we have implemented synchronization. And it is actually synchronization enabled with intertribal communication. So definitely this is a very important program. You can find this whole program inside our Android application. Yes, he will have to do a search in birth rate communication in Java. And then you will have to go to the video. And below the video you will be able to see this whole code. So that's all for this tutorial. Thanks for watching. 22. Debugging and Breakpoints in Eclipse Java: So in this video, we are going to discuss about debugging, and we're going to use the debugger and the Eclipse IDE, and we will debug our Java program. So basically debugging is a way to actually monitor some code. And if, let suppose there is a function in a whole software and you want to monitor how it is changing the values in how it is actually performing the operations. Or if you want to check if the function is actually doing what is expected from it. So you can use debugging in that case. And let's suppose you have, you're working as a professional in some companies then no debugging is something that you should actually know. So now what we're going to do is I will just, I will just give you a very simple example and you will be able to understand what debugging is. And you will be able to do some debugging and testing and it will help you out in the future in removing bugs. So let's get to the topic. So this is the main class in which I have created a class student and I have created an object of this class student as student one. Now I've created a, if you will go on the student dot Java class. So here I've created two variables, marks and name. I've created a default constructor. And you can see the values here. And I've created a function of date which will update max and name. We're not going to go into the details of this program, but it's very simple that using update, we're going to update the values of max and name. And similarly you can, via, we have created a M max function which will return the max off those student reference given to it. So now you can see in this program, we, this is a very small programs. So even without debugging, we will be able to understand what the errors are in the program. But let's suppose we have a very big software and that software, it contains thousands of lines. So basically, if a tester, we'll just do some light unit test, which we will also discuss in a separate video. And if the tester comes out to known that there is something wrong with a particular function or a particular class. So he would just tell the programmer that this is particular problem, but this whole class or a particular function. And then the programmer will debug from that point, and that point is known as break-point. And breakpoint is just basically the point from which you want to monitor your whole program, a particular function or a particular class. So let's suppose in this case, we want to monitor how the student class or the, how the update function is updating the values or how the constructor is being called. So let's suppose we want to monitor from this line, which is this one. So wherever you are from the point where you want to just monitor your whole program, how the values of the variables are changing. You will create a breakpoint here. So how can you create a breakpoint here is you just have to go on this line on this space. Let me just zoom in so that you will be able to see it much clearly. So you can see here is a fault line. If you will right click on it. You will be able to see your toggle breakpoint on. You can even double-click. And it will also show line numbers here. So what you will have to do is just double-click here. And we'll double-click here. You will be able to see you small blue point, which is the break point. And from this break point we're going to debug our program. So now we have created a breakpoint. So let's start with the debugging process now. So the first thing you will have to do is you will be able to see this icon here, which looks like an insect icon and it says Debug Main. So there is a main class I will, you will just have to click on this icon to do the debugging work. So after you will click on it, it will just go into the debug perspective. So what is the debug perspective? Actually, there are two perspectives. This one is the Java perspective. Let me focus here. You can see that this one is the Java perspective and this one is the debug perspective. So once you are in a perspective, you're actually doing that particular task. So you are now actually debugging it. So that's why we are in the debugging perspective. So now you'll be able to see this whole part in this whole perspective. This is the console. And here the variables are here. Here is the breakpoints, and you can see the breakpoint is on line number 4. And here comes the expressions and the name and the values. We're concerned about variables at this point. So you can see at this point it has executed this main function and the arguments adding is string 0. So now we have reached a breakpoint. Now what we want to do is we want to monitor if this hole, how is this glass working or how is the update function working? So when you are on this breakpoint, it will actually break the program execution before that. So the program will terminate her, and now it will just run till here. So that's why it is known as a breakpoint. It breaks out of the program. So now if you want to execute this line, because I want to monitor this line, so I will basically right here, I want to monitor this line. So in that case, what you will have to do is you will first, we will discuss about these three options here. Let me focus on it. This one is known as the step into. This one is known as the stamp over. And this one is the step filters. We're not concerned about this at this point. We, we're only concerned about these two, the step into and the step over. So let's see what they do. So when you are in this line and you want to see what this line is going to do. We're going to use the step into key. But if we just want to skip the implementation of this line and we want to go on the next line. We will use the step over. So that really makes sense. So Let's see how this function is doing. Sorry how this object creation line is doing. So I will do a step into, so I will click on it. So when you click on it, you will be able to see in this dialogue box that it has, it is actually doing some work. And basically the program will just not work like an expected because at this point it is just creating the object and nothing else. So that's why it has loaded this. So now what I want to, I will do here is now I will go on the step over. So when you will step over, now, you will be able to see that it has returned from the load class function, which is basically, which basically means he has just the object creation or is successful. So the student one is initialized. So now I want to step over again. And now after you will step over, now you can see here is an item. You can see this arrow icon. This arrow icon basically specifies on which line you are and on which line you are actually debugging. So you can see debug the current instruction pointer. So I will quickly minimize this and I will press the Step Into function. So basically you can see that on running this line, it has created a student one object. And you can see that after stepping over this line, the default constructor was called and Marx was initialized as 0. Name was initialized as default name, which was according to the, to this one. You can see this constructor here, it as a default constructor and initializes marks as 0 and name as this. So you can see how much that this feature of monitoring your code line by line and actually monitoring the variables and how they are actually changing how the returning values and everything is so much beneficial that you will be able to find out the book is, you will be able to find out at the specific line where the bug is. So now let's step over this line. So now you will step over. You can see that now it has executed this line, and now it has changed the value of marks as 95 and the name as my name, which is Vishal. So now it has changed the values. So you can see how debugging is basically used and how you can actually monitor the change in the values of the variables. And let's suppose you have a function also. It will do the same with the function. It will display the values of the, of them. And basically this is how you use debugging. So if I will again press step over. Now, it will just say print line returned. And in the console you will be able to see the output is 95. So basically, this is how you use debugging. You create a breakpoint and you will start monitoring the whole program line by line. And in this way, you will be able to analyze each and every variable and how the functions are responding. So you can try it yourself. And what you can just do is you can create your own program, or if you have an existing program, you can actually run this debug mode. And you will just use this step over or step into and just see how much magical this is and how much useful this is. So in the next recording we will study about unit tests so that basically you will be able to find out where is the bug is actually the bug is, you will be able to test each and every function. And then you can also debug on that particular function. So that's all for this tutorial. Thanks for watching and make sure to subscribe the channel. And like our video and also download Android application which is available on the Play Store. And I've given the link in the description below. So just check it out. It has codes and all these videos that I upload. So thanks for watching. 23. Java JUnit Testing in Eclipse: In this tutorial, we're going to perform J unit testing in Java. So basically I have this Eclipse IDE, so we're going to perform J unit testing in Java. So in Eclipse IDE, we're going to create some JUnit tests, which are going to perform some testing procedures. So what exactly do we do in J unit test is that we desk some units and units are basically functions in your program. So let's suppose you have a lot of functions in your program and you aren't getting any unexpected output of your program, it means that some function is not actually giving you the desired output. So instead of checking the whole program, what you can do is you can actually break the whole program into units, which are basically functions. And you can test them individually. So without running the program, you can actually give some values to the function as a pattern meters or as inputs. And you can check whether the function is giving you the desired output or not. So let's start with this. First, I'm going to quickly create a Java project. In the Java project, I'm going to name it as J unit testing. So vm look like next. Then we will go on this, Add Gojek J unit testing to build back. All right, so next we're going to click Finish. Now here it will create a source folder and we will quickly create a back edge here. And let's name this package as S, right. So I finish it. And in this we're going to create a class in Java class. And let's name this glass as unit testing class. All right, so we will click Finish here. So here is our class. Now, in this class we're going to create some functions. We are going to start with one function and we will create some more functions. And what we will do is they will not run this Java class. We are going to just run the test and we can basically desk particular functions in it. So quickly I'm going to create a function which is a square root function. And if you give some input into this function, it is actually going to give you the square root of the number, which is n squared. Something like this, right? So very simple function. And let's suppose you replace this function with some other function. One thing will remain constant is that we're going to provide some inputs and you are expecting some outputs. Alright, so let's create our first J unit tests. So here I'm going to go on my project name and I'm going to right-click and go to New. And here, if you go down, you will see J unit test case. So quickly, click here JUnit test case. It will open up this dialog box. So let's name this as square unit. Because we are actually testing the square function. So I'm naming is it ass square unit? All right, and here you can see it's saying New JUnit Jupiter test. Instead of this, I would recommend you to click on New JUnit test. All right, So we are, we have now done this. Now click on finisher, and it will ask you to perform the following actions. Click Okay. Can see it creates a particular Java class here, which is great unit dot Java. And inside this we are going to actually try to test our square function. And then we will run this class. So what we can do is here in the test function. So whenever you will run this class, it will start with this debt dysfunction. So what I'm doing here is I'm going to quickly create an object of our unit testing class, the class in which our function decides. And I'm going to create an object of this class. Because unit testing, right? So now I'm going to use this object to call the square function. And I'm going to give some output, some input parameters here. So let's suppose I will give four here. So Omega naught squared function. And let's give four as an input. So if I will give four as an input to this function, it's going to give us some output, right? So let's store that output in the output variable. And since it is coming from the function, I'm going to name it as output underscore f. All right, so now we have, we're calling v running the test, and this will call the test function and Vf a scolding the output, which is coming from this function square. So similarly, if you have any other function in your main class and you just want to test it. You can just create an object and call it and store the output here. Now the next task, which is an important task, is to test the output. Right? So we're going to make use assert equals function, right? So this function is basically used to perform the test. In this function we will provided with two outputs, sorry, two input parameters. One is the expected output which you want that this function should give. So square root of 4 is 16. So I want that this function should give me 16 as the output. The second is what exactly this function is giving me, which is output F. So I'm going to pass output F fail. So now you see if you will run this square unit, not Java class, it will run this test function. And assert equals. We'll test that the output which is coming from the function which was inside our class. If the output is actually equal to the output of that V expected can see here this is 16, which is the output that I want this function to throw. So now let's run this. Just go one square unit and click on Run. So let's see what will be the output or egg. So it's saying that cannot find this on project will pop, can be run in there. Click on OK here, and just go here, learn all deaths in the selected package or source folder, and click on Apply and close it, and then again try to run this. So now we can see here, after running this, you will be able to see it says run one on one and you can see in green progress here. So this means that the output which is coming from this function is actually matching the output data expect. So now let's say, I expect that though this function square should give me 17 foursquare, I think that it is 17. And what I'm going to do here is I'm going to test whether the function is giving 17 or not. So in that case, I will run this again. And you can see here it says failures one and it shows a green, a red line here, which shows that the test was not successful because the output that is coming from the square function is not equal to what we expect. Now, we exactly know how to ask a function in our class without even running this classic intestinal function. Now what I'm going to create another function so that if they're, in most of the cases, in most of the programs, we have multiple functions. So I'm going to show you how you can actually run the test for multiple functions. So I have created a function sum here, which will give me though, which will return me the sum of two numbers that I will give it. So now I will create a separate test for this function, which is the JUnit test case. And I'm going to name it as some unit because it is the sum function. Then I'm going to click on Finish. And you can see it will create class some unit. Again, what we're going to do is we're going to follow the same method which we have done in the previous section. And instead of square function, we're going to pass some, and some expects to input parameters with just 45. So four plus five, I want the output that will come from this program as nine. And we'll save it and then I will run it. And again, I'm going to do this, run all tests in the selected projects, apply, blows and then run this program again. And you can see that it says that the test on the unit function is now done. So if you want to test any particular specific function, you just click on the glass and you run it. And after running it, you can see that it is running the test. So what will happen if you want to run all the tests? So let's suppose in this function you have n and maybe a lot of functions. And what you want to do is you want to, we run all the tests on all the functions. And so that you will be able to see clearly here which function is doing wrong and you can actually go on there. So for that we have J unit is sued. So I'm going to click Other here. Click other. Now here, I'm just going to minimize it. So it will open the dialog box with these options. Just go on Java, scroll down and go to JUnit, and you will see J unit test cod. So we're going to click here, we're going to click Next. Now you can see here it opens a dialog box like this. Now in case of package, we're going to go and browse. So we have the tests package, I'm going to click Okay here. So whenever I will click Okay, you can see it will show me all the test cases. So I am going to select all the tests, all the unit tests that we're running. I'm going to quickly click on the Finish. So it will create an old test dot Java class, which you can see, which will run all the classes inside this glass. So if I run this, you can see here it displays runs two by two. This means it goes running to test cases and both the desk cases have class. So this these are the two test cases which have paused. If I, if one test case fails, what we are going to do is if you will run the oldest. Now you can see it will show you that these two tests are not. There is some problem with these two tests. And it shows the failure. And then there is a failure in your desk, right? So in this way, you can actually do the unit testing in Java. And it can help you a lot in finding out the errors and debugging, right, so that's all for this tutorial. Thanks for watching.