DEV Community

Deepikandas
Deepikandas

Posted on

#12 Known is a drop! Access Modifiers in JAVA

An Access modifier limits the visibility of classes, fields, constructors, or methods in the Java program.


Class:
Outer class cannot be private but inner class can be private.

Access modifiers for Default constructor
Default constructor only exists if you write no constructor

Its access modifier matches the class modifier

public class → public constructor

default class → default/package-private constructor

If you write any constructor yourself, Java does not generate a default constructor.

If we declare any constructor of a class as private, then we cannot create an object of that class from outside the class.


Important Takeaways

protected + different package → access allowed only through inheritance.

Child reference = allowed because the object is of type Child, and Child inherits the protected field.

Parent reference = not allowed because the object is not part of the subclass; it’s a foreign Parent object.

Top comments (0)