Friday 03rd May 2024
REGISTRATION
Online Fashion Store

CBSE eBooks

CBSE Guess > eBooks > Class XII > Computer Science By . Mr. MRK

Chapter – 4. CLASSES AND OBJECTS

Previous Index Next

2008 Outside Delhi:

2.a. Differentiate between private and protected visibility modes in context of object oriented programming using a suitable example illustrating each.

Ans: private and protected visibility modes in context of OOP: The visibility mode (private or public or protected) in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or protected derived. The visibility modes basically control the access specifier to be for inheritable members of base class, in the derived class.

Private visibility mode: The private derivation means, the derived class can access the public and private members of the base class privately. With privately derived class, the public and protected members of the base class become private members of the derived class. That means the inherited members can be accessed only through member functions of the derived class.

Protected visibility mode: The protected derivation means that the derived class can access the public and private members of the base class protectedly. With protectedly derived class, the public and protected members of the base calss become protected members of the derived class. That means the inherited members are now not available to the outside world and can be accessed only through the member functions of the derived class and the classes based upon the derived classes. These members can be inherited further if any classes are inheriting from the derived class.

Visibility Inheritable Inheritable Private
Mode public member becomes ( in derived class) protected member becomes (in derived class) member of base class are not directly accessible to derived class.
protected Protected protected    
private Private private    

private and protected access specifiers in context of OOP: private access specifier is used to make any variable or a method which has a limited access within the class only. At the time of inheritance, these variables cannot be accessed (inherited) to the derived class. protected access specifier is used to make any variable or a method which has a limited access within the class only (here like private). But at the time of inheritance, these variables can be inherited to the derived class. Except regarding inheritance, both access specifiers ie private and protected will work same. Eg:

class student
{
private: int rno;
char name[21];
protected: int age;
void input( );
void display( );
}

Here, since rno and name are declared in private, they can be accessed only inside the class. Since age,input( ) and display() are declared in protected, they also can be accessed only inside the class but they can be inherited, where as private members (rno and name) cannot be inherited.

Previous Index Next