CodingBison

To restrict the scope of a class or members of a class, Java supports 3 types of access specifiers: (1) Private, (2) Protected, and (3) Public We have seen the key word "public" used in many places and that is the only access specifier we have used so far. Let us see the role access specifiers play in inheritance.

Member access in Inheritance

Now that we are clear with the basic concept of inheritance, let us add some flavor to the concepts we have learned so far. We know that a sub-class inherits all the properties from its super-class. Is this statement completely true? The answer is "NO". The "private" members of a super-class are not inherited by its sub-classes.



Figure: Inheritance, Member Access

As shown in the above figure, let us consider the default access specifier is same as "public". All the default/public members are inherited by the sub-classes. We will see in the coming section understanding "Access Specifiers", that a default access specifier is not same as public and both of them have different scopes.

Now that we have introduced the concept of "public access specifier" to the class members, let us see the difference it makes when we change the scope to "private". Like shown in the below figure, private variables are only visible within the class in which they are defined. Sub-classes will not inherit the private members (both member Variables and methods) from its super-class, the same things holds true in the case of multilevel inheritance as well. For example, if there is an other class say "skyfall" that extends jamesBondMovies class (Which extends movie class), private members of jamesBondMovies class are visible only in that class and are not visible to skyfall class.



Figure: Private Member Access





comments powered by Disqus