So, let us look at something which does not seem normal at first sight, a private class. So, we have seen that we can have objects which contain other objects in their instance variables. For instance, when we create an employee, we might want to record the joining date of the employee and the joining date can be recorded in terms of another class called date. So, we have a public class called date, which has its own instance variables, its own structure, date, day, month and year. And this is a nested object which is sitting inside an employee object.
So, in this case, date is a public class and it is also available to other classes. So, employee use uses date, but there is nothing to prevent other classes from using date. And this is then a public class available throughout the system. So what we are going to discuss now is private classes. So why would you need a private class and when would it make sense?
So, consider a linked list. So, typically a linked list is made up of nodes. So, you first create a node class.
So, node class typically has two fields. So, remember a linked list typically the picture is like this. So, you have a head pointing. the first element and then each one has a next pointing to the next element.
So, each each node in a linked list typically has a value and a next point. So, you first define a node class and then in the linked list you say that you have have maybe an extra variable to record the size of the list and you have a node pointer in this case first, which points to the first node in the list. And now if you want to get the value at the head of this list, for instance, this returns the head of the list and extracts it it is like the equivalent of a pop in a stack. So, what you do is you remove the first data item, return it and you move the first data item point to the next one.
So, this is a typical behavior of a linked list. Now in this case, linked list is using node as a nested object just in the way that employee was using date. However, perhaps here there is some justification for saying that this node object or this node class should not be public because it could very well be that we also want to add a new a previous.
So we could have a doubly linked list. So we could have a list in which we point to this, the node in front and the node behind. And this makes it sometimes much easier to navigate a list because when you're trying to delete, for instance, in a list, you have to be a bit careful. about looking one step ahead, but if you have a doubly linked list, it is much easier to delete the node on which you are currently sitting. Now from the user's perspective, they are dealing with a list of values.
They should not care or know whether the nodes that underlie this list have two pointers, one pointer or what the structure of the pointer is. So the structure of this node. It is not something that is desirable to expose to the user of this class linked list because that is really an implementation detail.
So, in the interest of encapsulation, we should actually not expose that. So, this is where now private classes come in. So, what you can do is you can push that node class which is outside into the definition of linked list and make it a private class.
So, now this private class has exactly the same structure as we had before. So, it has So, perhaps these could also be private. So, it has now a node and a next.
But this data value and the next value are now accessible only from inside because nobody outside even knows the existence of this node class. So, because it is defined inside another class is sometimes also called an inner class. So, one thing to remember is that the node class is a node class. We said that when we have a private instance variable inside a class, all objects of that class can see the private variables of each other. So, for instance, if we want to compare two objects, if you want to say o1 dot equal o2 and do this comparison based on this instance variables, then it is impossible to do it unless o1 can look at the values of o2.
So, for this reason, we said that a private variable is not private within a class, it is only private outside that class. So, in the same way, this private thing is of course accessible here, but conversely, this is also accessible here. So, this private class can see the other private instance variables that are defined inside linked list. So, for instance, you could use the value of size inside this class is perfectly legitimate.
And we will see that this is important that we need private classes to be to actually make exploit private classes to the fullest. we will need private classes to be able to access other parts of the object inside which they reside. So, the important thing is that they are private, since they are private, if they are outside, then nobody can see it including this class. So, they cannot be outside, they must be inside. So, they are inner classes.
So, you have to define it inside the public class, which is going to use it. And then it is accessible to that class. And whatever the class has is also accessible to that inner class.
So, an object can have nested objects, this is clear. But what we have seen is that in some situations, the structure of these nested objects should not be exposed, not even need not be exposed, it probably should not be exposed. So you can bury that extra object inside the public object as a private class and get an extra degree of data encapsulation. So this usage of private classes is of course a very minimal utility. And what we are going to see is that you can do something much more fundamental to private classes.
You can combine private classes with the idea of an interface to allow controlled access to the state of an object. So, you remember that we can access the state of an object through public methods, but sometimes you need some control over that and that control can be done through these objects which are created as private classes.