+ 65

Clarifications for private inheritance

I just want to make sure if I got it correctly. When you inherit a Private class, all I have to do is declare the private inherittance to access it or is it better if I inherit the public one that includes the Set and GetObj to access it's private class? all answers are much appreciated

19th Apr 2018, 11:47 PM
James
James - avatar
54 ответов
+ 41
If you're talking about C++, it would be better to, instead of make the variables 'private', make them 'protected' instead. This makes the variables private to everything but the classes that inherit from the class that contains them. For example: class origin { protected: int i; }; class nextOrigin: public origin { public: void use_i() { i += 20; } }; So you can access them without using getters and setters.
20th Apr 2018, 3:44 AM
Zeke Williams
Zeke Williams - avatar
+ 22
This answer is regarding Java, however it also works for C++ You cannot inherit private methods or fields, actually private stuff will be ignored if something inherits from its class. As suggested before, you should use protected instead. protected fields and methods will be inherited but cannot, other than public, be accessed from outside. public like an ALL ACCESS area protected like a MEMBERS ONLY area <no modifier> like a STAFF ONLY area private like a NO ACCESS area If that helps Look here otherwise: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html It may be interesting to add, that there is a package private statement. This, as the name suggests, limits access to the actual package, but this is probably not of concern to you at your level.
21st Apr 2018, 3:48 PM
Loeschzwerg
+ 16
Thanks @zeke
20th Apr 2018, 7:06 PM
Sadaam Linux
Sadaam Linux - avatar
+ 9
That really is an easier way. tons of thanks! 😁
20th Apr 2018, 3:49 AM
James
James - avatar
+ 8
You're welcome James ! Happy Coding
20th Apr 2018, 3:50 AM
Zeke Williams
Zeke Williams - avatar
+ 6
You can take what Zeke Williams says to you James, it is so easy!
21st Apr 2018, 10:36 PM
yventz gustave
yventz gustave - avatar
21st Apr 2018, 9:22 PM
Yusuf
Yusuf - avatar
+ 5
Mario Apiloko Start a new QA thread next time and read the FAQ.
21st Apr 2018, 9:59 PM
Timon Paßlick
+ 4
Kinqslee Py I know that Java, Python and Ruby are the most used ones, but I suggested him C and C++ first because they will help him develop the logic and understand the concepts first.
22nd Apr 2018, 2:26 PM
Rugved Modak
Rugved Modak - avatar
+ 4
Thanks it is a lot easier!
22nd Apr 2018, 6:45 PM
JTLZ
+ 3
Well James , sometimes it's not right away. I just happened to be scrolling through the most recent discussions when I saw yours 😃
20th Apr 2018, 4:01 AM
Zeke Williams
Zeke Williams - avatar
+ 3
no for inherit in c++ you should declare protected for inheritance variablr in base class
21st Apr 2018, 6:31 PM
AHSAN ZAID
AHSAN ZAID - avatar
+ 3
hello am new here please which language is more easier to learn
21st Apr 2018, 9:57 PM
Apiloko Marho Emmanuel
Apiloko Marho Emmanuel - avatar
+ 3
Apiloko Marho Emmanuel If you want to do core programming, start with C, then C++ for the basics If you want to do web development, start HTML, then CSS, JS, PHP Further ahead, For further core programming, learn Java, python and ruby For windows apps, do C# For OS X apps, do Swift and Objective C For Android apps, Kotlin and Android For database management, SQL and MySQL, these are kind of must do, but later
22nd Apr 2018, 7:27 AM
Rugved Modak
Rugved Modak - avatar
+ 3
That's easy and nice .....!!!!😃😃
22nd Apr 2018, 8:16 AM
Brijesh B
Brijesh B - avatar
+ 3
definitely helpful, Sai Aravind ch. thanks for the answer 😊
22nd Apr 2018, 3:16 PM
James
James - avatar
+ 3
nice trends
22nd Apr 2018, 5:17 PM
Mohammad Ismail
Mohammad Ismail - avatar
+ 3
Kinqslee Py Sololearn, Ruboto Core, Ruboto Benchmark
23rd Apr 2018, 4:30 AM
Rugved Modak
Rugved Modak - avatar
+ 3
Luther Conley while getters and setters work fine, it kind of breaks the rules of encapsulation. For example, if one were to have a private name variable, you wouldn't want to write: cout << obj.getName() << endl; Instead, you want other class methods to access the variables within the class like: obj.showName(); //class Obj void Obj::showName() const { cout << this.name << endl; }
24th Apr 2018, 4:24 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Aye aye Sir Luther Conley! my focus will always be with C++ and algorithms, of course. I'm just familiarizing with other language 😊
25th Apr 2018, 1:53 AM
James
James - avatar