Encapsulation in Java Programming
Encapsulation in Java Programming
Encapsulation in Java Programming is one of the features of object-oriented programming which include encapsulation, inheritance, polymorphism, and abstraction. Encapsulation is binding together data members and member functions in a single unit called class such that those functions act on those data members. Its motivation is to prevent direct access of the data members and is allowed access only through the methods.
Data Encapsulation in Java Programming Steps:
-
-
Make data members private.
-
make setter methods to set their values.
-
make getter methods to get their values.
-
Encapsulation Example in Java:
Cars.java
This file consists of a class representing a car and has 4 data members and their respective setter and getter methods. Setter methods are used to set values of an object’s data members to a certain value while getter methods are used to retrieve those methods while all the data members are private thus maintaining the essence of encapsulation.
public class Cars { //Data members and member functions of class Cars private String model; private int year; private String color; private double price; void setModel(String model){ this.model=model; } void setYear(int year){ this.year=year; } void setColor(String color){ this.color=color; } void setPrice(double price){ this.price=price; } String getModel(){ return this.model; } int getYear(){ return this.year; } String getColor(){ return this.color; } double getPrice(){ return this.price; } void drive(){ System.out.println("You drive the car."); } void brake(){ System.out.println("You step on the brakes."); } }
Main.java
This file consists of a class inside which an object of Cars class is created and its setter and getter methods are called. Its data members cannot be directly accessed by the created object as they are private and can be accessed inside member functions and the class itself and nowhere else.
//Creates objects of another class called Cars and works on its data members and member functions //Java Program with Encapsulation public class Main { public static void main(String[] args){ Cars myCar=new Cars(); //Calling setter methods myCar.setModel("Corvette"); myCar.setYear(2020); myCar.setColor("Blue"); myCar.setPrice(50000.00); //Calling getter methods System.out.println(myCar.getModel()); System.out.println(myCar.getYear()); System.out.println(myCar.getColor()); System.out.println(myCar.getPrice()); } }
Output:
Corvette 2020 Blue 50000.0
Advantages of Encapsulation in Java Programming
Data Hiding:
Encapsulation leads to data hiding as users will have no idea about the hows of the methods and only about the whats. The user will not know how the methods of the class have been implemented but will surely know about the things occurring.
Increased Flexibility:
The programmer can further restrict access to the data members by omitting the setter methods and only retaining the getter methods thus making the data members read-only.
Reusability:
It provides and improves the reusability of code and original methods can be changed and new methods can be added easily without disrupting the program flow.