Member access and Inheritance

Filter Course


Member access and Inheritance

Published by: Dikshya

Published date: 26 Jun 2023

Member access and Inheritance

In object-oriented programming, member access refers to how classes and objects can access the members (fields, methods, and constructors) of other classes. Inheritance plays a crucial role in determining the accessibility of members in different scenarios.

-Public Access:

Members declared as public are accessible by any class or object, both within the same package and in other packages. Public members have the widest scope of accessibility. When a class inherits from another class, it can freely access the public members of the superclass.

public class Superclass {
    public int publicField;
    public void publicMethod() {
        // Code here
    }
}

public class Subclass extends Superclass {
    public void accessSuperclassMembers() {
        publicField = 10;      // Accessing public field of superclass
        publicMethod();       // Accessing public method of superclass
    }
}

Protected Access:

Members declared as protected have package-level accessibility, just like public members, but they are also accessible to subclasses (even if they are in a different package). Protected members allow subclasses to inherit and use those members.

Example:

public class Superclass {
    protected int protectedField;
    protected void protectedMethod() {
        // Code here
    }
}

public class Subclass extends Superclass {
    public void accessSuperclassMembers() {
        protectedField = 10;   // Accessing protected field of superclass
        protectedMethod();    // Accessing protected method of superclass
    }
}

Default (Package-Private) Access:

Members that are not explicitly specified with public, protected, or private access modifiers have default access. Default members are accessible only within the same package. When a class inherits from another class, it can access the default members of the superclass if both classes are in the same package.

Example:

package com.example;

public class Superclass {
    int defaultField;
    void defaultMethod() {
        // Code here
    }
}

package com.example;

public class Subclass extends Superclass {
    public void accessSuperclassMembers() {
        defaultField = 10;     // Accessing default field of superclass
        defaultMethod();      // Accessing default method of superclass
    }
}

Private Access:

Members declared as private are only accessible within the same class. Private members are not inherited by subclasses and are not accessible outside the class.

public class Superclass {
    private int privateField;
    private void privateMethod() {
        // Code here
    }
}

public class Subclass extends Superclass {
    public void accessSuperclassMembers() {
        // Cannot access privateField or privateMethod here
    }
}

Subclasses can inherit from a superclass members that are accessible, provided that the access modifiers placed on those members are respected. The superclass's public and protected members are readily accessible to the subclass. Subclasses inside the same package have access to default members. Private members, however, are neither inheritable by subclasses nor immediately accessible.

It's crucial to remember that subclasses have the ability to provide their own implementation by overriding the superclass's functions with the @Override annotation. In order to support polymorphism and dynamic method dispatching, the actual object type is used to identify the proper method implementation at runtime.