Understanding Inheritence - Part 2

Can the child class access every member of the parent class ? Lets understand this concept

No. The child class only has access to the public and protected members of the parent class. It cannot use the private members of the parent class. There are two questions which can be asked and their answers are different. Please read these questions properly

Q.1 ) Out of public, private and protected members of the parent class what all is inherited by the child class ?
Answer : Everything. The child class inherits everything from the parent class, including the private data members

Q.2 ) Out of all the members inherited from the parent class what all can the child class use ?
Answer : The child class can only use public and protected data members inherited from the parent class. It cannot use the private data or private functions from the parent class.

Example

Class Parent
{
    Public int X;
    Private int Y;
    Protected int Z;
}


Class Child inherits Parent
{
}

Question : In the classes above how many data members are there in the parent class ?
Answer : Parent class as three data members X, Y and Z. All are of integer types

Question : How many data members are there in the child class ?
Answer : Child class has three data members X, Y and Z which it has inherited from the parent class.

Question : Out of X, Y and Z which data members can the object of the parent class use ?
Answer : The object of the parent class can use all three data members because they are a part of the parent class.

Question : Out of X, Y and Z data members which the child class has inherited from the parent class which all data members can the object of the child class use ?
Answer : The object of the child class can only use two data members X and Z. It cannot use Y because it is a private member of the parent class and in case of inheritance the child classes cannot access the private members of the parent class.

It’s a common misconception which programmers have that the child class only inherits public and protected members from the parent class. This is wrong. The child class will always inherit everything from the parent class but can only use public and protected members.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp