Register Now


Lost Password

Enter your email to reset your password.

BY Author

Java – Member Access Rules in Inheritance

A subclass can contain all the members of its superclass, except private members.

The protected and public members of superclass can be accessed by its subclass, present in the same package or different package.

Members of a superclass with no access modifiers can be accessed by subclass present only in the same package.


class Parent3 
    public String h = "House";
    private String v = "Car";   // v is private so it can't be accessed by any subclass
    protected String m = "Money";
    String s = "Business_Shares";

class Child3 extends Parent3
    String b = "Bike";
    public void childProperty()
        System.out.println("Child Has :: ");

class MemberAccessDemo 
    public static void main(String args[])
        Child3 c = new Child3();


Child Has ::

In this program, we have accessed only those fields of a superclass which is public, protected and default (with no access modifiers) but not private.
If we try to access private members (here, v = “Car”) of superclass we’ll get an error because it is not allowed by Java.


Leave a reply