Java
method scope
default access
Java methods
object-oriented programming

What is the default scope of a method in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When discussing the scope of a method in Java, we're essentially exploring its accessibility from different parts of a program. In Java, methods are encapsulated within classes, and their accessibility is controlled via access modifiers. These modifiers determine whether other classes or packages can use the method. In Java, the default access level is often misunderstood, and understanding it can help in better designing your classes for security, reusability, and efficiency.

Java Access Modifiers

Java provides four access modifiers, and they determine the visibility of classes and class members like methods. Here's a quick rundown:

  1. public: The method is accessible from any other class.
  2. protected: The method is accessible within the same package or subclasses.
  3. default: Also known as "package-private"; the method is accessible only within its own package.
  4. private: The method is accessible only within the class it is declared.

Default Access Level

When you declare a method without specifying an access modifier, it takes on the default access level, often called "package-private." This means the method is accessible only within classes that are in the same package.

Example of Default Access Level

Consider two classes in the same package:

java
1package com.example;
2
3class Sample {
4    void display() {
5        System.out.println("Default access method in Sample class.");
6    }
7}
8
9class Main {
10    public static void main(String[] args) {
11        Sample sample = new Sample();
12        sample.display(); // This works because Main and Sample are in the same package.
13    }
14}

Now, consider accessing the display method from a class in a different package:

java
1package com.other;
2
3import com.example.Sample;
4
5class OtherMain {
6    public static void main(String[] args) {
7        Sample sample = new Sample();
8        sample.display(); // This will cause a compile-time error because the method has default access level.
9    }
10}

In this example, OtherMain cannot access display() because it resides in a different package from the Sample class.

Key Points

Below is a table summarizing the key points about Java method access levels:

Access ModifierClassPackageSubclassGlobal
publicYesYesYesYes
protectedYesYesYesNo
defaultYesYesNoNo
privateYesNoNoNo
  • "Class" refers to the class people are working on.
  • "Package" refers to other classes in the same package.
  • "Subclass" refers to derived classes, possibly in different packages.
  • "Global" refers to classes outside the package and subclass hierarchy.

Additional Details

Why Use Default Access?

Using default access is particularly beneficial in scenarios where:

  • You want to limit the method's access to only those classes that logically form a coherent module.
  • You aim to promote encapsulation by preventing access by unrelated classes.
  • You wish to hide the internal implementation while exposing a simpler interface.

Alternatives to Default Scope

Depending on your design and architectural needs, you might consider using other access levels:

  • public: When you want universally accessible methods.
  • protected: Ideal if you plan to extend your class and want subclasses to reuse or modify behavior.
  • private: Use this to encapsulate the behavior entirely within the class.

Conclusion

The default scope in Java plays a crucial role in maintaining the integrity and modularity of your applications. By constraining access to its containing package, it provides a balanced encapsulation level. As you design Java applications, understanding and using the default and other access levels can significantly impact your software's robustness and maintainability.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.