Java Methods – Declaration & Access Specifiers

Complete guide to method declaration, components, and access control in Java.


Table of Contents

1. What Is a Method in Java?

A method is a block of code that performs a specific task and runs only when called.

Why methods matter:

In simple terms: a method defines what to do and how to do it, and it executes on demand.


2. Method Declaration Syntax

2.1 General Syntax

accessModifier returnType methodName(parameters) {
    // method body
}

2.2 Components Explained

Component Purpose
Access Modifier Controls visibility (public, protected, default, private)
Return Type Specifies what the method returns (int, String, void, etc.)
Method Name Identifier used to call the method (camelCase convention)
Parameters Input values passed to the method (optional)
Method Body Contains the logic to be executed

2.3 Example with Return Value

public int add(int a, int b) {
    return a + b;
}

Breakdown:

2.4 Example: No Return Value

public void printMessage() {
    System.out.println("Hello");
}

3. Access Specifiers

Java provides four access levels to control method visibility across classes, packages, and inheritance hierarchies.

3.1 public Methods

Concept:

public void getInvoice() { }

Use case: APIs and entry points


3.2 private Methods

Concept:

private void salaryDetails() { }

What this enforces:

📌 Golden rule: private methods are not inherited.


3.3 protected Methods

Concept:

protected void getBonusDetails() { }

Inheritance behavior:

class JobPortal extends Invoice {
    getBonusDetails(); // allowed
}

📌 Important: protected works through inheritance, not object reference.


3.4 Default (Package-Private) Methods

Concept:

void getRetentionDetails() { }

Critical rule:

class Child extends Parent {
    // ❌ default methods are NOT inherited across packages
}

📌 Common mistake: default ≠ protected


4. Access Matrix

Modifier Same Class Same Package Subclass (other pkg) Other Package
public
protected
default
private

5. Real-World Design Insight

Enterprise pattern example:

📌 Design principle: expose only what is needed, hide everything else.


6. Interview Takeaways

6.1 Key Points

6.2 Rules to Remember

6.3 Mental Model (One Line Each)


7. Types of Methods in Java

In Java, methods can be broadly classified based on who defines them and how they behave.

7.1 System-Defined Methods

What are they? Methods provided by Java libraries.

Examples: System.out.println(), Math.max(), String.length()

System.out.println("Hello");

📌 Key point: you use them, you don’t define them.


7.2 User-Defined Methods

What are they? Methods written by the programmer for specific tasks.

public void profession() {
    System.out.println("I'm in Person Class.");
}

📌 Why they matter: code reuse, readability, and business logic separation.


7.3 Method Overloading

Definition: Multiple methods with the same name but different parameter lists (type/count/order).

int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }

Rules:

📎 See also: Varargs – flexible parameters


7.4 Method Overriding

Definition: A child class provides its own implementation of a method defined in the parent class.

class Doctor extends Person {
    @Override
    public void profession() {
        System.out.println("Doctor profession");
    }
}

Person obj = new Doctor();
obj.profession(); // calls Doctor’s implementation

Runtime behavior:


7.5 Rules for Method Overriding (Must Remember)


7.6 Overloading vs Overriding (Quick Comparison)

Feature Overloading Overriding
Same class
Inheritance needed
Parameters differ
Return type differs Allowed Restricted
Polymorphism Compile-time Runtime

7.7 One-Line Mental Models


7.8 Final Interview Takeaways


📚 Continue Learning

⚡ Static Methods in Java & Best Practices → Click Here

8. Final Methods in Java

8.1 What Is a Final Method?

A final method is a method whose implementation cannot be overridden by subclasses.

public final void profession() { }

📌 Meaning: The behavior of this method is fixed and guaranteed.


8.2 Why Java Provides Final Methods

Final methods are used when:

📌 Example use cases:


8.3 What Happens When You Try to Override a Final Method

class Doctor extends Person {
    public void profession() { }
}

Result: ❌ Compile-time error

Reason:

📌 Compiler rule: A final method cannot be overridden.


8.4 Final vs Overriding

Feature Normal Method Final Method
Can be overridden ✅ Yes ❌ No
Polymorphism ✅ Yes ❌ Fixed behavior
Inheritance Behavior can change Behavior is locked
Design flexibility High Restricted

8.5 Final Methods and Polymorphism

Person p = new Doctor();
p.profession();

Behavior:

📌 Key Insight: Final methods disable runtime polymorphism.


8.6 Final vs Static Methods (Important Distinction)

Aspect Final Method Static Method
Overridable
Polymorphism
Belongs to Object Class
Inherited Hidden
Uses @Override

📌 Important: Final prevents overriding; static prevents polymorphism.


8.7 One-Line Mental Model

Final method → behavior is locked

Static method → behavior is class-level


8.8 Interview Carry-Forward Points


9. Abstract Methods in Java

9.1 What Is an Abstract Method?

An abstract method is a method without a body. It only defines what needs to be done, not how.

public abstract void work();

📌 Key idea: Abstract methods force subclasses to provide an implementation.


9.2 Where Can Abstract Methods Exist?

public abstract class Person { }

9.3 Abstract Class with Both Concrete and Abstract Methods

Abstract classes can mix concrete and abstract behavior:

public abstract class Person {

        public void profession() { }

        public abstract void work();
}

📌 Design insight: Abstract classes provide partial implementation.


9.4 Implementing Abstract Methods in Child Class

public class Doctor extends Person {

        @Override
        public void work() {
                System.out.println("Doctor work");
        }
}

Rule:


9.5 Abstract Methods and Overriding

@Override
public void work() { }

📌 Important: Abstract methods enable runtime polymorphism.


9.6 Abstract Method vs Final Method

Feature Abstract Method Final Method
Has body
Can be overridden Must be
Enforces implementation
Used for Contracts Locked behavior

📌 Rule: A method cannot be both abstract and final.


9.7 Abstract Methods and Polymorphism

Person p = new Doctor();
p.work();

📌 Key Insight: Abstract methods are the foundation of runtime polymorphism.


9.8 Why Use Abstract Methods?


9.9 Restrictions on Abstract Methods


9.10 One-Line Mental Model

Abstract method → “you must implement this.”

Concrete method → “you may use this as-is.”


9.11 Interview Carry-Forward Points


← Back to Basics ☕ Java Hub