101. subclass cannot access private members of superclass

subclass cannot access the members of superclass which are declared as private.

Lets implement this on Eclipse IDE:

1. Create 'A' superclass with an instance variable and a method specified as 'private' s shown below:

2. Create 'B' as a subclass of  'A' superclass and try to inherit the private instance variables and methods of 'A' superclass as shown below:

3. Observe that errors 'Not Visible' are getting displayed on trying to inherit instance variable 'a' and method 'sample( )' which are declared as 'private' in 'A' superclass as shown below:

After looking at this error, its very clear that a subclass cannot access the private members of its superclass.

Download this project:

Click here to download this project containing 'A' and 'B' class files used in this post (You can download this project and import into Eclipse IDE on your machine)

Please comment below to feedback or ask questions.

Assigning subclass reference to superclass reference will be explained in the next post.

No comments: