What Are Access Specifiers? Should I Inherit With Private, Protected or Public

What are access specifiers? Should I inherit with private, protected or public?

what are Access Specifiers?

There are 3 access specifiers for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are:

Public - The members declared as Public are accessible from outside the Class through an object of the class.

Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it.

Private - These members are only accessible from within the class. No outside Access is allowed.

An Source Code Example:

class MyClass
{
public:
int a;
protected:
int b;
private:
int c;
};

int main()
{
MyClass obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, gives compiler error
obj.c = 30; //Not Allowed, gives compiler error
}

Inheritance and Access Specifiers

Inheritance in C++ can be one of the following types:

  • Private Inheritance
  • Public Inheritance
  • Protected inheritance

Here are the member access rules with respect to each of these:

First and most important rule Private members of a class are never accessible from anywhere except the members of the same class.

Public Inheritance:

All Public members of the Base Class become Public Members of the derived class &

All Protected members of the Base Class become Protected Members of the Derived Class.

i.e. No change in the Access of the members. The access rules we discussed before are further then applied to these members.

Code Example:

Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};

class Derived:public Base
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};

int main()
{
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error

}

Private Inheritance:

All Public members of the Base Class become Private Members of the Derived class &

All Protected members of the Base Class become Private Members of the Derived Class.

An code Example:

Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};

class Derived:private Base //Not mentioning private is OK because for classes it defaults to private
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};

class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error
}
};

int main()
{
Derived obj;
obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error

}

Protected Inheritance:

All Public members of the Base Class become Protected Members of the derived class &

All Protected members of the Base Class become Protected Members of the Derived Class.

A Code Example:

Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};

class Derived:protected Base
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};

class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; //Allowed, a is protected member inside Derived & Derived2 is public derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allowed, Compiler Error
}
};

int main()
{
Derived obj;
obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}

Remember the same access rules apply to the classes and members down the inheritance hierarchy.


Important points to note:

- Access Specification is per-Class not per-Object

Note that the access specification C++ work on per-Class basis and not per-object basis.

A good example of this is that in a copy constructor or Copy Assignment operator function, all the members of the object being passed can be accessed.

- A Derived class can only access members of its own Base class

Consider the following code example:

class Myclass
{
protected:
int x;
};

class derived : public Myclass
{
public:
void f( Myclass& obj )
{
obj.x = 5;
}
};

int main()
{
return 0;
}

It gives an compilation error:

prog.cpp:4: error: ‘int Myclass::x’ is protected

Because the derived class can only access members of its own Base Class. Note that the object obj being passed here is no way related to the derived class function in which it is being accessed, it is an altogether different object and hence derived member function cannot access its members.


What is a friend? How does friend affect access specification rules?

You can declare a function or class as friend of another class. When you do so the access specification rules do not apply to the friended class/function. The class or function can access all the members of that particular class.

So do friends break Encapsulation?

No they don't, On the contrary they enhance Encapsulation!

friendship is used to indicate a intentional strong coupling between two entities.

If there exists a special relationship between two entities such that one needs access to others private or protected members but You do not want everyone to have access by using the public access specifier then you should use friendship.

What is the difference between public, protected, package-private and private in Java?

The official tutorial may be of some use to you.

















































ClassPackageSubclass
(same pkg)
Subclass
(diff pkg)
World
public+++++
protected++++
no modifier+++
private+

What is the difference between public, private, and protected inheritance in C++?

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: public, protected and private.

Let:

class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};
  • Everything that is aware of Base is also aware that Base contains publicMember.
  • Only the children (and their children) are aware that Base contains protectedMember.
  • No one but Base is aware of privateMember.

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 Base and a class Child that inherits from Base.

  • If the inheritance is public, everything that is aware of Base and Child is also aware that Child inherits from Base.
  • If the inheritance is protected, only Child, and its children, are aware that they inherit from Base.
  • If the inheritance is private, no one other than Child is aware of the inheritance.

Why Java Does not Have Access Specifier based Inheritance

C++ provides the ability to allow physical inheritance to be used as part of the implementation detail of a class.

This part of C++ was designed before Barbara Liskov introduced what is now known as the Liskov substitution principle. To satisfy the Liskov substitution principle, wherever an instance of the superclass is used, an instance of a subclass may be used as well. In your case, you could pass a B to a method that expected an A.

Java was designed after the introduction of this principle. In Java, the physical mechanism of inheritance is used only for classes that are intended to logically inherit from their superclass. To use the implementation of a class, but not its interface, you can use delegation.

From Stroustrup's "The design and evolution of C++":

"The private/public distinction for base classes predates the debate on implementation inheritance versus interface inheritance by about five years [Synder,1986][Liskov,1987]. If you want to inherit an implementation only, you use private derivation in C++. Public derivation gives users of the derived class access to the interface provided by the base class. Private derivation leaves the base an an implementation detail; even the public members of the base class are inaccessible except through the interface explicitly provided for the derived class."

why doesnt the support of public or private or protected Inheritance not give in c#

Maybe this is a comment, but its too big to fit in the comment area.

I took your class definitions and tried this out -

class A
{
public:
int pub1;
private:
int prvt1;
protected:
int proc1;
};


class B : public A
{

//public int pub1;//This variable is because of inheritacne and is internal.
//public int proc1;//This variable is because of inheritacne and is internal.
public:
int pub2;
private:
int prvt2;
protected:
int pro2;
};

int main()
{
B* b = new B();
b->pub2 = 1;
b->proc1 = 2;
}

The compiler barked at me -

prog.cpp: In function ‘int main()’:
prog.cpp:8:9: error: ‘int A::proc1’ is protected
prog.cpp:29:8: error: within this context

I guess C++ doesn't convert protected member into public. Or am I missing something from the question?

Default inheritance access specifier

Just a small addition to all the existing answers: the default type of the inheritance depends on the inheriting (derived) type (B in the example), not on the one that is being inherited (base) (A in the example).

For example:

class A {};
struct B: /* public */ A {};
struct A {};
class B: /* private */ A {};


Related Topics



Leave a reply



Submit