answersLogoWhite

0


Best Answer

The only way that private attributes of a base class can be accessed by a derived class (protected or public) is through a protected or public method in the base class.

The protected or public method is the interface through which access to the attributes is defined and controlled.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Yes, but only the protected and public members of the base class are accessible to the derived class. Private members of the base class are only accessible to friends and to other members of the base class. If any private member needs to be accessible to a derived class, you can either declare the member protected (which exposes that member to all derivatives), or declare the derivative a friend of the base class (exposing all private members to just that one derivative), or keep all private data private, and simply provide a protected abstract interface that all derivatives can use. The latter approach is generally the best as it ensures base class encapsulation is maintained.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago
Java AnswerThis cannot normally be done. Java 1.5 introduced the 'reflection' framework, which allows you to mess with code enough to access these types of things, but this practice is considered to be very poor coding style and should never be done. C++ AnswerIf your derived class is declared to be a friend of the base class, then it can access pretty much anything it wants.
This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The only way a derived class can ever gain access to the private members of a base class (or any class other than itself, for that matter) is when the derived class is declared a friend of the base class. However, in the real world you would never allow a derived class to gain private access to its own base class. The whole point of having a base class is to provide a common generic interface for all its derivatives. And if a derivative ever needs private access then there's something very wrong with your class design.

As far as indirect access to private members is concerned, the only way this is possible is via the protected or public interface of the base class. However the correct term is abstraction.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The base class only knows about its own members, and those it inherits from other base classes. It knows nothing about any of its derivatives.

The one exception is when the base class declares a virtual function which the derived class overrides. When you call the method on the base class, the virtual table (v-table) automatically redirects the call to the most-derived class method, if one exists.

Even if an overridden method exists, the base class still has no knowledge whatsoever of that method's implementation, nor which derived class that method belongs to. The base class merely provides an interface to that implementation. However, having obtained an interface to the derived class, the derived class itself has unrestricted access to any member in that class.

Although it is possible to embed knowledge of a derived class in a base class by including the derived class declaration in the base class and then dynamically casting the base class to its derived class, this is a sign of poor design. The whole point of having a base class in the first place is to provide a common interface to its derivatives, to allow new derivatives to be created whenever required, without any need to alter the base class in any way. Updating a base class every time a new derivative is created is not only a maintenance headache, dynamic casting is an expensive and completely unnecessary overhead. Virtual methods are specifically designed to avoid these problems.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Ordinarily, no. Private members of the base class cannot be inherited by derived classes (only protected and public members can be inherited). If you have no access to the base class source code, then the only way to access private data is via the protected or public interfaces provided. If you do have access to the source code, you could declare the derived class to be a friend of the base class. However, this is rarely a good idea. Ideally, base classes that are intended to be derived from should never require knowledge of their derivatives, and that includes friendship. The only real exception to that rule is when declaring final classes, where the base class has a private constructor (which is only accessible to friend classes) and the friend classes use virtual inheritance (the friend classes cannot be derived from and are therefore final). If a derivative requires private access for any other reason then you must question why the members are private in the first place. Typically it is to enable encapsulation and the interface should be abstract enough to cater for any derivative, polymorphically, not to cater for specific derivatives. In most cases, a protected or public interface would be the preferred option to a derived friendship.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A derived class cannot change how any base class data member is constructed, that is solely determined by the data members themselves. Even a class that contains member data cannot change how its own members are constructed. It would be a crazy world if one class could change how another class is constructed.

All objects are constructed in exactly the same way: base classes (if any) are always constructed before data members (if any). The physical order of construction is determined by the order the base classes and its data members are declared by the class itself.

Although we cannot change how an object is constructed, we can change how it is initialised. Initialisation has no actual bearing on the construction of an object, it only affects the value of an object once it has been constructed. That is, the underlying memory layout of an object is exactly the same regardless of the object's initial value. Again, it would be a crazy world if an object's underlying memory layout could be changed simply by changing its value.

Object initialisation is achieved by invoking an appropriate constructor for the object. Even if an object specifies no constructors we can still initialise it using a member-wise initialisation list. Consider the following:

struct A {

int i;

double d;

};

A a {42, 3.14}; // member-wise initialisation: a.i=42, a.d=3.14

However, we cannot use initialisation lists to initialise base class members. This is by design because the only class that can initialise member data is the class that actually contains those members:

struct B : A {

};

B b {42, 3.14}; // Error!

The only way we can alter the initialisation of base class members is when the derived class provides an appropriate constructor and that constructor then performs the initialisation for us:

struct B : A { B (int x, double y): A {x, y} {}

};

B b {42, 3.14}; // OK! B's constructor invokes member-wise initialisation of A.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

You can't. But you can do something similar by making the private members in the base class protected as opposed to private. This gives derived classes access to those members.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

Public and protected data members are inherited, private data members are not. The same applies to member functions.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain how a private data of a base class can be accessed by publicly derived class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class. A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.


What is the application of public protected and private keywords?

The public, protected, and private keywords are access modifiers that specify if the item they modify can be accessed inside or outside the class or a derived class.A public item is fully accessible, inside or outside the class, including inside a derived class.A protected item is accessible only inside the class or inside a derived class.A private item is accessible only inside the class.


Can a derived class make a public base function private true or false?

True. A derived class can make a public base function private. The derived function is private, within the derived class, but public in other contexts.


How can you access private functions of a class from the Main function in Cpp?

Any member functions and data members declared as 'private' in a class, can only be accessed directly by functions within the class.They cannot be accessed directly by derived objects, nor from anywhere outside an object of the class, such as from the Main function.To access private class members, you must rely on what are called accessor functions. Accessor functions are functions inside the class, either public or protected, which automatically have access to private members.If a function from Main, or elsewhere outside the class hierarchy, needs access, then you need to use publicaccessor functions. For derived class access, you can use protected accessor functions.


Difference between private and protected specifier in c plus plus language?

In C++, the private specifier means that the item can only be accessed by methods of the class, not including methods of derived classes. Protected, on the other hand, means the item can be accessed by methods of the class, and methods of derived classes. Public, to complete the explanation, means that the item can be acessed by any method, this class, another class, or otherwise.

Related questions

What is private specifier in c plus plus?

The private specifier states that the member can only be accessed by the containing class, and not by any derived class, nor by any other code outside of a class.


Is it publicly-held or private-held company?

Accuride Corporation is publicly-held or private-held company?


Is the default access specifier same as protected?

A private member of a class can only be accessed by methods of that class. A protected member of a class can only be accessed by methods of that class and by methods of a derived class of that class.


What is the application of public protected and private keywords?

The public, protected, and private keywords are access modifiers that specify if the item they modify can be accessed inside or outside the class or a derived class.A public item is fully accessible, inside or outside the class, including inside a derived class.A protected item is accessible only inside the class or inside a derived class.A private item is accessible only inside the class.


Private in java?

The private identifier is used to specify that an element can not be directly accessed from the outside. For example, a field or method declared as private can not be accessed directly from outside of the object or class in which it is used.


What is a private brand?

It is a brand that is not publicly traded.


Can a derived class make a public base function private true or false?

True. A derived class can make a public base function private. The derived function is private, within the derived class, but public in other contexts.


Is Forever 21 a publicly traded company?

No, Forever 21 is a private company. It is not publicly traded.


What is the major difference between public and private access method in computer programming?

public means this can be accessed from any class private means this can be accessed only from the current class


Is livescribe a publicly traded company?

No. It is a private company.


Is Toyota a private or a public company?

Toyota is a publicly traded company.


How can you access private functions of a class from the Main function in Cpp?

Any member functions and data members declared as 'private' in a class, can only be accessed directly by functions within the class.They cannot be accessed directly by derived objects, nor from anywhere outside an object of the class, such as from the Main function.To access private class members, you must rely on what are called accessor functions. Accessor functions are functions inside the class, either public or protected, which automatically have access to private members.If a function from Main, or elsewhere outside the class hierarchy, needs access, then you need to use publicaccessor functions. For derived class access, you can use protected accessor functions.