I looked on SO and couldn't find a good explanation of the difference between public, private, and protected inheritance in C++. All of the questions I've found deal with specific cases. What is the difference in general?
Join them; it only takes a minute:
|
|
||||
|
|
|
To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:". There are three accessors that I'm aware of: Let:
By "is aware of", I mean "acknowledge the existence of, and thus be able to access". next:The same happens with public, private and protected inheritance. Let's consider a class
|
|||||||||||||||||||||
|
IMPORTANT NOTE: Classes B, C and D all contain the variables x, y and z. It is just question of access. About usage of protected and private inheritance you could read here. |
|||||
|
|
Limiting the visibility of inheritance will make code not able to see that some class inherits another class: Implicit conversions from the derived to the base won't work, and Only members/friends of a class can see private inheritance, and only members/friends and derived classes can see protected inheritance. public inheritance
protected inheritance
private inheritance
public member
protected member
private member
Note that C-style casts purposely allows casting a derived class to a protected or private base class in a defined and safe manner and to cast into the other direction too. This should be avoided at all costs, because it can make code dependent on implementation details - but if necessary, you can make use of this technique. |
|||||
|
|
It has to do with how the public members of the base class are exposed from the derived class.
As litb points out, public inheritance is traditional inheritance that you'll see in most programming languages. That is it models an "IS-A" relationship. Private inheritance, something AFAIK peculiar to C++, is an "IMPLEMENTED IN TERMS OF" relationship. That is you want to use the public interface in the derived class, but don't want the user of the derived class to have access to that interface. Many argue that in this case you should aggregate the base class, that is instead of having the base class as a private base, make in a member of derived in order to reuse base class's functionality. |
|||||||||
|
Inheritance type : Object inherited as:
|
|||||
|
|
These three keywords are also used in a completely different context to specify the visibility inheritance model. This table gathers all of the possible combinations of the component declaration and inheritance model presenting the resulting access to the components when the subclass is completely defined.
It reads in the following way (take a look at the first row):
An example:
The resulting access for variables
The resulting access for variables
Now lets define a subclass:
The defined class named Sub which is a subclass of class named No. It doesn’t. If we compile the following code, we will get nothing but compilation errors saying that When we omit the visibility specifier, the compiler assumes that we are going to apply the so-called private inheritance. It means that all public superclass components turn into private access, private superclass components won't be accessible at all. It consequently means that you are not allowed to use the latter inside the subclass. We have to inform the compiler that we want to preserve the previously used access policy.
Objects of the This is a very serious restriction. Is there any workaround? Yes. The third access level is called protected. The keyword protected means that the component marked with it behaves like a public one when used by any of the subclasses and looks like a private one to the rest of the world. -- This is true only for the publicly inherited classes (like the Super class in our example) --
As you see in the example code we a new functionality to the It wouldn’t be possible if the variable was declared as private. In the main function scope the variable remains hidden anyway so if you write anything like:
The compiler will inform you that it is an Finally, the last program will produce the following output:
|
|||||
|
|
Public inheritance models an IS-A relationship. With
every Private inheritance models an IS-IMPLEMENTED-USING relationship (or whatever that's called). With
a
This I don't think anyone knows what |
|||||||||||||
|
|
If you inherit publicly from another class, everybody knows you are inheriting and you can be used polymorphically by anyone through a base class pointer. If you inherit protectedly only your children classes will be able to use you polymorphically. If you inherit privately only yourself will be able to execute parent class methods. Which basically symbolizes the knowledge the rest of the classes have about your relationship with your parent class |
|||
|
|
|
Protected data members can be accessed by any classes that inherit from your class. Private data members, however, cannot. Let's say we have the following:
From within your extension to this class, referencing |
|||
|
|
Based on this example for java... I think a little table worth a thousand words :) |
|||||||||||||
|
|
1) Public Inheritance: a. Private members of Base class are not accessible in Derived class. b. Protected members of Base class remain protected in Derived class. c. Public members of Base class remain public in Derived class. So, other classes can use public members of Base class through Derived class object. 2) Protected Inheritance: a. Private members of Base class are not accessible in Derived class. b. Protected members of Base class remain protected in Derived class. c. Public members of Base class too become protected members of Derived class. So, other classes can't use public members of Base class through Derived class object; but they are available to subclass of Derived. 3) Private Inheritance: a. Private members of Base class are not accessible in Derived class. b. Protected & public members of Base class become private members of Derived class. So, no members of Base class can be accessed by other classes through Derived class object as they are private in Derived class. So, even subclass of Derived class can't access them. |
||||
|
|
Private:The private members of a base class can only be accessed by members of that base class . Public:The public members of a base class can be accessed by members of that base class, members of its derived class as well as the members which are outside the base class and derived class. Protected:The protected members of a base class can be accessed by members of base class as well as members of its derived class. In short:private: base protected: base + derived public: base + derived + any other member |
||||
|
|
|
It's essentially the access protection of the public and protected members of the base class in the derived class. With public inheritance, the derived class can see public and protected members of the base. With private inheritance, it can't. With protected, the derived class and any classes derived from that can see them. |
|||
|
|
|
Summary:
When inheriting, you can (in some languages) change the protection type of a data member in certain direction, e.g. from protected to public. |
|||
|
|
|
I found an easy answer and so thought of posting it for my future reference too. Its from the links http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
|
|||
|
|
|
In simple java programming we find bellow three type of inheritance, Private Only the class in which it is declared can see it. Protected Package Private + can be seen by subclasses or package member. Public Everyone can see it. The access specifiers can be strictly ordered as, public > protected > private where public is least secure and private is highly secure |
|||
|
|
|
See these codes to understand features of c++ about inheritance... I put the result at the end... Hope it helps.
|
||||
|
|
protected by Bo Persson Mar 30 '12 at 18:21
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
