CodesJava

Easy learning with example program codes

Encapsulation in java


Dictionary meaning of Encapsulation:

The condition of being enclosed.

Capsule:

Capsule refers to a small container which contains a dose of medicine.

Definition of Encapsulation:

Encapsulation is a process of wrapping code and data into a single unit.

Encapsulation in real world:

Let us take an example of a HR in a company. We communicate through HR not directly with the departments. HR is acting as a public interface here.

Encapsulation in programming:

Encapsulation is the way of declaring the data members as private and providing access to the data members through public methods (getter and setter methods). As private field can’t be access outside the class that means data is hiding within the class. That’s why encapsulation is also known as data hiding.

Important points:

1. Main Concept behind encapsulation is ‘control over the data’. It is achieved using class and access modifier private, protected, public. Class is act as a container which contains code and data.
2. Factory pattern and Singleton pattern in Java are based on the concept encapsulation.

Example:

Car.java

/**
 * This class is used to set and get car properties.
 * @author CodesJava
 */
public class Car {
      //data members
      private int speed;
      private String color;     
 
      //getter setters of above data members.
      public int getSpeed() {
            return speed;
      }
 
      public void setSpeed(int speed) {
            this.speed = speed;
      }
 
      public String getColor() {
            return color;
      }
 
      public void setColor(String color) {
            this.color = color;
      }    
}

CarTest.java

/**
 * This class is used to interact with Car class.
 * @author CodesJava
 */
public class CarTest {
      public static void main(String args[]){
            //create Car class object
            Car car1 = new Car();          
 
            //set car details.
            car1.setColor("white");
            car1.setSpeed(120);           
 
            //get and print car details.
            System.out.println("Car color: " + car1.getColor());
            System.out.println("Car speed: " + car1.getSpeed());
      }
}

Output:

Car color: white
Car speed: 120

Download this example.

Advantages/Benefits of Encapsulation:

  1. A read-only (immutable) or write-only class can be made.
  2. Control over the data.
  3. It helps in achieving high cohesion and low coupling in the code.

 
Next Topic: Polymorphism in java.
Previous Topic: Abstraction in java.

Related Topics:

OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap