If we use them properly, our code will be more managed in the long run. Here, it must be noticed how access specifiers play an important role. So, nobody can call any method directly without going through insertStudent method. This is the most correct approach as all other methods are private and only one method is public which can be accessed from outside. The client can call any methods directly from outside and can insert anything like fees, etc.īut requirement says that all methods should get called so that student will get inserted with all details not just a single or partial detail. Still, our code gets rejected as requirement is not completely fulfilled. We will then need to comment only one line. So seen from Java the member will be protected just as in Java itself. you can have friends in the same package and derived classes can also see protected members. Now, consider that after some years we want to remove skill insertion. Protected in Groovy has the same meaning as protected in Java, i.e. It is just copy and pasting code at different places. This is just code writing in a different way. That means we will have less maintenance. In this approach, we just tried to bring more readability and modularity in our code. What are private public default and protected access Java modifiers - The access specifiers are used to define the access restriction on the class and members of a class.The private access modifier is the most restrictive access level. So, we need to change this code as shown below again: The above written code will work properly at the first instance, but if we think for the future, then it will be difficult to manage it. If, in the future, we want to make fees optional, it won’t be possible and we will need to change whole code which increases unit testing. This approach will not be accepted as all code written is at one place. Now we will start writing a simple class where the above specified requirements will be fulfilledįirst approach: package Question: Why should you know private variable? Where have you used in your project?Īnswer: :Let’s say we have the following requirement:In a college we want to insert student details and one student insertion means that is:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |