Inheritance is one of the main features of programming in Java. It is a mechanism in which one or more classes acquire the properties of another class. The class which inherits the properties of another class is called the subclass or the child class. The class whose properties are inherited is known as the base class, super class or the parent class. Accessing variables and methods by using Java Access Modifiers implements reusability of existing code.
What are Java Access Modifiers?
Java access modifiers specify which class can access a given class and its properties. There are three access modifiers in java. These are:
Public Modifier
Public Access modifier is the default modifier and the easiest of three. It is used to specify that a member is visible to all the classes and can be accessed from everywhere. We should declare public members only if such access does not produce undesirable results. These act like secrets which can be known to anyone.
Protected Modifier
This specifies that the member can be accessed by only a class’s own members or the members of its sub-class. These act as family secrets which only the concerned family knows but no outsiders know about it.
Private Modifier
Private access modifier is the most restrictive access level. It specifies that the members can only be accessed by its own class i.e. the class where it is defined. This is used with variables or methods containing information that need not be accessed by an outsider as it could make the program inconsistent. This modifier is the primary method by which an object encapsulates itself and hides its data from the outside world. Hence, these act as secrets which we do not tell anybody.
Using Java Access Modifiers
Now, let us have a better understanding to how these modifiers are used and inherited in various classes.
Private access modifier
If a method or variable in a class is declared as private, then only the code inside the same class can access the variable or make a call to that method. Subclasses will not be able to access these methods and variables, nor can an external class do so. This is so because a subclass does not inherit the private members of the parent class.
Classes cannot be marked as private as making a class private will mean that no other class could access it which would mean that you cannot really use that class. Hence, classes are never declared as private.
Let us take up an example:
// Parent Class class pClass { private int num1; void setdata(int n) { num1 = n; } int getdata() { return num1; } } // sub class class sub extends pClass { int num2; void prod() { int num = getdata(); System.out.println("Product = " + (num2 * num)); } } class privateEg { public static void main(String args[]) { sub obj = new sub(); obj.setdata(15); obj.num2 = 10; obj.prod(); } }
Output
In the above example, num1 is a private member of the class pClass. Only the members setdata() and getdata() in the base class can access this field directly by its name. However it is inaccessible to members of the derived class. So, in order to access the private member in the method product of the subclass, we call the getdata() method of the base class which is by default public and is directly accessible to the subclass.
Protected Access Modifier
A protected variable or method in a public class can be accessed by its own members or the members of its sub-class only. This holds true even if the subclass is not located in the same package as the super class.
Let us take up an example:
// Parent Class class pClass { protected int num1, num2; pClass(int a, int b) { num1 = a; num2 = b; } void show() { System.out.println("Number 1 = " + num1); System.out.println("Number 2 = " + num2); } } // sub class class sub extends pClass { private int ans; sub(int a, int b) { super(a,b); } void prod() { ans = num1 * num2; } void showprod() { System.out.println("Product = " + ans); } } class proEg { public static void main(String args[]) { sub obj = new sub(10,8); obj.show(); obj.prod(); obj.showprod(); } }
Output
Public Access Modifier
This is the default access modifier. If we do not specify any access modifier it will be public by default. We use public when we want that the variable or method should be accessible by the entire application.
Summary
Hence, we can conclude that the derived class inherits all the members and methods that are declared as protected or public. But, if the members of the super class are declared as private then the derived class cannot directly use them. The private members can be accessed by members of the same class.