DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 4 - Tightly Encapsulated Class in Java

After learning Data Hiding, Abstraction, and Encapsulation, let's explore a concept that frequently appears in Java interviews and certification exams: Tightly Encapsulated Class.

At first glance, you might think a tightly encapsulated class is simply a class with private variables and public getters and setters.

That's a common misconception.

In Java, the definition is much simpler—and much stricter.

There is only one rule you need to check.

In this article, we'll understand that rule, explore inheritance-related interview traps, and learn how to identify tightly encapsulated classes with confidence.


What is a Tightly Encapsulated Class?

A Tightly Encapsulated Class is a class in which every instance variable is declared as private.

In simple words:

A class is said to be tightly encapsulated if and only if every variable of that class is declared as private.

That's the complete definition.

It doesn't matter whether:

  • Getter methods exist
  • Setter methods exist
  • Getters or setters are public
  • Constructors are present

The only thing that matters is the visibility of the variables.


Why Do We Need a Tightly Encapsulated Class?

The purpose of a tightly encapsulated class is to ensure that all internal data remains protected from direct access.

When every variable is private:

  • External classes cannot modify object state directly.
  • Business rules can be enforced through methods.
  • Objects remain in a valid state.
  • Maintenance becomes easier.

Think of a locker.

You may have many keys, passwords, or authentication methods to open it.

But the locker is considered secure because everything inside is locked.

Similarly, Java checks only whether the variables are private.


The Single Rule

This is the easiest table to memorize.

Check Required?
Every variable is private ✅ Yes
Getter methods exist ❌ No
Setter methods exist ❌ No
Getters are public ❌ No
Setters are public ❌ No
Constructors exist ❌ No

Only one rule matters.

Every variable must be private.


Common Interview Trap

Many developers answer:

"A tightly encapsulated class must have private fields and public getters and setters."

This is incorrect.

Getter and setter methods are not part of the definition.

Java only checks the variables.


Basic Example

class Account {

    private double balance;

    public double getBalance() {
        return balance;
    }

}
Enter fullscreen mode Exit fullscreen mode

Is this class tightly encapsulated?

Answer: ✅ Yes.

Why?

  • balance is private.
  • No setter exists.
  • Java doesn't care.

The single rule passes.


How It Works

Step 1

The class declares one instance variable.

Step 2

The variable is marked private.

Step 3

Java checks all variables.

Step 4

Since every variable is private, the class is tightly encapsulated.


Rules

Rule 1: Every Variable Must Be Private

This is the only mandatory rule.

class Employee {

    private String employeeName;

    private double salary;

}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

Two variables are declared.

Step 2

Both variables are private.

Step 3

No variable is visible outside the class.

Step 4

The class is tightly encapsulated.


Rule 2: Getter and Setter Methods Are Not Checked

class Student {

    private String name;

}
Enter fullscreen mode Exit fullscreen mode

Even though this class has no getter or setter, it is still tightly encapsulated.

Step-by-Step Explanation

Step 1

The variable is private.

Step 2

Java ignores the absence of accessor methods.

Step 3

Only variable visibility is checked.

Step 4

The class passes the test.


Rule 3: One Non-Private Variable Breaks the Rule

class Product {

    private String productName;

    public double price;

}
Enter fullscreen mode Exit fullscreen mode

This class is not tightly encapsulated.

Step-by-Step Explanation

Step 1

productName is private.

Step 2

price is public.

Step 3

One variable violates the rule.

Step 4

The entire class fails.


Practical Examples

Beginner Example

class Customer {

    private String customerName;

}
Enter fullscreen mode Exit fullscreen mode

✅ Tightly encapsulated.


Business Example

class Order {

    private long orderId;

    private double totalAmount;

    private String status;

}
Enter fullscreen mode Exit fullscreen mode

✅ Tightly encapsulated.


Interview Example

class Payment {

    private String transactionId;

    protected String paymentMode;

}
Enter fullscreen mode Exit fullscreen mode

❌ Not tightly encapsulated.

Why?

paymentMode is protected, not private.


Inheritance Rule (Most Important)

This is one of the most frequently asked interview questions.

If the parent class is not tightly encapsulated, then no child class can be tightly encapsulated.

Let's understand why.

Example

class A {

    int x = 10;

}

class B extends A {

    private int y = 20;

}

class C extends B {

    private int z = 30;

}
Enter fullscreen mode Exit fullscreen mode

Which Classes Are Tightly Encapsulated?

Class Tightly Encapsulated? Reason
A ❌ No x is package-private (default access), not private
B ❌ No Inherits non-private x from A
C ❌ No Also inherits non-private x through B

Although B and C declare their own variables as private, they still inherit a non-private variable.

Therefore, neither class is tightly encapsulated.


Visual Explanation

          A

      int x

      ❌ Not Private

          │

          ▼

          B

 private int y

 inherits x

      ❌ Loose

          │

          ▼

          C

 private int z

 inherits x

      ❌ Loose
Enter fullscreen mode Exit fullscreen mode

The non-private variable spreads through inheritance.

Think of it as a broken chain.


Another Practice Example

class P {

    private int a;

}

class Q extends P {

    private int b;

    public int c;

}

class R extends Q {

    private int d;

}
Enter fullscreen mode Exit fullscreen mode

Which Classes Are Tightly Encapsulated?

Class Tightly Encapsulated? Reason
P ✅ Yes All variables are private
Q ❌ No c is public
R ❌ No Parent Q is not tightly encapsulated

Visual Explanation

        P

 private a

      ✅

      │

      ▼

        Q

 private b

 public c

      ❌

      │

      ▼

        R

 private d

inherits public c

      ❌
Enter fullscreen mode Exit fullscreen mode

Once a parent violates the rule, every child automatically fails.


Encapsulation vs Tightly Encapsulated Class

This comparison is another favorite interview topic.

Feature Encapsulated Class Tightly Encapsulated Class
Purpose Design principle Rule-based definition
Data hiding Yes Yes
Abstraction Yes Not checked
Getter/Setter methods Usually expected Not required
Rule Private data + controlled access Every variable must be private

Important Observation

A class can be:

  • Encapsulated but not tightly encapsulated
  • Tightly encapsulated but not follow JavaBean conventions
  • Neither

The two concepts are related but not identical.


Common Beginner Mistakes

Mistake 1: Thinking Getters and Setters Are Mandatory

❌ Incorrect

"A tightly encapsulated class must have getters and setters."

Correct:

Only variable visibility is checked.


Mistake 2: Forgetting Inherited Variables

Many developers inspect only the child class.

Always inspect inherited variables too.


Mistake 3: Forgetting Default Access

class Demo {

    int value;

}
Enter fullscreen mode Exit fullscreen mode

This is not private.

Default access is package-private, so the class is not tightly encapsulated.


Mistake 4: Assuming protected Is Good Enough

protected int age;
Enter fullscreen mode Exit fullscreen mode

Still not tightly encapsulated.

Only private satisfies the rule.


Interview Questions

1. What is a Tightly Encapsulated Class?

Why interviewers ask this

To test whether you know the exact Java definition.

Expected answer

A class is tightly encapsulated if every variable in the class is declared private.

Common trap

Mentioning getters and setters.


2. Are Getter and Setter Methods Mandatory?

Expected answer

No.

Java checks only whether every variable is private.


3. Can a Class Without Getters and Setters Be Tightly Encapsulated?

Expected answer

Yes.

If every variable is private, the class is tightly encapsulated.


4. Can a Child Class Be Tightly Encapsulated If Its Parent Is Not?

Expected answer

No.

A child inherits the parent's non-private variables, so it cannot be tightly encapsulated.


5. Does a protected Variable Qualify?

Expected answer

No.

Only private variables satisfy the definition.


Best Practices

  • Declare all instance variables as private.
  • Expose only the operations your class needs.
  • Don't add setters unless modification is genuinely required.
  • Keep business rules inside the class.
  • When designing inheritance hierarchies, ensure parent classes also protect their state appropriately.
  • Don't confuse JavaBean conventions with the definition of a tightly encapsulated class.

Performance Notes

Declaring variables as private has no meaningful runtime performance cost. Whether a class is tightly encapsulated is a design and access-control concept, not a performance optimization.


Quick Memory Trick 🧠

Remember the word:

TIGHT

↓

T = Total

I = Instance

G = Variables

H = Hidden

T = Totally Private
Enter fullscreen mode Exit fullscreen mode

If every variable is private, the class is TIGHT.


FAQ

Is a tightly encapsulated class the same as an encapsulated class?

No. A tightly encapsulated class follows one strict rule: every variable must be private. An encapsulated class is a broader design concept involving data hiding and controlled access.

Are constructors checked?

No.

Constructors are not part of the definition.

Are methods checked?

No.

Only variables are checked.

What if one variable is protected?

The class is not tightly encapsulated.

Does inheritance affect tight encapsulation?

Yes. If a parent class is not tightly encapsulated, none of its subclasses can be tightly encapsulated.


Key Takeaways

  • A tightly encapsulated class has one defining rule: every variable must be private.
  • Getter methods and setter methods are not part of the definition.
  • Constructors and method access modifiers are not checked.
  • A single non-private variable causes the class to fail the test.
  • If a parent class is not tightly encapsulated, none of its child classes can be tightly encapsulated.
  • Don't confuse Encapsulation with Tightly Encapsulated Class—they are related but different concepts.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)