i) private: ‘private’ members of a class are not accessible anywhere outside the class. They are accessible only within the class by the methods of that class.
ii) public: ‘public’ members of a class are accessible everywhere outside the class. So any other program can read them and use them.
iii) protected: ’protected’ members of a class are accessible outside the class, but generally, within the same directory.
iv) default: if no access specifier is written by the programmer, then the Java compiler uses a ‘default’ access specifier. It accessible outside the class but within the same directory.
Class ParentClass {
int a;
private int b;
protected int c;
public int d;
ParentClass() {
a=10;
b=20;
c=30;
d=40;
}
void DefaultMethod() {
System.out.println(“Default Method”);
}
public void PublicMethod() {
System.out.println(“Public Method”);
}
private void PrivateMethod() {
System.out.println(“Private Method”);
}
protected void ProtectedMethod() {
System.out.println(“ProtectedMethod”);
}
}
class ChildClass extends ParentClass {
void show() {
System.out.println(“Method Access in Child Class”);
DefaultMethod();
//PrivateMethod();
PublicMethod();
ProtectedMethod();
System.out.println(“Value Access in child class”);
System.out.println(“Default=”+a);
//System.out.println(“private=”+b);
System.out.println(“Public=”+c);
System.out.println(“Protected=”+d);
}
}
class abc {
public static void main(String args[]) {
ChildClass obj=new ChildClass();
obj.show();
System.out.println(“Value Access in Main method”);
System.out.println(“Default=”+obj.a);
//System.out.println(“private=”+obj.b);
System.out.println(“Public=”+obj.c);
System.out.println(“Protected=”+obj.d);
}
}