0% found this document useful (0 votes)
3 views3 pages

Access Modifiers and Inheritance

The document explains access control in inheritance in Java, detailing four access modifiers: private, default, protected, and public. It outlines the accessibility of each modifier in relation to subclasses and packages, providing code examples for clarity. Key takeaways include that private members are not inherited, default members are package-private, protected members are accessible in subclasses, and public members are accessible everywhere.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Access Modifiers and Inheritance

The document explains access control in inheritance in Java, detailing four access modifiers: private, default, protected, and public. It outlines the accessibility of each modifier in relation to subclasses and packages, providing code examples for clarity. Key takeaways include that private members are not inherited, default members are package-private, protected members are accessible in subclasses, and public members are accessible everywhere.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Access Control in Inheritance in Java

In Java, access control in inheritance determines how members (fields, methods,


constructors) of a superclass can be accessed in a subclass. Java provides four levels of access
control:

Subclass (Different
Modifier Same Class Same Package Everywhere
Package)
private ✅ ❌ ❌ ❌
default (no
✅ ✅ ❌ ❌
modifier)
protected ✅ ✅ ✅ ❌
public ✅ ✅ ✅ ✅

1. private Access Modifier


 Members declared private in the superclass are not accessible in the subclass.
 These members are only accessible within the same class.

class Parent {
private int privateVar = 10;
}

class Child extends Parent {


void display() {
// System.out.println(privateVar); // ERROR: privateVar is not accessible
}
}

2. Default (No Modifier) Access


 Members without an access modifier are only accessible within the same package.
 If a subclass is in a different package, it cannot access the default members.

package package1;

class Parent {
int defaultVar = 20; // Default access
}

package package2;
import package1.Parent;

class Child extends Parent {


void display() {
// System.out.println(defaultVar); // ERROR: defaultVar is not accessible
}
}

3. protected Access Modifier


 Members declared protected can be accessed:
o Within the same package (like default access).
o By subclasses in any package (via inheritance).

package package1;

public class Parent {


protected int protectedVar = 30;
}

package package2;
import package1.Parent;

class Child extends Parent {


void display() {
System.out.println(protectedVar); // ✅ Accessible because it's protected
}
}

4. public Access Modifier


 public members are accessible from anywhere (within and outside packages).

package package1;

public class Parent {


public int publicVar = 40;
}

package package2;
import package1.Parent;

class Child extends Parent {


void display() {
System.out.println(publicVar); // ✅ Accessible from anywhere
}
}
Accessing Protected Members Using Superclass Reference
Even though protected members can be accessed in subclasses, they cannot be accessed
through a superclass reference from another package.

package package1;
public class Parent {
protected int protectedVar = 50;
}

package package2;
import package1.Parent;

class Test {
public static void main(String[] args) {
Parent obj = new Parent();
// System.out.println(obj.protectedVar); // ERROR: Not accessible through
Parent reference
}
}

Key Takeaways

 private → Not inherited, only accessible in the same class.


 default (no modifier) → Accessible only within the same package.
 protected → Accessible within the same package and in subclasses (even
in different packages).
 public → Accessible everywhere.

You might also like