Fundamentals 7 min read

Understanding Encapsulation and Interfaces in Java

This article explains Java encapsulation and interfaces, illustrating how access modifiers like public and private control object members, improve usability and safety, and provides a complete code example with a Human class, usage guidelines, and a practical exercise.

Java Captain
Java Captain
Java Captain
Understanding Encapsulation and Interfaces in Java

Objects represent concrete entities while classes define their types; an object contains state (data members) and behavior (methods). Inside an object, this accesses its members, and outside you use object.field or object.method() .

Encapsulation and Interfaces

Encapsulation hides implementation details and exposes only a limited external interface, similar to how a flashlight hides its internal circuitry and offers only a switch and a charging port for user interaction, thereby improving usability and safety.

In Java, encapsulation is achieved through access modifiers:

public : member is visible externally and forms part of the class's interface.

private : member is hidden from external code and can only be used internally.

protected : (related to inheritance, discussed later).

The following code demonstrates these concepts:

public class Test {
    public static void main(String[] args) {
        Human aPerson = new Human(160);
        System.out.println(aPerson.getHeight());
        aPerson.growHeight(170);
        System.out.println(aPerson.getHeight());
        aPerson.repeatBreath(100);
    }
}

class Human {
    /** constructor */
    public Human(int h) {
        this.height = h;
        System.out.println("I'm born");
    }
    /** accessor */
    public int getHeight() {
        return this.height;
    }
    /** mutator */
    public void growHeight(int h) {
        this.height = this.height + h;
    }
    /** encapsulated, for internal use */
    private void breath() {
        System.out.println("hu...hu...");
    }
    /** call breath() */
    public void repeatBreath(int rep) {
        int i;
        for(i = 0; i < rep; i++) {
            this.breath();
        }
    }
    private int height; // encapsulated, for internal use
}

Only the public methods getHeight() , growHeight() and repeatBreath() are accessible from outside the Human class; the private field height and method breath() remain hidden.

If an external class tries to access aPerson.height , the compiler reports an error such as:

Test.java:6: height has private access in Human
System.out.println(aPerson.height);
               ^
1 error

This demonstrates why data members should generally be declared private and accessed through public getters/setters, protecting the internal state and allowing the class designer to enforce invariants.

Class Visibility

In a single .java file, only one class may be declared public . Other classes without the keyword have package‑level visibility, a topic covered in the package discussion.

Exercise

Encapsulate a Torch class that represents a flashlight. The public interface should include methods to turn the torch on/off and to charge it, while the internal state (e.g., battery level) remains private.

Summary

Encapsulation uses private and public modifiers to hide internal details and expose a clean interface, enhancing both usability and safety of Java classes.

Encapsulation and interfaces

private, public access modifiers

Original source: cnblogs.com/vamei/archive/2013/03/27/2982209.html

Javaprogramming fundamentalsoopEncapsulationAccess Modifiers
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.