Advertisement

header ads

Java OOP Encapsulation - A simple tutorial



In this java encapsulation tutorial, we discuss some of the basics with easily understandable examples. Encapsulation is packaging complex functionalities for ease of use. In encapsulation, rather than putting the whole code into one class with only main method, we divide code for individual smaller units (classes and methods). Also encapsulation can hide certain methods or data when needed.

Encapsulation increase the maintainability, bug fixing capability and code reuseabuility.

'getter' and 'setter' in java

  • 'getters' and 'setters' play a major role in encapsulation as well as other OOP concepts.
  • To achieve encapsulation sometimes we use private variables.
  • Then we use these 'getter' and 'setter' methods to access these variables from outside the class.
  • 'getter' - Accessor method
  • 'setter' - Modifier method
  • If a cetain variable field only has the 'getter' method, we call it as a read only field
  • If a cetain variable field only has the 'setter' method, we call it as a write only field


Consider following simple example to get a clear idea about encapsulation concept.

This is project structure
This is Car.java class

  • It has 3 private variables with getters and setters for all of them.
  • Also it contains a simple public method 'createCar' (line 32)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.encapsulation;

public class Car {
 private String carModel;
 private String color;
 private double engineCapacity;

 public String getCarModel() {
  return carModel;
 }

 public void setCarModel(String carModel) {
  this.carModel = carModel;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

 public double getEngineCapacity() {
  return engineCapacity;
 }

 public void setEngineCapacity(double engineCapacity) {
  this.engineCapacity = engineCapacity;
 }

 public void createCar() {

  System.out.println(
    "A " + getCarModel() + " With engine capacity: " + getEngineCapacity() + " and color: " + getColor());
 }

}

This is Main.java class

  • It only contains main method
  • Car class has been instanstiated in main method
  • Values for the variables has been set using setter methods


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.example.encapsulation;

public class Main {

 public static void main(String[] args) {
  Car car = new Car();
  car.setCarModel("Lamborghini Avantador");
  car.setColor("Yello");
  car.setEngineCapacity(3000);
  car.createCar();
 }
}

Above method provides following output
A Lamborghini Avantador With engine capacity: 3000.0 and color: Yello

As mentioned above, we can hide unwanted fields as well as divide the code to separate units for increase the usability and the maintainability of the programme. This is a simple example to demonstrate the behavior of encapsulation. Share this among friends and like our facebook page to stay upto date with our latest tutorials.

Post a Comment

0 Comments