+ 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
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.
+ 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.
+ 16
Thanks @zeke
+ 9
That really is an easier way. tons of thanks! 😁
+ 8
You're welcome James ! Happy Coding
+ 6
You can take what Zeke Williams says to you James, it is so easy!
+ 5
Here is my thought:
https://code.sololearn.com/c151jy1fNbxM/?ref=app
+ 5
Mario Apiloko Start a new QA thread next time and read the FAQ.
+ 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.
+ 4
Thanks it is a lot easier!
+ 3
Well James , sometimes it's not right away. I just happened to be scrolling through the most recent discussions when I saw yours 😃
+ 3
no for inherit in c++ you should declare protected for inheritance variablr in base class
+ 3
hello am new here please which language is more easier to learn
+ 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
+ 3
That's easy and nice .....!!!!😃😃
+ 3
definitely helpful, Sai Aravind ch. thanks for the answer 😊
+ 3
nice trends
+ 3
Kinqslee Py
Sololearn, Ruboto Core, Ruboto Benchmark
+ 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;
}
+ 3
Aye aye Sir Luther Conley! my focus will always be with C++ and algorithms, of course. I'm just familiarizing with other language 😊