Clarifications for private inheritance | SoloLearn: Learn to code for FREE!


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

4/19/2018 11:47:50 PM


55 Answers

New Answer


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.


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: 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.


Thanks @zeke


That really is an easier way. tons of thanks! 😁


You're welcome James ! Happy Coding


You can take what Zeke Williams says to you James, it is so easy!


Here is my thought:


Mario Apiloko Start a new QA thread next time and read the FAQ.


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.


Thanks it is a lot easier!


Well James , sometimes it's not right away. I just happened to be scrolling through the most recent discussions when I saw yours 😃


no for inherit in c++ you should declare protected for inheritance variablr in base class


hello am new here please which language is more easier to learn


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


That's easy and nice .....!!!!😃😃


definitely helpful, Sai Aravind ch. thanks for the answer 😊


nice trends


Kinqslee Py Sololearn, Ruboto Core, Ruboto Benchmark


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 << << endl; }


Aye aye Sir Luther Conley! my focus will always be with C++ and algorithms, of course. I'm just familiarizing with other language 😊