answersLogoWhite

0

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.

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

How can the base class data be accessed from database directly using derived class?

In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.


What do you mean by inheriting the interface?

In object oriented programming, a derived class inherits the protected and public members of its base class. Those members therefore define the interface that is inherited by the derived class. The derived class may augment that interface to provide a more specialised implementation of the interface, without the need to re-write the generic interface of the base class. The implication is that the derived class is a more specialised form of the base class.


What is the difference between protected and friend in c plus plus?

The private, protected and public keywords are used to modify the access specifiers of class or struct members. Unless otherwise specified, class members are private by default, while struct members are public by default. Private members of a class are only accessible to members and to friends of that class. Protected members are the same as private members, but are also accessible to derived classes. Public members have unrestricted access. The private, protected and public access specifiers can also be used to modify the type of inheritance that applies to a derived class. Private inheritance means all public and protected members of the base class become private members of the derived class. Protected inheritance means all public members of the base class become protected members of the derived class. Public inheritance means all public and protected members of the base class remain public and protected members of the derived class. Private members of the base class are never inherited by derived classes. A derived class or one or more of its member functions may be declared a friend of the base class, thus permitting private access, but you would never do this unless the hierarchy were a closed, static hierarchy where all derivatives can be determined at compile time. Dynamically bound derivatives of unknown origin cannot be declared friends.


What does the keyword extends mean in BlueJ?

The "extends" keyword in Java is how you declare a subclass.class Base {void foo() {}}class Sub extends Base { //Sub is now a subclass of Basevoid begin(){Sub s = new Base(); //cannot do this the other way around!}}


What is function overridding in c?

Function overriding applies to class methods and is only applicable within a derived class. Derived classes inherit all the public and protected members of their base classes, and all inherited methods can be overridden by the derived class to provide implementations that are specific to the derivative. The base class implementations should be thought of as being more generalised implementations that are specific only to the base class itself. However, some or all of the base class implementations may well be sufficient for your derivative in which case there is no need to override those methods at all. You only need to override methods that require more specialised implementation.Methods that are declared virtual within the base class are expected to be overridden by the derived class. By contrast, all pure-virtual methods must be overridden otherwise the derived class, like its base class, is rendered abstract. You cannot instantiate an abstract class -- it is intended to be a conceptual object (like a shape) rather than an actual object (like a square or a circle).You may also override non-virtual methods, however the fact they are non-virtual in the first place means that, barring an oversight on the part of the base class designer, it is not expected that you do so. That doesn't mean that you cannot override them, but it does mean that the base class methods (including all overloaded version of the override) are effectively hidden from the derived class. Often this would be undesirable but, occasionally, that's exactly what you want.Base classes cannot foresee what classes you might derive from them in the future. Fortunately they do not need to know anything about those classes (if they did, you'd have to continually modify the base class every time you created a new derivative). By declaring methods to be virtual, the virtual table ensures that all derived classes will "do the right thing" even when those methods are called implicitly from within the base class implementations. As a result, there is no need for a base class to ever know the runtime information of its derived classes (and if there is, you'll immediately know there's something very wrong with your class design).Often an override simply needs to augment a base class method rather than override it completely. This is achieved by calling the base class method explicitly from anywhere within the overridden implementation. This reduces the need to duplicate otherwise functional code that already exists in the base class. It should be noted that base class methods should never be called explicitly from within the base class itself; the override is always expected to be called (implicitly) and this behaviour should only ever be over-ruled by the derivative. Calling a base class method explicitly from outwith the derived class may cause unwanted side-effects because the derived class will no longer "do the right thing".Pure-virtual methods of a base class may or may not provide an implementation, but either way you must provide an implementation within your override. If calling the base class method explicitly causes a compile time error (stating no such method exists) then the base class provides no implementation at all, therefore you are expected to provide a complete implementation. As an example, a shape class may have a pure-virtual Draw() method but it cannot implement it without knowing what type of shape it is. Whereas a circle class that is derived from the shape class will always know how to draw itself and must therefore provide the complete implementation of the Draw() method.Overridden virtual methods come into their own when dealing with collections of objects. The objects themselves needn't be of the same type, but so long as they all share a common base class, you can build a collection from the base class alone (all the objects have an "is-a" relationship with their common base class). The collection itself is only concerned with what the base class is actually capable of, as defined by the base class interface, but the virtual table ensures that, regardless of the runtime type of the derived classes, every object in the collection exhibits the correct behaviour.

Related Questions

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.


How can the base class data be accessed from database directly using derived class?

In order to access a base class member the base class member must be declared protected or public. Private data is only accessible to members of the same class and to friends of the class. I'm not entirely sure what you mean by accessing from a database. A derived class does not require a database in order to access its base class members, it only requires protected access at the very least. Note that although you can declare derived classes to be friends of their base classes and thus allow private access, it would be an unusual design since base classes should never know anything about any of their derivatives.


What do you mean by inheriting the interface?

In object oriented programming, a derived class inherits the protected and public members of its base class. Those members therefore define the interface that is inherited by the derived class. The derived class may augment that interface to provide a more specialised implementation of the interface, without the need to re-write the generic interface of the base class. The implication is that the derived class is a more specialised form of the base class.


Derivation on math?

you mean like calculus?


What is the phrase disparted imact mean?

There no such words as disparted or imact. If you mean 'disparate impact' then this is a legal term used in the US regarding employment law. Where there is an unintentional and disproportionate and adverse impact on a protected minority group or class but not against a non-protected group or class.


What does the name 'Amani' mean?

The Arabic derivation means 'desires, aspirations, wishes'. The Swahili derivation means 'harmony, peace'.


What doesDE mean in surname?

The Latin, Spanish and Portuguese derivations mean "from", while the French derivation means "of".


What does DeAnte' mean?

From what I can find in my brief searches is that Deante is a derivation of the name Dante.


Does your name Natalia mean anything?

It is a derivation of the word Christmas. Natale...means Christmas


What is the difference between protected and friend in c plus plus?

The private, protected and public keywords are used to modify the access specifiers of class or struct members. Unless otherwise specified, class members are private by default, while struct members are public by default. Private members of a class are only accessible to members and to friends of that class. Protected members are the same as private members, but are also accessible to derived classes. Public members have unrestricted access. The private, protected and public access specifiers can also be used to modify the type of inheritance that applies to a derived class. Private inheritance means all public and protected members of the base class become private members of the derived class. Protected inheritance means all public members of the base class become protected members of the derived class. Public inheritance means all public and protected members of the base class remain public and protected members of the derived class. Private members of the base class are never inherited by derived classes. A derived class or one or more of its member functions may be declared a friend of the base class, thus permitting private access, but you would never do this unless the hierarchy were a closed, static hierarchy where all derivatives can be determined at compile time. Dynamically bound derivatives of unknown origin cannot be declared friends.


What does the keyword extends mean in BlueJ?

The "extends" keyword in Java is how you declare a subclass.class Base {void foo() {}}class Sub extends Base { //Sub is now a subclass of Basevoid begin(){Sub s = new Base(); //cannot do this the other way around!}}


Are professionally taken photos protected?

Do you mean copyright protected? Yes, they are.