What is the point of inheriting a 'private' class when all members will be unreachable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the point of inheriting a 'private' class when all members will be unreachable?

6th Aug 2017, 8:24 AM
Христо Христов
Христо Христов - avatar
4 Answers
+ 7
Consider this problem... Suppose you decide to make a simple Bank Management system, and now you will create users, store their data, land allow them to withdraw and deposit money for starters... The best thing to store data will be in a class object , like class Account, etc. You may have members like name, DOB, contact, address, email, etc and some other data like account number, interest rate on loan and deposit, account password, pin for debit cards, etc etc. Now on a new user creation, you may generate a random account number and assign it to the user, along with details. You also generate pin numbers etc which are not to be disclosed to anybody except the user. So, how can you ensure that these numbers stay safe? You declare them private, and create password protected getters and setters, for changing and accessing. Thus the private modifier protects your data from those who try to hack into your program... Your money (or data) remains safe and can be accessed only by the acc holder or the bank for security reasons...
6th Aug 2017, 8:54 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 7
Now inheriting a class as private does not mean that you will not be able to access those members. It simply means that all protected or private members of the base class will become private for the derived class. Thus you can further protect the data by hiding the existence of the base class from others, and use protected getters and setters for the members inherited...
6th Aug 2017, 8:57 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Private - Like you'd think, only the class in which it is declared can see it. Package Private - Can only be seen and used by the package in which it was declared. This is the default. Protected - Package Private + can be seen by subclasses or package member. Public - Everyone can see it. basically public is more restricted than default extracted from https://cheeze.club/8j0j
6th Aug 2017, 8:27 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
0
thanks for the answers!
6th Aug 2017, 4:07 PM
Христо Христов
Христо Христов - avatar