answersLogoWhite

0


Best Answer

All fields and methods of a class are inherited:

public class A

{private int x =10;public int XVal{get{return x;}}}

public class B:A

{public B(){x = 20;}}

User Avatar

Wiki User

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

Wiki User

13y ago

In most common OO languages, such as C++, C#, Java, VB.net, etc. private members cannot be inherited, only public and protected members.

This answer is:
User Avatar

User Avatar

Anonymous

Lvl 1
4y ago

true

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can private members of a class be inherited?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the things not inherited from parent class in Java?

Private members are not inherited from the parent class.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


In c plus plus what is the purpose of the keyword public and private in the definition of a class?

Public members/functions can be accessed from outside the class, private members/functions can only be accessed from functions of that class. Ex. class sampleClass{ private int value; public void setValue(int a){value = a;} /* legal, value can be accessed since this is a method within the same class */ public int getValue(){return value;} }; int main() { sampleClass sc; // class is instantiated sc.setValue(5); // legal, setValue() is public sc.value = 7; // ERROR, value is private, will not compile printf("%d\n", sc.getValue()); // Will print 5 return 0; }

Related questions

What are the things not inherited from parent class in Java?

Private members are not inherited from the parent class.


Why private data members or functions are not inherited?

Because that's what private means. Private data members or functions are intended to be usable only in the base class, and the inheriting class can only access protected or public members or functions.


Is it true that a derived class inherits all the members of its base class?

False. A derived class inherits the public and protected members of its base class. Private members of the base class cannot be inherited.


Which base class member functions are not inherited by a derived class?

Derived classes only inherit the protected and public members of their base classes. Private member functions cannot be inherited by a derived class.


What do you mean by protected derivation of a sub class from base class?

When you derive a class (the sub-class) from a base class using protected access, all public members of the base class become protected members of the derived class, while protected members of the base class will remain protected. Private members are never inherited so they remain private to the base class. By contrast, if you use public inheritance, the public members of the base class remain public to the derived class, while protected members of the base class remain protected in the derived class. If you use private inheritance, both the public and protected members of the base class become private to the derived class. Note that accessibility cannot be increased, only reduced or left the same. That is, a protected member of a base class cannot be inherited as a public member of a derived class -- it can only be declared private or remain protected. Note also that accessibility is viewed from outside of the derived class. That is, all members of a base class other than the private members are inherited by the derived class and are therefore fully accessible to the derived class. But from outside of the derived class, all base class accessibility is determined by the access specified by the type of inheritance.


What is public derivation in object-oriented programming?

Public derivation or public inheritance means that all the public members of the base calls are declared public in the derived class while the protected members remain protected. Protected inheritance means all the public members of the base class are declared protected in the derived class, as are the protected members. Private inheritance means all the public and protected members of the base class are declared private in the derived class. Private members of the base class are never inherited and are therefore unaffected by inheritance. Note that regardless of the type of inheritance specified, individual non-private members of the base class can be inherited with public or protected access as required of the derived class. The type of inheritance can be therefore be thought of as being the default inheritance for all base class members which can (optionally) be overridden for specific members where required.


In c plus plus what is the purpose of the keyword public and private in the definition of a class?

Public members/functions can be accessed from outside the class, private members/functions can only be accessed from functions of that class. Ex. class sampleClass{ private int value; public void setValue(int a){value = a;} /* legal, value can be accessed since this is a method within the same class */ public int getValue(){return value;} }; int main() { sampleClass sc; // class is instantiated sc.setValue(5); // legal, setValue() is public sc.value = 7; // ERROR, value is private, will not compile printf("%d\n", sc.getValue()); // Will print 5 return 0; }


What specifies private access modifier and private base class?

A private variable is something that is not visible outside the class. A private class is one that cannot be inherited


What are the privileges granted to friend function?

Friend functions (and classes) have private access to the classes that declare them as friends. Although they have the same access rights as members of the class itself, friends are not themselves members of the class and cannot be inherited.


Difference bitween various visibility modes used c plus plus?

By visibility I assume you mean member accessibility. C++ uses three levels of accessibility: private, protected and public. Private members are only accessible to the class itself and friends of the class. Protected members are the same as private members except derived classes also have access. Public members are fully accessible. With regards inheritance, base class members with greater access than that specified are reduced to the specified access in the derived class. Thus public inheritance has no effect on base class member access. Protected inheritance reduces public members of the base class to protected members of the derived class. Private inheritance reduces both public and protected members of the base class to private members of the derived class. Private members of the base class are never inherited, thus they always remain private to the base class. Note that access to base class members can never be increased through inheritance, only reduced or kept the same. However, as well as defining an overall inheritance access, you can also specify member-wise inheritance access. Thus you could use public inheritance overall, but specify certain public members of the base class to be protected or private in the derived class and/or certain protected members of the base class to be private members of the derived class.


What is public private protected variable function?

The public, protected and private keywords only apply to object oriented programming languages. They are used to determine the accessibility of specific class members and their bases. Private members are only accessible to the class and to friends of the class. Protected members are the same as private but are also accessible to derivatives of the class. Public members are accessible to all code. When applied to base classes, the public, protected and private keywords can be used to either maintain or reduce the accessibility of the base class members (but never to increase their accessibility). When declared public, the accessibility of the base class members remains as defined by the base class. When declared protected, the public members become protected members. And when declared private, all members of the base class become private members. As well as defining the overall accessibility of the base class members, the accessibility of individual base class members can also be specified.


How does a class in c plus plus enforce data encapsulation?

Data encapsulation is enforced by restricting access to the class members. Access can be specified on a per-member basis, defaulting to private access for a class and public access for a struct. Private members are accessible to class members and to friends of the class. Protected members are the same as private members but are also accessible to derived class members. Public members are fully-accessible. Data members are typically declared private while interfaces are typically declared public or protected.