0% found this document useful (0 votes)
42 views6 pages

Java Constructor Access Specifiers Explained

This document about java constructor

Uploaded by

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

Java Constructor Access Specifiers Explained

This document about java constructor

Uploaded by

Kishor Aswar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Constructor Access Specifiers in

Java
Public | Private | Protected | Default
Public Constructor

• ✔ Can be accessed from anywhere.


• ✔ Most common constructor type.
• Example:
• class Car {
• public Car() { [Link]("Car created!"); }
• }
Private Constructor

• ✔ Object creation restricted.


• ✔ Used in Singleton or Utility classes.
• Example:
• class Database {
• private Database() {}
• public static Database getInstance() { return new Database(); }
• }
Protected Constructor

• ✔ Accessible within same package and subclasses.


• ✔ Not accessible directly outside package.
• Example:
• class Vehicle {
• protected Vehicle() {}
• }
Default (Package-Private)
Constructor
• ✔ No modifier used.
• ✔ Accessible only within same package.
• Example:
• class Student {
• Student() { [Link]("Student created"); }
• }
Summary: Constructor Access
Specifiers
Access Specifier Who Can Access?

public Anywhere

private Only inside the same class

protected Same package + subclasses

default Same package only

You might also like