Podcast Lookup

Java Encapsulation & Inheritance Explained | Packages, Access Modifiers & Super Keyword

The Java Developers Hub

  • December 3, 2026
  • 1 h 38 min
  • Episode 39
Episode page
About this episodeEncapsulation and Inheritance are two fundamental pillars of Object-Oriented Programming (OOP) in Java. They help developers build secure…
Encapsulation and Inheritance are two fundamental pillars of Object-Oriented Programming (OOP) in Java. They help developers build secure, reusable, organised, and maintainable applications.In this episode, we explore how encapsulation protects object data and how inheritance allows classes to reuse properties and behaviours from other classes.You'll also learn how Java packages organise related classes, how access modifiers control visibility, and how the super keyword allows child classes to access members and constructors of their parent classes.Whether you're a beginner learning Java or preparing to build real-world applications, this tutorial will strengthen your understanding of object-oriented programming.What You'll Learn
  • Understanding Object-Oriented Programming in Java.

  • What encapsulation means and why it is important.

  • Using private fields to protect object data.

  • Creating getters and setters.

  • Understanding data hiding and controlled access.

  • What inheritance is and how it works.

  • Creating parent and child classes using extends.

  • Understanding single, multilevel, and hierarchical inheritance.

  • Understanding why Java does not support multiple inheritance through classes.

  • Introduction to Java packages and their importance.

  • Creating and importing custom packages.

  • Understanding Java access modifiers: public, private, protected, and default.

  • Understanding the super keyword.

  • Accessing parent class variables and methods using super.

  • Calling parent class constructors using super().

  • Applying encapsulation and inheritance in practical Java programs.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Encapsulation & Inheritance Explained | Packages, Access Modifiers & Super Keyword

00:00—
  1. Speaker 1

    Welcome back Coder Army to our Java series and today we are going to start with a very important topic. It is called Pillars of Object Oriented Programming. Why is it important? We have understood the basic concepts of OOPs till now. What are classes? What are objects? What are constructors? How do we make these objects? Now we will have to understand which are the pillars on which object-oriented programming is based. Today we are going to talk about two pillars, which are encapsulation and inheritance. Once you understand this properly, then we will move towards abstraction, polymorphism and you will enjoy it more. So let's quickly go to the screen and see what they say. Yes, so because I said we are going to talk about pillars of object oriented programming, in which the first pillar is encapsulation. Pillars doesn't mean anything. What we did is we put real world entities, real world objects in our program. We started to write it. We started to mimic it. Now we just want that the objects of the real world. The properties that we show. We can mimic that too. And their first property is encapsulation. Okay. What does encapsulation say? So first of all we will see what this word means. If you look carefully. So in this word comes en and then capsulation. Capsulation means a concept of encapsulation. That means to put something in a capsule. Okay. To form a capsule. Okay. And do you know what a capsule is? If we talk about a medicine capsule, there are some parts in it Medicines, some parts have different medicines, etc. And everything is well put together in a wrapper. Right? So, from here we take this concept further. We say that in our real life objects, their behavior and their data is encapsulated. What does it mean? Look, suppose we take an example of a real life animal. Okay? Any animal which is our real life object, we take its example. We know what kind of data an animal can have. Let's say it can have a number of legs. What else can we have? What type of animal is it? Carnivores, herbivores, etc. So this can be our data of any animal. So I write it as data. And we have some of its behaviors. Behaviors like an animal can eat, walk, run, etc. etc. It could have a lot of behaviors, so I'll write it as behavior And we used to substitute these two in our object oriented programming We used to make the data as variables And we used to make the methods as methods And by combining these two, we said that we have represented an object Now you think Is the data of the animal and the behavior of the animal the intrinsic property of the animal? If I represent an animal in a way that it is an animal, then it has all the data in it. And it has all the behavior in it. This is its intrinsic property. It is not an external property. So if I want to mimic it in programming as it is. So in programming, the container that I will make to represent this object. It should have all the data in it. And it should have all the behavior in it. Right. Which I am still doing. How? I am a... To represent an object, I create a class. I write class Animal. And what do I do in this class? I add all its variables. For example, I added int, number of legs. Or I added string type. So what did I do? I added its data. These are the data. And how did I add its methods? I made methods here. Void eat. Suppose I made a method. I can make another method like this. Void Let's say run. Right? So I made multiple methods like this. Right? And what did I represent? What did I say? What does this represent? My behavior. In the same way, I represented an entire object in my code environment. So encapsulation says the first thing. That your data and your behavior. Because it is the intrinsic property of an object. So it should be encapsulated in an object. In a class. Which we were already doing. So that is not a problem. So we were already following encapsulation. We were following 50% but we were not following 50% yet. Now I will tell you what encapsulation means. So we will write it properly first. So encapsulation. Tells you two things definition wise. First that both data and behavior. Data and behavior. Should be encapsulated. Should be together. Or we can use encapsulated. Encapsulated. Within an object. So we did this. How did we do it? We made a class. We put data and behavior together. But it also says. We should provide. Sorry. We should not provide. We should not provide. unrestricted access of our data unrestricted access of data kya matlab iska We should not provide unrestricted access to our data. Okay. Now, let's understand this. We understood this point of encapsulation that our data and behavior should be encapsulated together. And we were already doing that. We were already following half encapsulation because we used to make a class. We used to have data in it. We used to have all our behavior in it. Very good. But we didn't understand the second point of it. That we should not give unrestricted access to our data outside. Okay. Let's understand this. Let's talk about a real life object. This time I am talking about an object which is not physical. But it is a real life object. For example, bank account. Bank account. This is a real life object. Everyone has a bank account. And I said that we can treat everything as an object in programming. If we make an application of a bank, then what do we make to mimic it? If we make an object of it, then we would write class bank account. We could do this in programming. In real life, I said a bank account exists. In our bank account, there is a balance which tells us the current balance. Now what is this? This is just a data. So let's say I have to represent this object. Now obviously it could have more data. Let's say it has our bank account. Which bank is it in? So bank name comes. Apart from that, our IFSC code, etc. etc. There could be a lot of data. But let's say I represent this data in my programming. So what do I do in programming? What we have learned till now. I make our class simple. Bank account. Bank account. Obviously. And what all things we can store in it Whichever we want to represent here So let's say I keep a balance as double Because our balance can also be in point So I stored it in double Apart from this let's say I keep our bank name in string Bank name Right? Or if I had to represent some other variables, I would have done it. But let me tell you one thing, I have already programmatically represented a real life bank account. Right? But don't you think there is a problem of access here? The problem of access is that any user will make an object of my bank account. And can change this balance anything. Means tomorrow I can write in my programming that if I want to make an object of a bank account. So you know what I write? I write bank account. Its reference object let's say BA equal to new. Bank account. Let's do it here. This is our syntax. Now, no user can do this. Let's say, 1 lakh for BA.Balance. He set his balance to 1 lakh. But this is wrong. This should be programmatically wrong. No one will set his balance to anything. I will do 1 crore, 2 crore. Right. But I can do this. Because I have balance accessible. I have authority. No one has authority to change anything in this balance. Okay. But is this a real life property? Can we change our balance in our bank accounts in real life? No, we can't change our balance. If we want to increase our balance, we have to deposit money. And if we want to reduce our balance, we have to withdraw money. That means in real life, we have two behaviors. One is our deposit behavior. One is withdrawal. I don't make a bracket here because we represent behaviour in methods, in our code. Here I am just telling you that these are two behaviours. What is this? Let me write it down. This is our behaviour. That we deposit, we are doing something. To do something, we call it behaviour. Or we withdraw the money. And by doing these two things, our balance changes. Not like we directly update the balance. Right? Absolutely right. So why don't we mimic this thing in our programming? Till now we were just mimicking that whatever bank account is, we made an object of it, made a class and whatever data it has, we will write it here. Whatever its variables are, methods are, we will write them here and our work will be done. But this is not the right thing We don't want to give this unrestricted access that we have given outside I don't want anyone to make an object out of it and change this balance I want everyone not to be able to access it So how can I do that? Basically, my second property of encapsulation How can I make it follow this second property? So we will see this in our code And for this, first of all we have to understand What are access modifiers? Because of this, I can restrict the access of those variables in my code. I can avoid unrestricted access. So we have to understand access modifier. Now look, access modifier tells us only one thing. Who have access to? Who has access? What kind of access? It can be of any thing. Whether it is of any variable. Whether it is of any method. Whether it is of any constructor. Or even if it's a whole class. So basically, we have access modifiers that tell us who has access to a variable, a method, a constructor, or a class. These are some keywords that we will use and control our access. In Java, we have four types of access modifiers. How many types? Four types. So there are four access modifiers in java and we will understand them one by one. First I will list them down. One is public, remember these are all keywords. One is default, one is protected and finally we have private. These are the four keywords in java which we call access modifiers Which manage the access of any variable, method, constructor or class Which we just saw above Where do we put them and how do we work with them? Now we will see that So let's take our account class So I said we had a class Let's write it like this We already had a class whose name was BankAccount. So I'll quickly make this BankAccount. And what did I say? In this, we had a variable named balance. So what did I say? Double balance. Okay. Now we ignore all the other variables. We had IFSC, bank name, branch name, etc. Whatever it was, we ignore all of them. And let's just take a balance field. Okay. I had said that if I had made its object till here, like this, I would have closed it here. So, anyone would have come and made its object bank account, which we had done above, BA equal to new bank account, would have written like this. And he could have accessed BA.balance and changed it. Anyone could have put any value here, like put 1 lakh rupees. This was wrong. So I want no one to be able to access this balance outside. So for that, I will put a keyword here. Private. As soon as I put a private keyword here, now what happened? No one can access this balance outside. Now if someone makes an object of a bank account, BA, and tries to do BA.balance, Compile time error will come which will say that you can't access balance because it is a private member. You can't access private members outside this class. But we have seen that there can be 4 types of accesses, private, protected, default, public. Now we will do all these and understand one by one what is the access of all these. But I have told you where we will put it, all the keywords will be here, put it in front of a private variable, you can put public here or you can put protected or you can put default. I could have put it in front of a constructor or method or even in front of a class. I could have written public, private, default or protected. Now let's see how they are accessed. First of all, we had Private. Let's start with private. Private access modifier says that any field in which you have added private, you can't access it anywhere else. You can only access it in that class. What does it mean? It means, as we saw above, that we made a class by doing bank account. Let's call it BA. Okay. And I had kept a field in it by doing double balance. Okay. Something like this. And if I added private in front of it, This is not P capital, this is small. If I put it in front of it, private. So what will happen? This balance can't be accessed outside the class. Who can access it? Only the methods inside the class. Or the variables inside the class. So let's say if I make this class a little longer. Till here. And there are some more things in this class. For example, if there is a method. Let's say the name of that method is withdraw. Sorry. Let's say the name of that method is withdraw. So, here a voidWithDraw is a method. What can it do? It can play with this balance if it needs balance. So, let's say it made balance minus minus. We will see this logic later. For now, let's assume that it is using this balance field. It made balance minus minus. So, it can access this balance field. But, outside this class, no one can access this balance. This is the most restricted access. that there is no class level access, basically no one can access this field outside the class. The field in front of which you have put private, now you can put private in front of the field or in front of any method. If you put private in front of this method, then no one can access this method outside the class. Means no one can write, if an object of ba is made, let's say small ba, then ba.withdraw. No one will be able to call this. No one will be able to call this if you put private here. Okay? So I hope you understood private access modifier. Now let's move towards our next access modifier which is default. So our next access modifier is default. Now see, if you have put default in front of any field, then who can access it? Everyone can access it even from outside the class which comes from the same package. Now what is the package? We haven't read this yet. No problem, we will read that too. First let's write Anyone can access belonging to same package. Belonging to same package. Okay, brother, what is package? We haven't read anything about package yet. Okay, let me tell you. Let's say we had, what we used to do till now, when we used to write our code, we used to write class, let's say I made a class, demo. Okay, this class does something. Let's say I made another class, let's say class animal, I made it in this file, it also does something. Now, in any one of these classes, my main code will be there, so let's say it is in this. Main means my main function. So let's say my void main function is in this. I am writing the entire syntax now, but let's say, in any one of these, my void main function will be there. So it is in the demo. And in which also void main function will be there, we have seen that in boilerplate code, it is always public. Here it is written as public. We will understand why it is public later. But here it is written as public. So for now, this is our public class demo. We have made another class in it, animal. Now in this file, we can make many classes if we want. We say, what is package? Package is nothing. Package is a kind of folder in which you can store many classes together. So basically, if we see its Java definition. So Java definition is, if I write it like this. A package. Group. Similar. Classes. And interfaces. We haven't read interfaces yet. We will read them later. Together. Okay. So the package is used to group similar classes and interfaces. Now we will see how to make it and how to use it. Now let's go ahead and complete all the access modifiers. So I said that there is a default access modifier that only people of the same package can access. If we move towards the next access modifier, then ours is protected. Protected. Okay. I forgot to tell you the special thing about default. If you don't put anything in front of any field. So let's say if you made a class. Bank account. Okay. Bank account. And it has only one field. Balance. Balance. Okay. And let's say double balance. This is written. Now if you haven't installed any access modifier before this, then by default, this is the default access modifier. This is by default. It's not like you have to always install default, this is optional. If you don't install anything, then this one will always be installed. Okay, very good. Now our next access modifier is our protected. Now protected says that any field to which you have marked protected, anyone can access it, which will either be in the same package, or in its inherited class. Okay, till now we haven't studied inheritance. We will study inheritance later. No problem. But for now, you understand inheritance will be our next topic. But suppose, in which ever field we will put protected, it can access classes of same package. Or it can access inherited classes which are also available in other package. Again, we will see this when we will understand inheritance. And last is our, which is most unrestricted, that is public. Now public access modifier says that anyone can access this field Anyone can access this field So if you put public in front of the class that we just took So it can access the same package or different package or inherited class or non inherited class Anyone can access in the whole code base So this is the most unrestricted So if you understand all these access modifiers, obviously we haven't seen protected yet because it belongs to inherited class and we also have to see the packages. But apart from this, if you understand how their accesses are getting broader, the strictest was private, the least strict was default, then less protected and then the least strict was public. Now I'll tell you an important thing, all the access modifiers that we have, I'll write them all down first. So I wrote down all my access modifiers together, private, default, public, protected. And what did I tell you? Their access is getting broader. The most strict is private, then less, then less, and the least strict is public. Now the most interesting thing, if you're talking about any real world code, So the most used access modifier is private and public. Apart from these two, our default and protected are very less used. In fact, default is not used at all because what does default mean? It's the same package but it doesn't make any difference. Either you say that it's very strict, very private or very public or you say that it can access inherited classes. So default is used the least in production but because theoretically it exists in Java, we will read all of them one by one and see the code of all of them. So now I wanted to tell you one thing that think about it, I said that all these are access modifiers and they can be applied in front of a class, in front of a variable, in front of a method, in front of a method and in front of a constructor. They can be applied in front of all these. Now think about it, variable, method, constructor are fine, they all come inside the class. Let's talk about the class first. I can write it like this, public class and any name of the class, let's say demo I can make a class like this In fact, we have seen such classes till now One which is our main function, like if we have a main function in it, void main The class above it used to be always public This one used to be public So what does public class mean? It's a simple thing, public class means that this class is accessible anywhere So anyone can access this class in the whole code base But I am telling you that there are two such access modifiers which I can't put in front of this class. And which are those access modifiers? One is your protected and one is your private. I can't put this private and protected in front of this class. Basically, if I say, I can neither write private here, I will get an error, nor I can write protected here, I will get an error. This is not allowed in Java. Why? Before understanding the Q, let me tell you one thing. Private class can be the same type of class which is nested class. We will study that later. If in a class A, I have made another class which we have not studied yet but we will study later. So I can private this internal class. But I can't private this root level class. So let's understand private first and then protected. Why can't I private? Understand. What does private access modifier mean? Private means that no one can access it outside of a class. But this is a class itself. And this is a root level class. Let's say class A is a root level class. There is nothing outside of it. There is only a package outside of it. I said that there are multiple classes in a package. We will see that now. There is only one package outside of this. So there is no sense outside of this. I will ask that private to what? In whose respect is this class private? It should be private in someone's respect. But there is nothing above in its respect. This is the highest root level class. So that's why private does not make sense here. Right? Now the next one is protected. Now this class can't be protected either. Because again, protected just means that you can access in the same package or different package if you are its child class. But the concept of child class, the concept of inheritance, whatever we will see in the future, is applied in variables and in our methods. The concept of inheritance is applied to the variables and methods in the class But there is no concept of inheritance in the whole class So that's why we can't apply protected here So neither we can apply private nor protected What we can apply is either public or default In fact, we were applying default till now For example, in the same class, I can't apply private class So what can I apply? Public So let's say I applied public here If you had a public class demo file, then we would not put anything in front of this class. And what I meant by not putting anything is to put the default. This is by default access modifier. So we always put default. Apart from this, we can also put public. Okay, very good. Now, if we understand all these access modifiers, then we go back to our encapsulation and understand how we implement encapsulation in our programming world. Okay. So, again, let me take an example of my bank account. I had made a class called Bank Account. And what did I say? Let's say there is only one variable in it, which is balance. So, I write double balance. Okay. Incapsulation says that our data and our behaviors should be encapsulated together Which we always do in the class, here I can write methods that are encapsulated in the class And the second one says that the excess should be restricted so that no one can change the balance field And now we have read the excess modifier, which means I can put an excess modifier in front of this variable like private If I put private here, then no one can access this balance outside. That means, if I try to make an object from here, then I will write a code like this. Bank account BA equal to new bank account. Right? Okay. Now if I write BA.BALANCE and try to set it. First of all, as soon as I do BA.BALANCE, my compiler error will be lost here. That I can't access a private member. It means that I am not able to access this balance outside. Okay, very good. So I listened to the encapsulation that I don't want to give unrestricted access. So I put private in front of it. Okay. But how do I change the balance? I can't directly access the balance. But I will have to increase or decrease the balance of this bank account. Simply, a user can't put 1 lakh in the balance. But he can withdraw or deposit money in his account. In that case, encapsulation says that private any data. And give it access through behaviors. Meaning through methods. So what can I do? I can do this. That I can put two methods in my class. Which we saw above. One would be. Let's say deposit. Deposit. Which I will call. Let's say here I will take a parameter in deposit. That how much money are you depositing. So I took amount. Okay. And. I will take my balance. I will increase it by that amount, plus equal to amount. Something like this. And what can I do with the same? I can make another method, void withdraw. Let's write it like this, withdraw. And again, how much amount are you withdrawing? So I will take amount here. And what will I do? Again, I will reduce my balance to that level, minus equal to amount. Something like this. So, if I have made two such methods, deposit and withdraw, I can change the balance through this. Because look, this can access the balance, because private fields can be accessed inside the class. So, basically, I can play with this deposit and withdraw through this balance. Directly, I cannot access this balance outside. So, our encapsulation says this. If you want to remove unrestricted access, then you can put private in front of any field and make public the methods through which it can be accessed. So I put public in front of this field. This is also public and this is also public. Public means that I can access them from the outside world. Now basically understand what will happen that no one will be able to call BA.Balance. This is not allowed. But what will he be able to do? He will be able to deposit some money. Let's say he has deposited 500 rupees. Your balance will increase from 500 rupees. If he wants to withdraw something, then he will be able to withdraw BA.Balance. say he has withdrawn 300 rupees, then our balance will decrease from 300 rupees. So he can do all this work, but he can't directly change the balance. Now an important question arises, what is the difference? You said that they should not set the balance as 1 lakh directly, so we made it private. But we gave two methods here, deposit and withdraw, through which they can also access the balance. We did not have to stop the user from accessing the balance, we just had to stop him from accessing it directly. Obviously, when you go to ATM, you deposit your money. So, this method is the same. Now, obviously, I have written simple here. Increase the balance with amount or decrease the balance with amount. But obviously, it will not be like that. There will be a lot of validations in it. Like the amount you have put. First, the ATM will check whether the currency is correct or not. Whether the note is correct or not. Then the notes that will come will increase the balance from that amount. Now obviously when you are going to withdraw, then your ATM will first check that how much amount you have put in your account, that you have to withdraw 300 rupees. Is there 300 rupees in your account? If there is less than 300 rupees in your account, then it will not allow you to withdraw, it will fail here itself. So now I have written directly in the method balance minus equal to amount, but actually when we will write business logic, So we will put a lot of validations in it. We will first check if the amount should be greater than or equal to our balance. We will go ahead after that. So basically what we are doing is that we have put restrictions. apply some restrictions whenever the user withdraws from the deposit, then we go and tease this balance variable. We don't directly ask the user to set any of his balance. Right? So, this is the whole concept of encapsulation that if you If you want to access anything from outside, then give the user restrictions, give him behaviors, give him methods, that you can access through this, you can't access directly. Now, as I made a class of a bank account here, and gave this balance variable through access, through deposit and withdraw. Now, if someone wants to check their balance, let's say, he doesn't want to deposit or withdraw, he just wants to check how much is the balance of his account. So, I can give some more methods here. So, I work, I make another function of mine, let's say, public balance. I'm happy public access modifier will come because we like to make our methods public And if I want to check this balance I want to tell the user how much is the balance So I want to return the balance So let's say I write return type double And the method name will be let's say getBalance It won't do anything It will simply return balance Something like this So, what did I do? I made another new method, getBalance, if the user wants to check their balance. Now, what can the user do from outside? The user can do from outside, ba.getBalance. Now, As soon as a user calls this getBalance method, he will get his balance update. But we just said that we don't want him to directly access the balance. He won't be able to do that. Even now, when he calls this getBalance, he can only see the balance. See what is written here, returnBalance. He can't change anything in this balance. He can't assign any value equal to getBalance. Because the work of this method, as soon as you come into this getBalance method, it simply returns this balance. So it returns this balance. You can't change anything in it. Now I made a method named getBalance. I could have made setBalance like this. But I don't want to make that. Because setBalance means that set something directly to the balance. But I don't want that. So I just made getBalance here and left it. In fact, getBalance, setBalance or any other variable like accountNumber. So getAccountNumber, setAccountNumber. We call these methods getters and setters. In fact, the most used things in encapsulation are getters and setters. We will see an example of that in the code, how to write getters and setters properly, what is its use, and how they are beneficial for us. But I hope you have understood the whole concept of encapsulation properly, which says two things, how everything should be capsulated, both variables and members, and not giving unrestricted access. For that, we use access modifier. Incapsulation means to private the variables and give them access through public methods. Either through getter setters or through our normal methods like deposit, withdraw, functional methods. So I hope you understood all these things. Now let's go to the code and see all these things. So again I have made an empty project. So I will write on it that what we are going to understand here. Here we are going to understand encapsulation. Encapsulation. Okay. So first of all, let's do one thing. Let's make the class of the account that we saw. So let's say I made a class named bank account. And inside that, I have a variable double balance. Okay. Something like this. Right now, I'm not doing anything. I'll leave everything empty. And let's see. What did we do directly till now? We used to make an object of bank account. Sorry. Not this. We used to make an object of bank account. Let's say BA equal to new bank account. Now, what could I simply do? BA dot balance equal to let's say I have set a value of 0.00. In fact, if I set this value, then I can print it. If I write system.out.println and simply print it, ba.balance, then my print will also be done. So, did I directly update my balance? Which is very wrong. So we read access modifiers. So what I will do here is I will put private here. As you can see I have put private here. Here error came. Now what is the error? The field bank account balance is not visible. For this the visibility is finished. Because what happened to this field? private. Now to access it, I made two public methods. One, I made public void deposit. Okay, what will it do? It will take an amount and as soon as it takes an amount, we will add that amount to that balance. Now I am not applying any validation. I am simply making it to explain what to do. Same, I will take another public method. Let's say withdraw. Withdraw. Okay, what will it do? It will take an amount and reduce our balance to that amount. Here also I am not applying any validation. Now, let's take another public method. Not public void but double. GetBalance which will show the user's balance. It will simply return this balance. Now through getBalance, it can only see the balance. It can't set it. Now as soon as I did all this, I can't directly access the balance. But I can do this, that BA.deposit, let's say first I deposited 500 rupees, very good. Then BA.withdraw, then I needed 300 rupees, so I took out 300 rupees, very good. And finally, what did I do? Let's do system.out.println. And let's check our balance. So what can I do? I can do BA. Get balance. Something like this. So if I put 500 and subtract 300, then my 200 balance would have been saved. So if I run this code, then my 200.0 balance would have been saved. Very good. So we saw this concept. Now what did I say? That such functions with which we can get or set something, we call them getters. Or our setters. Something like this. Now this will be a very good use case If we look at our student class In that we can make a lot of getters and setters Because in this we couldn't make a setter Because we shouldn't have set a balance So let's do one thing, let's leave this class as it is And let's make a new class We make a class, let's say a student class And I say the student has some information Like he has a name He has let's say a roll number He has let's say an age And it has the name of the college. In this way it has information. Now again if I use the concept of encapsulation. Then I say I make these fields private. In encapsulation we will make most of the fields private. I made this also private. I made this also private. And I made this also private. In this way. Now, if I private it, then no one can access it directly. So, let's make a constructor. Because I want it to give some value for the first time. So, I made a constructor named student. Now, you know what we do. We take name, int role number, int age, and string college. We take all of them here. And we can set all of them. So, this.name equal to name. This.name age equal to age this.roll number equal to roll number and this.college sorry college equal to college something like this so first of all I have created a constructor so that it can give any value by starting now I will make some getters and setters so here I will write getters and setters. So that I can get and set these methods. Because it is private, no one can take these values directly. Public void getName, I will make one or two fields and show you, you will understand everything. Now what does getName do? It returns you the value of the name. So return name, now obviously I am returning it, so here, sorry, now because I am returning here, I will write string instead of void. Because I am returning a string value. Now, if I have made a getName, then I can make one public, let's say void setName. What we will do is, we will take a name and we will set it here that this.name is equal to name. Something like this. But now you must be thinking that we don't have to give access to name. But by using get name and set name, now user can get name and set name. So what's the benefit? Why don't we make it public? Then also user can get and set. Now think. Because we have set or get it through a method. How many validations can we apply here? Assume. Name will not have any normal validation here. But think about the college name. I want that whenever a user will put the name of his college. So it should be a college that exists. Now I can't put any random ABC college. Okay. That college should exist. So in its setter. The method of set college name. I will put some validation in it. That this college should exist. What will I do? I will access all the colleges in the world from the internet. I will bring their list. I will find in their list. Is there any such college name that this user has passed in the parameter? If not, then I will throw an error. Because no such college exists. I can only call an API. Basically, business logic depends on what I can do. But because I have accessed them through methods. I have a lot of control. Because what does someone write directly in college? Let's say s1. college is equal to abccollege. abc does not exist. But let's say I make a getter setup of this. Let's say I make a getcollege and a setcollege. Something like this. Okay. Getcollege will give you college. And setcollege will take the name of a college. And it will set it in this.college. Equal to college. Something like this. Now you think, because this method is setCollege, I am not setting it directly. But here, I can add validations. So here, college should be real. Now here we can obviously call an API. Basically we can access the entire list of colleges from the internet. And we can search whether this college that the user is giving is valid or not. Then we will set the college. So we have a lot of control. Now no user can set this college directly. Same as I have made getName, getCollege, getSetCollege You can make getAge, getSetAge, getRuleNumber, getSetRuleNumber in the same way We call all these methods getters and setters And through this, you access all these variables Now you have control over how many validations you want to put in any setter Now if you want to get any name, it can do anything But when a user is setting a name, there should be two things in that name FirstName and LastName Let's say you have this rule, so you can apply it here. Now obviously you can apply validation on age. Now obviously no one can write their age 200. So you can apply an upper limit like a date of birth. You can apply a limit of role number because you know that in your college, the number goes up to 100. So 101 cannot be a number. So where can you apply all these validations? In their setters. Where can we put this validation in their setters? Like we put it here in set college. Here we can put validation that college should be real. So here our encapsulation is very useful. Basically what to do? Private all your fields. And make getter setters for them. Or like we made here in the bank account case. Deposit or withdraw. Make some methods through which you can access them. And in those methods, you can apply some validations. Like here, you can apply validation in withdraw. That brother, your amount which you want to withdraw. Amount should be greater than or equal to. Balance. Right. I have written it upside down here. Should not be greater than. Should be less than equal to. Amount which you are withdrawing. It will always be less or equal to balance. So, you can apply some such validations. So, I hope you have understood all the concepts. You must have understood the encapsulation, you must have understood the access modifier, how they are. Till now I have only made the bank account object, you can make the student object here and check it. Now let's understand a little more about the packages. So look, to explain you about the packages, I have made the demo2. java an empty file in which my demo2 and my main function are. First, I'll take you to my folder structure. My topmost folder is video16, which has two files, demo and demo2. java. Demo.java is the same file that we used to see in bank account and student. And demo2 is the one that I made empty. First of all, in this video16, I'll make another subfolder in this folder. Let's say I call it school. Similarly, I'll make another folder in video 16, which I'll call college. So, I have a folder of school and a folder of college. Now, in both the folders, I'll make a file in the college folder, student.java. And in the same school, I'll make a file, student.java. Something like this. Now, since these two files are in different folders, their names can be the same, right? student.java, student.java, because both college and school have students, so I made a file in both of them. Now you understand, as soon as I made a file, see what my editor did. First of all, I go to student.java. Which student? I go to college student. So, he himself wrote package college. He included this line himself. In fact, you see, this line is written even before the public class student. Till now, when we used to write code, our first line used to be this. Which used to be our class, right? But now, see, suddenly, our first line has become college. Okay, now let's understand what this means. This means that this package college is telling that this college folder, whatever file you keep making in it, will all belong to a package whose name will be college. You can make a lot of classes in this college package. It will represent the same package by combining all the classes. Same goes for school. Now, as this is a school, this is a package. In this, all the classes that you will make, like we have made a student class, that comes in a school package. I can make a teacher class in this, that too will come in the school. Same, I can make a teacher in this too, that too will come in the college. Now, let's see its use. I will do one thing. In this college package, the student class, I am not writing any main function in it. In that, I will make a function void print. Nothing else happens. Here, I will print system.out.println on the screen. Let's say school student. school student, something like this. Okay, now I'll make a function like this. Sorry, this was not for school, it was for college. I wrote school here. Let's make it college first. College student. And what did I say? I have another student class in the school package. So I'll write the same print function in it. And this time I'll print it. School So I hope you have understood what we have done so far. There is a package school in which a class is a student, which has a print function that prints a school student. Similarly, there is a package college, which also has a student class, which has the same print function that prints a college student. Now, I want to make the object of both classes in the main function of demo2. java. This student and this student. One is a college student and the other is a school student. But the names of both the classes are the same. So, first of all, how do I tell Java that the demo2. java that exists outside of these two folders. School is here, college is here, demo2. java is here. How do I tell Java that I have to use the classes of college and school in demo2. For that, I have a keyword which I call import. Import. Now, those who come from C + + background will know that we write hashtag include, right? That means we are including some pre-processor directives. Those who don't know, no problem. In Java, we just write imports instead of that. So, I am saying, what do I have to import? I have to import this college student. So, first of all, I will write import package name. What is the name of package? College. Okay? And after that its class name is student. So here syntax is package name dot class name. So let's write it here too. Syntax is import package name dot class name. Something like this. So I wrote college dot student to import. Now as soon as I imported it. What can I do? I can make the object of this student class. S1 equal to new student class. Now I can directly write s1. print As soon as I wrote s1. print, an error came up. The method print from the type print is not visible. Exactly, why it didn't become visible? Because I didn't put public in front of the void print. And if I didn't put public, then by default, default is used. This keyword is used by default. And default means that you can access this print function in the same package. Now this package is college. In this, I will make all the classes. Let's say if I make one more class here, teacher. If I make another teacher class here. It comes in this package. Now it's not necessary that this teacher class should be in this file. Even if it's in this file, it'll be in the same package. And even if there's another file in this folder, it'll be in the same package. So I'll do both and show you. If I make the teacher in the same file. And let's say I want to call the print function in this. Let's say there's a function in this. Void. Print. Now this function does nothing. It calls the print function of the student. So I can do it here. Student s1 equal to new. New student object and print call s1. I can do this. See, there is no error. Because this teacher class comes in the same package of this student class. And it is not necessary that I keep it in the same file. What should I do? In this college package, I make another file. In this college package, I make another file. By doing teacher.java. Now see, teacher.java still has package college written on top. This editor writes by itself. So what I am doing, whatever files are being made here, student.java, teacher.java, I am making everything in the directory of this college. So my package will always be college. I can write the same code inside it. Sorry, I didn't want to copy this whole thing. Let's remove this. Yes. In this also I write the same code that the object of the student is s1 and s1. print. I didn't have to import anything here. Because this teacher.java and this student.java both come under the college package. So I can make the object of this student directly in this teacher. And I can also call the methods of that student in this teacher. So I hope you understood this thing. Now let's leave it like this. There are students and teachers in the college. And there are only students in the school. You can make a teacher here if you want. But let's leave it like this. Now let's go back. I will cut this teacher class. Let's go back to demo2. java. I said that I am able to make an object of student here. This line is correct. Why am I able to make it? Because our student class is public. Now the class can be public. Public means anyone can make an object of it. From anywhere. Okay, I made it. But I can't call its method because its method is default by default. And default means that you can use it in the same package. So where can I call the print function of this student class in the same package? So I called it in the teacher and showed you. But I can't call it in the external package, for that I have to make it public. So, I will write here public void print. Now, see here the error is over. Now, as soon as I print s1. print, what will be printed? Which student will be printed? College student. This will be printed on the screen. Why? Because I have imported this package. Import college.student. Now, let's run this. College student is printed. Very good. Now, what will happen? If I comment this line. And I write another line. Import school.student. I have imported the student of the school package. Here I have written school dot student. Very good. Now as I have written import school dot student. I am again making an object of that student. This line will remain the same. The name of both the classes is student. Now here also error has come in s1 dot print. Why? Because again it is saying that it is not visible. Because I have not put that there too. This is my school student. Here also I have to put public. So what I have done is I have put public here too. Very good. Now I can make an object of this too. Now when I will print s1 dot. So what should be printed on the screen? School student. So, let's see. School student. Very good. Okay. What will happen? If I keep this import as well as this import, then there will be some error. So, here the error is telling that the import school.student collides with another import statement. So, which student do I want to make an object? This he will not know. So, first of all, I cannot write import. Okay. I will comment this. Now obviously I am making an object of college, but I want to make another object of school. So what can I do? I can write a complete statement. First I will write the package name school.class.student and in this way I will make an object of it as to equal to new again our school.student Something like this. So what I said till now is that the syntax is the name of the class. Now I said add the package name to the syntax. So package.class s2 equal to new package.class. Now if I call on s2, I will call on s2 my print function. So first my print will be this one college student. Then my print will be my school student. So let's do this once. See this college student and then school student. Very good. Okay, but I can also comment on this line. And if I write like this here, college.student equal to new college.student, then also my entire code should run. I mean, if I want to avoid these imports, I can write the name of the entire package directly here. college.student, school.student, like this. And still, I will get the same output. College student, school student, right? This is a very interesting thing. Okay, I hope you understood this. Okay, one more thing. I can also import college.student and I will write college.star instead of college.student. So now this means that what I did is that I imported all the classes in this college package. Now I can make any class object in that college package. Whether I make it of student or teacher. In this, there is a student in college. College has a teacher here. Make it of a teacher, etc. Okay, and one more thing. When I didn't write anything here, let's say, I'll remove this right now. When I didn't write any import, I said that I can directly make an object by calling it college.student or school.student. But this is true only if my college, school and demo2. java come together in the same folder. Only then my IDE will be able to recommend it that you are talking about the same package. But if my module is different, suppose this is video 16 and this is video 17. If I write college.student in that, then it will throw an error here. It will say that I don't know which package you are talking about. So I have to introduce the package name above. Okay. So I will remove these lines. Sorry, I will not leave these lines in the comment. Instead of these, I can write something like this. import college.star Dot star means I have imported all of its classes. And like this import school dot star. So I have imported all of the school's files too. Now when I have to make an object, I will simply write college dot student, school dot student, etc. So I can do something like this. I hope you understood how we use imports. What are the packages in Java. Now let's go back to the screen. So I hope you have cleared the packages. Now I am saying that the package we just made. These were our user defined package. User defined package. We created our own packages. But there are some packages in Java which are inbuilt. We call it inbuilt package. Inbuilt package. So Java gives us a lot of inbuilt packages in the language. Let's see which packages are there. We have a java.util package, a java.lang package, a java.lang package, In this way, we have a lot of packages of inbuilt Java. Now, what is java.utilPackage for? This is for our collection frameworks like ArrayList, LinkList. LanguagePackage is imported by default. to import the link, etc. And IO is used to import the input and output. These are the inbuilt packages of Java that we can use. Suppose we want to use the array list of Java, which we haven't read yet. But when we read it later, we want to use the array list. So we will use the java.util package. So what will we write? We will write import java.util in our code. This is the name of your package. Then what did I say? Dot and then the name of your class. The name of your class is array list. So you can write java.util.arraylist like this. Or you can write java.util.star. And star means that you have imported all the classes of that particular package, java.util.package. So this means that we already have a lot of packages in our java libraries. We call this whole java libraries. In which you already have a lot of packages written. You just have to import it in your code and use it. Whether you want to take user input, whether you want to do any collections, framework use, etc. So let's go a little deeper and see how it works on JVM level. It's very simple. You understand that this is your code. You have written code. So in this, there will be a line at the top. Let's say import, you would have written java.util.arraylist. So you did nothing. You wrote the code of arraylist. You have to import the ArrayList class. Then here you must have a public class of your own. Let's say public class demo. In that class, you must have a function. Let's say main function. Something like this. And you must have written some code in it. You can obviously make an object of this ArrayList. You work with it. But now I am telling you. What will be the name of this file? Let's say demo.java. This will be the name of this file. Now, the name of this file, demo.java, what does it become when it is compiled? Your file is compiled and becomes demo.class. And this is called bytecode. We have seen this before, right? So, internally, as soon as your file It will be compiled and converted into bytecode. First of all, your Java compiler will check which import lines you have written. So, first of all, you have written java.util, this is your package. And your class is ArrayList. So, it will bring the. class file of ArrayList.. class file means its bytecode file. ArrayList itself will be a class. Understand it like this. ArrayList will be an inbuilt class, class ArrayList. This java must have already made it Now this class must have been made by compiling it and also by making its bytecode So a file of this must have existed, dot class The full name of this file would be ArrayList.class So, Java compiler will load this ArrayList.class so that whenever we use this ArrayList in our code, there should be no ambiguity. Our code should run easily and we should know which ArrayList we are talking about. So, all this happens internally. The JVM, Java Virtual Machine, loads the ArrayList.class file in the memory and we know that whatever operation we are going to do in the ArrayList, add element or remove element we will see later how to do that but whatever function we perform on it we can do that easily because it knows that I also have the code of the array list. I have its bytecode. This is the whole package. We use it a lot in production. In fact, every Java file, Java class comes in a package. So, I just showed you how to make a basic package. So, first I made a package of school, then a package of college, and in both of them, I put a file named student. Okay? And our class also became student. This is how I made the package. Generally, your package is made on a hierarchical level. So let's say first there will be a folder called com, then another folder and so on. And you will keep the name of this package like this. This is the name of such a big package. We will see all this when we move towards Spring Boot. Now in Java, we assume that what we have understood about packages is enough. When we look at the rest, we will understand more in detail. So now we move towards our next topic, which is inheritance. Okay. Inheritance. Now what does this say? This is also a beautiful topic. Now let's first understand inheritance from a real life application. I had told you, let's suppose we are denoting a real world object. So this is also a pillar of OAPS. So obviously, this means that this property will follow in real life. So what happens in real life? Let's say we have an object, a vehicle. And we have an object, a car. So, in real life, we say that these two objects are somehow related to each other. How? This car is a child of a vehicle. Child means, if I denote it like this, then child means that every car is a vehicle. But obviously, vice versa is not true that every vehicle is a car. A vehicle can be a bike, a vehicle can be a cycle, etc. etc. But car, because it's a vehicle, so every car will be a vehicle. Right? So, the relationship between these two, we call that relationship is a relationship. Is a means, if I say it, then I say it like this. Car is a vehicle. Okay? If I make another relationship here, let's say, I make a bike here. Okay? So, this would also be is a. Because I could have said, bike is a vehicle. Right? Similarly, you can see another example. In real life, you have a student. This is an object in real life. And one is your let's say engineering student. Okay. This is also an object in real life. Now I said that there is a relation between these two. And what is that relation? Again it will be your is a relationship. Is a. Because what can I say? Engineering student is a student. Every engineering student will be a student. But all the students will not be engineering. Let's say someone will be a student of biology. Let's say someone will be a student of MBA. There can be many types of students. One of them is an engineering student. So I hope you understood that inheritance tells us that in the real world, two objects are connected to a relationship. And we represent it programmatically. Okay, very good. But how do we represent it and what is the benefit of it? First, let's understand what is the benefit of it. If I said that every car is a vehicle. Okay, if I talk about the most basic behaviors of a vehicle, then first of all, obviously, you understand that every vehicle can move, that's why it's a vehicle. If a vehicle is stationary, then how can it be a vehicle, right? So, every vehicle can move. So, I am saying that every vehicle can start. So, what can I write here? That it will have a behavior that every vehicle can start. Now, I am saying that a car can also start. A car can also start. So think for yourself, because vehicle also starts, that's why car starts. It's not like car starts independently. Similarly, if I make a bike in another vehicle, that can also start. And that also starts because it's the property of vehicle to start, no matter which vehicle it is. What I mean to say is, if I represent it in code, then I will make a vehicle class and a car class. And in both of them, I will be showing this inheritance property through some keyword. But I don't want to write the method of start in both. Because I will write start here and here. Why should I make the code so long twice? I will simply write start in the vehicle. And I will ask the car to inherit its property from the vehicle. Because you are a vehicle. What does it mean? Let's see. So it means. Let's say. I made a class here. Vehicle. Let's take our engineering student as an example. So first of all, I have made a class here. So what did I write here? Suppose I wrote the same property that it has a function void start. And this function does nothing, it writes this. It must be printing on any screen like vacantly starting etc. It doesn't go on that. It leaves its internal code. But here there must be a start function. Now this is my class. Now, I will make another class in Java. Let's say, I will name it car. Now, I want this car to inherit this vehicle class. Make a relationship with it. So, I will call the keyword extends. Extends. Which car is extending? Vehicle. Okay, very good. So here I have made another class which is called Wickel class. Now this means that the Wickel class is a relationship between them. Now I don't need to write any start function in it. I can write this but I don't need to write it. Let me write another function here void setGear. I can write setGear because the setGear function is specific to the car but setGear function is not specific to the vehicle because vehicle can be a cycle and it is not necessary that there is gear in the cycle vehicle can be a It can also be a gearless bike. It is not necessary to have gears in it. Basically, vehicle is a major term. If vehicle is a big circle, then car is a small circle. Cycle is a small circle. And bike is a different small circle. So, every vehicle doesn't need a gear. But, car has a gear. So, this function is specific to car. It's not specific to vehicle. So, I'll write this method inside this. But, start is inside sub. So, I wrote it inside vehicle. I don't need to write it inside this. Now, when I make an object of this car. If I write like this tomorrow. Let's say car C equal to new car. I made this object. So, on this C variable. I can also call start and setgear on C variable. Now how can I call start? Because this car extends our vehicle. So I can call start here. Which start will be called here? This one. And if I do setgear, then this code will be called. But yes, if I also I have written some code of start and now if I will start C dot then this start will be called. Now this start is shadowed. I have shadowed this parent start. But for now we assume that this is not written. We want the parent one to be the start call because why would I want to write start in all of them. So see how easy my work has become. Now whatever vehicle I will represent in my code that tomorrow I can make a bike, cycle, truck, anything. I will make any number of vehicles, I know every vehicle will start. So whenever I call their cycle.start, truck.start, they will always call the vehicle's start. So this is a basic funda of inheritance. And if we look carefully at which properties inheritance provides us, then you will understand that the most important property it provides is Reusability. Reusability. That now you think, we don't have to write our redundant code again and again. Why will I write the start function every time? No matter how many types of vehicle I make, whether I make a car, whether I make a cycle, whether I make a truck. I won't write the start function in sub. I get that code reusability because of inheritance. Now inheritance gives us one more benefit. We say that because of this we can support Polymorphism. Polymorphism is again a pillar of object-oriented programming. We'll see that in the next lecture. But now I'm telling you that we can support polymorphism only because of inheritance. So these are some benefits that inheritance gives us in a language. Java gives us code usability, supports polymorphism, etc. Now let's quickly see this inheritance example in code. And we'll discuss types of inheritance there as well. So here I have again created a demo3 file in which I have written the main function. Now I want to give you an example of inheritance. First of all, let's make a class named student. Let's take the example of student class. So let's say the student has some property of his own. There must be some name, age etc. So now we will leave only name and age. We will put rest of the things later. I am saying that mainly students have a behavior, a method. That is mark attendance. Every student can mark his attendance. Whether he is any student. So basically let's say I will just print here. System.out.println Attendance marked. Simple. I just printed this. Now, let's say I make another class whose name is Engineering Student. And I know that every engineering student is a student. So, there is a relationship between these two. There is a relationship of inheritance. So, I put the keyword Extents and write Student here. So, Engineering Student Extents Student. Very good. Now, I am saying that Engineering students can also mark their attendance. In fact, every student can mark their attendance. So I don't need to write mark attendance again. But yes, I can write a new function here. Let's say attend lab. Because I know that engineering students have labs to attend. Every student doesn't need a lab. But engineering students do have labs. So I don't do anything here. Let's say I print it. labs lab attended. Now if I make an object of this engineering student, let's say es equal to new engineering student. Now on this engineering student, I can call mark attendance and attend lab. AttendLab, I can call both these functions on EngineeringStudent. Why? Because its object has its access too. AttendLab's access. And this big function's access too. MarkAttendance's. So let me run it once. Okay. Attendance marked. LabAttended. Very good. But now think of one thing. If I make an object of student class. Student s1 equal to new. New student. Assume I made an object of this. Now, which methods can I call on this reference? Can I write s1. markAttendance? Yes, I can. There is no error. Why? Because the object of this student can do markAttendance because there is markAttendance in the student. But can s1. attendLab See, I am not getting any recommendation. But if I write it myself, then I will get an error. What is the error? The method attendLab is undefined. Because in this student class, there is no function of attendLab. Right? It means that we can call only those methods on the student, which are written in the student class. But on this engineering student, we can call all those methods, which are written in it. Or its parent class, which are written in it. So basically, if we say, then our Inheritance is also a parent-child relationship. We call our parent class as superclass and child class as subclass. So in this case, our parent class is student and child class is engineering student. Because in engineering student, we can attend lab and mark attendance. But in student class, we can only mark attendance. Because it has only this authority. So doing this is wrong. Let me put in bracket that I am wrong. It is not allowed. You can't do it. Okay, we understood that this is a parent-child relationship between superclass and subclass. And I can call all the functions in this, which is written in this or in the parent class. Okay, but we read access modifiers a while ago. And from that, if we read that we don't put anything, then by default, it is default. And default means you can access in the same package. Right? So this is the same package. But if I would have written protected here, if I would have written protected here, then also there is no problem, then also I can access this. Because protected means that you can access in the same package or your child class can also access. Now here child class is engineering student. Engineering student can access this method. But if I write here If I had written public here, then obviously there would not be any problem. Because public can also access everyone. But the problem would have been if I had written private here. As soon as I made mark attendance private here, I got an error here. Now neither engineering student can call mark attendance, nor student can call mark attendance. Because mark attendance method is private. So student can't call, so what can engineering student call? Look, there is an error in both. The error of both will say the same. The method mark attendance from type student is not visible. and the method mark attendance from type study is not visible. It is not visible at all. So I have to make this method public or protected or by default I can leave it as it is. Okay. Now let it be public. Okay. So you must have understood this too. Now I want to talk to you about types of inheritance. Okay. Types of inheritance. What are the types of inheritance? So first I will write its types. First of all, our simple inheritance. Whatever we saw earlier was our simple inheritance. We also have multi-level inheritance, hierarchical inheritance, and multiple inheritance. We have these four types of theoretical inheritance. We will see all the inheritance one by one. First, let's put our main function here. Okay, it's done. Okay. So, first of all, we have seen the simple inheritance. This is a simple inheritance. It's a simple parent-child relationship. There is a student and an engineering student. So, I will copy this here. Here, I have made a student and an engineering student. Okay. So, here the student is our parent class. And this engineering student is our Childglass. This is our simple inheritance. Next is multi-level inheritance. Now what does this say? Look, multi-level inheritance says that what happens if I write this there. Okay. Multi-level inheritance says that what happens that if our A class is parent of B class and B class is parent of C class. Something like this. And the inheritance should be like this. Right? So whose child is C? Whose child is B? Whose child is B? Whose child is A? Something like this should be the inheritance. Okay, what does this mean? See, this means that first of all I made a class student. Its child is an engineering student. Now suppose I make a child of this too. Class, let's say CSE engineering student. Which extends to engineering student. Engineering student. Something like this. So now see. This CSE engineering student. Child of engineering student. Which is child of student. So how did the relationship between these three happen? Again what we talked about happened the same way. Right. First of all what will come to you? Student. Okay. Whose child is that? Whose child is engineering student. Engineering student. And finally, who is his child? His child is our CSC engineering student. So, this is multi-level inheritance. Now, in this CSC engineering student, if I make an object of this, let's say, there will be a function in this. In this, there will be mark attendance. In this, attend lab. And let's say, there will be a function in this. Let's say, attend CSC lab. I make any function. Let's say. Now, let's leave this empty. Now, if I make an object of this CSC engineering student, let's say, CSC 1 equal to new CSCEngineeringStudent Now on this CS1, which methods can I call? I can call all the methods. This one too, AttendLab too, and MarkAttendance too. Why? Because this is its child and this is its child. So it has the authority of all the public methods of the student. And it will have the authority of both its and its public methods. Because if it has its own authority, then if it has its own authority, then it has its own authority. So, basically, CSE engineering students can call all the methods. They can call all the methods to the top. Obviously, they can call only those methods which are not private. Remember, right? So, public can call all the methods. Very good. And protected too. Okay. So, this is our multi-level inheritance. One is hierarchical inheritance. Okay. What is hierarchical inheritance? We took this as an example. Multi-level. Okay. It became multi-level. Okay. Now, what is hierarchical? If we talk about hierarchy, then I can show it like this, let's say I have a class A, it extends a B class and it extends a C class. So here I write B and here I write C, something like this. So, there is an A class which is inherited by a B class and a C class. So, it is like a hierarchy, like a tree structure. How can I show this? You think, if I assume that my student is A class, then he becomes A class. And if he is an engineering student, then he becomes B class. And for now, you forget this CSE engineering student. You forget this for now. So, I don't make an object for this. I comment this for now. Okay. But now we have a parent class and a child class. And here I will make one more child class. I will make one more child class. Whose name I will put as engineering student. Medical student. Medical student also extends the student. This is the second child which is C class. After this also the same function can be done. lab, no problem, it has its own function. So, how did hierarchy happen? A is inherited by BB and CB. See, engineering student extends student, medical student extends student. So, these two students, Which is being extended to our main student class. We have made a hierarchy like this. Now what does this mean? If we make an object of medical student, then he can do mark attendance and attend lab. And when he will attend lab, he will attend his own lab. Let's say I write here lab attended for medical student. And here I write lab attended for engineering student. So, print will be lab attended for medical student. If this will be its object. Same if I will make object of engineering student. So, he can do this mark attendance. Because it is common function of both of them. But he can also do attend lab. So, when he will call attend lab. So, this will be print. Lab attended for engineering student. Okay. So, I hope you understood their hierarchy. That A is our parent class. And their two children are B and C. In this case, A is our student. And B and C is our engineering student. And medical student. So this is our hierarchical inheritance. And this is also supported in Java. In fact in Java, simple, multi-level, hierarchical, all three are supported. Only one inheritance is not supported in Java. That is multiple inheritance. And I can write this as not supported in Java. Then why do we read it? We read it because it is supported in other languages. And theoretically, it is also an inheritance. Java does not support it through classes. But there is a way that is supported in Java, which is interfaces. We will read that later. But for now, you understand that Java has three inheritance supported. This one is not. And what is inheritance? Let's see that first. And after that, we will see why this one is not supported. So multiple inheritance means, if I tell you this once. Multiple inheritance means that we have two parent classes A and B. And both of our classes, let's say A and B. These two together make up a parent class of C. Something like this. And this is not supported in Java, that by combining A class and B class, our parent becomes a C class. This is not supported. What I mean to say is, if I show it in this, now as if there is a student parent here, if I make another parent here, let's say class, now I am naming it as student2. It doesn't make sense, but I just named it. And this is also a parent. So, parent B. This is the second parent. So, I can't say that engineering student extends to student too. Comma student 2 too. This will be wrong. There will be an error. The error is syntax error. You can't write like this. So, it is not supported. I can't do this. I can't do this. Now, why can't I do this? The reason is, again, let's go to the screen and look at the iPad. First, I hope you understood what types of inheritance are there. And only multiple inheritance are not supported in Java. Okay. So, let's see why multiple inheritance is not supported in Java. And one of the reasons is due to diamond problem. Diamond problem. Okay, what is this diamond problem? This is a very interesting problem. Okay. Because of this, multiple inheritance Java is not supported. Let's first understand this problem. Now look, diamond problem says, let's say you have a parent class A. And it inherits two classes, B and C. Okay. And this B and C, both of them are inherited by the same class, D. Now to say, this part of it, this is multiple inheritance. Multiple inheritance. And this part is not supported in java. Now because this part looks like a diamond, we call it diamond problem. Now what is the problem here? Suppose there is a class A. This class has a method show. I will write it like this. There is a method called show. Now what does same b do? It inherits a and c inherits a. So it will also have this show method. But let's say it wants to write a show method of itself. So it has also written a show method of itself. Do you know what happens if I write a show method of itself? If I make an object of b, it will execute this show and not this one. Same if I make of c, it will execute this show. Now the problem starts here. Suppose I have child class D. And suppose I have multiple inheritance supported. No compiler error comes. And I have not written anything in D. D is also empty. Or there will be some other methods. But there is no show method in it. Now if I write like this. Let's say small d equal to new d. Now if I show call on this small d. Then what would be the show call? Because this D is getting a show from here and here as well. So which show will call this D? This is because our Java compiler will not be able to tell which method to call in runtime. That's why our multiple inheritance is not supported in Java. And this problem is called diamond problem. Because its structure looks like a diamond. But it also has a solution. The solution is that if you want to support multiple inheritance So use interfaces to solve this diamond problem. We will see how to solve it when we read interfaces. But I hope you understood what is diamond problem and why multiple inheritance is not supported in java. Now there is one last thing and then the lecture is over. That is a keyword which we call super keyword. Super. This is also a very important keyword. Now, if you understood this keyword, then you will understand super keyword very easily. Okay, how? See, what did this store? This store reference of current object. This is what it used to do. It used to store reference of current object. Super will store reference of parent object, which will be our parent object, will hold its reference. That's it. There's only this much difference. This will hold the reference of parent object, this will hold the reference of current object. Now, we can use super in many ways, like we could do this. So, let's see all the uses of super. So, look. The first use of super is to access parent class variable. Parent class variable. What does it mean? Look. Let's say, I have a class A. Okay. And there is a class B. And this B class extends A. Means it inherits. So I write extends A. Something like this. Now in this A class, I have a variable. Let's say int x equal to 4. And in this B class, I have to use this x variable in any function or code part. So what can I do? I can write super.x. Now whenever I write the keyword super.x, my compiler will understand super means reference of parent class. Its reference holds. Its x is 4. Right? This means if I declare int x here too, let's say 5, then this super.x and int x are different x's. If I print x in this B class, then 5 will be printed. And if I print super.x, then 4 will be printed. It accesses the parent class variable. Okay, this was its first use. Let's see the second use. Again, assume I have a parent class A. And a child class is B. And in this parent class, there is a method, let's say, greet. Simple, void greet. Which does nothing, it would have hello printed on the screen. And let's say in this class B, I want to call this method. Let's say here there will be a method, void, sayHello, any method, I have made a random method. And in this method, sayHello, I want to call greet first and then do some work. So I can also call the methods of parent class using the keyword super. So I can write super. dot greet. So call super's greet. I can also do this. Okay. Very good. So basically, what will be my second point? I can call parent class method using super. Okay. These are its two use cases. Okay. Third and the most important use case of this is call parent class constructor. This is the most useful thing. That it can call the constructor of parent class. What does it mean? It means that if we have class A. Okay. It will have its own constructor. Similarly we have class B. Which extends A. It will have its own constructor. So if I have in B's constructor. Let's make it a little bigger. If I have. In B's constructor. Wrote super. and wrote it as a method call. Like we used to write this method call, we used to call the constructor of this class that we had read. Now if I write super, then it means that first call this A class constructor and after that come back and do whatever you want to do. So first take call to A class constructor Then come back and do whatever you want to do. So this basically calls to parent class Constructor. Now why this use comes? This use happens if I want to first assign some values here. And then I assign my B values. So this use comes here. The most interesting thing is that super writing is optional here. If you don't write by yourself, then Java will include it by itself. Okay, let's quickly look at these things in the code. So, first of all, I have made a class named demo5. Let me quickly make a student class. I made a student class and put two variables in it. Let's say name and put our age. Let's also put a roll number. Roll number. And let's say, let's leave 3 for now. And I make another new class, let's say Engineering Student. And I extend my student class. Student class. Something like this. And I am saying, here also there are 2 variables. Let's say, one is college name. Now I am saying that what was the use of this super? That I can call the variables of this print class. So let's say if there is a method in this, let's say void print Print, simple. It will print all the details of the student. So, it will print the name, age, number and college of the student. So, it will do like this. Let's say system.out.println Now, see what is the name of the student. It is written here in its parent class. So, it can write simple. Super Sorry, super.name Something like this Now it will go ahead like this Then what can it print? It can print super.age Something like this Then it will go ahead And what can it print? It can print super.age Super.RollNumber Can print details like this Because here name is ageRollNumber and here is college There is no such variable which is same in both So even if you don't put super here Then also there is no problem It is optional to put super But suppose there is a variable here Let's say for now I am putting an int x That is a random variable And here also I am putting an int x That is also a random variable So there is a random variable x in both So if I print here One more thing whose name is x. So see, there is no error. This x has printed it. Now which x will be printed? This will be this one. So it will always choose its class first. But I want to print this x. So for that, I have to put a keyword, super.x. Now as soon as I put super.x, this x will always be printed. Okay. So this is our complete story. That how can I call the variables of this student. In fact, if any method exists in it. So let's say void print already exists. And it prints the same details. So first I'll add x. There's no use of x here. I wrote this to make you understand. x is nothing. It just prints the name, age, and roll number. We won't write super here. Because there's no super class here. It calls the name, age, and roll number. I'll remove this. So now let's go to our engineering student. Here, my print function, I'm saying that name, age, rule number will be printed at the top. Now engineering student has college. And he also has these fields, using inheritance. So this will be printed at the top. I just have to print college student. So what I'll do, first I'll call super dot print. First I'll call super dot print. Means I'll call print of my parent class. So name, age, rule number will be printed and come. After that, my work will be done. college print, then I will print college from here. Something like this. See how interesting it is. So now if I make an object of this engineering student, let's say es1 equal to new engineering student and I call es1. print. So whose print will be called? This one. Because this one also has a print and this one also has a print. But what I said is that if this one has its own print, then always this one will be called. Now as soon as this one is called print, what did it do? First it called the super print. Then this one will be called. Then the name, age, row number will be printed. Our context will come back here and then the college will be printed. So basically all four things should be printed. So let's see once. It has been printed, but all values are null. Why? Because I didn't set their values. So let's do one thing. First, let's put ES1. name equal to some values. I will write their values. Now see, I have put their values here. Name, age, roll number, college. Now if I print them, then my correct values will be printed. Aditya 28, 101 and IIT Guwahati. Absolutely correct. Okay, so we understood the second use case too. Now let's work to explain the third use case. Let's say I make a constructor of a student. Do you know how to do that? Let's take a name Let's take an age and a roll number And what do I do? I do this.name sorry name equal to name this. age equal to age and this. roll number equal to roll number something sorry So we do it like this. Now because I made a constructor, you know Java won't make a default constructor. So I'll make that too. Okay. I've made a default constructor. Now look. Now my engineering student, I'll make a constructor in this too. Engineering student. And what will its constructor do? Let's say it'll take a college. String college. And this.college equals to college. This.college. Equal to college. Right? Now if I make an object of engineering student. And only pass college. Then it's okay. The value will come in college. Let's say. I pass a college from before. I will remove this for now. Okay. And I want to reuse this line. This line. I make ES2. Equal to let's say new engineering college. New engineering student. Which is in IIT Guwahati. Okay, I can do this. But if I print es2. So what will happen? So college will be printed and the rest will print their default values. Something like this. Null 0 0 IIT Guwahati. Because they didn't get those values. Right. Now what can I do in this case? I can write super here. That call the parent constructor. And give some values to that constructor. And what values should I give? I can take those values from here. Let's say first I gave name. I want a name, I want an age And what do I want? I want a roll number So I also want int roll number Roll number Comma So I took name, age, roll number and college And super called it By giving name, age, roll number By giving roll number And then I signed my college here Now see, what it means is that Instead of this line, I could have written this too If I remove this, I will comment this Instead of this I could have written like this too. Even then, everything was valid. Because this engineering student has access to these three. Name, age, role number and also college. So, I can expect all four things in its constructor. Then when I make an engineering student, I will give all four things. I will give it like this. Name becomes Aditya. Okay. Age becomes 28. Role number becomes 101. And college becomes, let's say, IITG. Something like this. I could have done this. But you think, Let's make it like this so that it is visible. Okay. But now think. If I had done this, I would have to write everything together. I want that the constructor of my parent class is already made. This constructor is already made. It will assign the name, age, and role number by itself. So why should I assign? So I don't write these three fields. I will call my constructor which is parent class. So I called super and my constructor. Which constructor? This one. Because I have written three fields in front of super. Now do you remember in constructor chaining, if I had written this, then I would have called another constructor which is of this class. If I am writing super, then I am calling constructor of parent class. So I called this constructor. Now as soon as we entered it, name, age, roll number went to this constructor. This constructor gave a value to it. Then again our flow of control came here. The value of the college came here. And then whenever we call our print, everything will be printed. Now see how the print is also printed. First call this print. That is, put the name, age, roll number on the screen. And then print the college. Print the details of the college. So see how well we are able to use super. Now, if I run this code, then you see, Aditya 28101 IITG. Everything is printed well. Okay. So, this is the use of the entire super. I hope you enjoyed it. You must have learned a lot of new things. That's all in this video. I will meet you in the next video. Till then, thank you so much.