TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java

Encapsulation in Java with Example

Overview

In this tutorial, we will learn Encapsulation in Java with an example that we are familiar with. In this example, we will take the Product class.

The product class has three private instance variables:

name, price, quantity.

Let’s take the Product class in this example:

https://www.testingdocs.com/accessor-and-mutator-methods-in-java/

What is Encapsulation?

Encapsulation is hiding the details of the class and binding the implementation with public methods is known as Encapsulation. Java developer allows access of the class to the outside world only through public methods. The public methods of the class are the public interface of the class to the outside world. The private instance methods are hidden and are only manipulated using Setters and Getters methods. ( Mutator and Accessor methods)

 

Encapsulation in Java

 

Now assume that we allow a new instance variable of the Product class called rating the product and we allow the rating of the product to be displayed on the website. However, the rating of the product should be an integer within the range[0,5] inclusive i.e 0 <= rating <= 5.

 

private String name;// name of the product 
private double price;// price of the product 
private int quantity;// quantity in the store
private int rating;// rating of the product [0,5] inclusive

Binding variables

Implementing Encapsulation and binding the variables to the public accessor and mutator methods.

Instance variables are declared private, and hence not visible to outside classes.

For each instance variable in the Product class, we can provide a public Setter (Mutator) method and Getter(Accessor) method.

Instance variable protection

Do not implement or remove the Setter method if we want the variable to be Read-only.
Protect and bind the variables in the Setter method.
In the below example, we protect the rating to be in the valid range [0,5] inclusive using the mutator method.

With out encapsulation instance variables can be easily corrupted by other classes. For example, rating instance variable valid values are [0,5] and any value is invalid. So we allow rating to be public, any class can corrupt the value:

 

private instance variable ProtectionSetter(Mutator) Method Getter(Accessor) Method
No AccessNO SetterNO Getter method
Read-Only AccessNO SetterImplement Getter method
Write-Only AccessImplement Setter methodNO Getter method
Read/Write AccessImplement Setter methodImplement Getter method

 

Sample Code

 

//www.TestingDocs.com
public class Product { 
 private String name;// name of the product 
 private double price;// price of the product 
 private int quantity;// quantity in the store
 private int rating;// rating of the product [0,5] inclusive

 //Accessor and Mutator for Product rating
 /**
 * @return the rating
 */
 public int getRating() {
 return rating;
 }
 /**
 * @param rating the rating to set
 * www.TestingDocs.com
 */
 public void setRating(int rating) {
 if(rating >= 0 && rating <= 5)
 this.rating = rating;
 else
 System.out.println("Error: Invalid Rating");
 }
}

-instance variable protection

Related Posts

Download Greenfoot Windows

Java /

Download & Install Greenfoot on Windows

Java /

Java Static Code Analysis

Java /

Java Testing Tools

Java /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java /

Exceptions in Java Programs

‹ Download and Install BlueJ IDE› Java Control Panel on Windows

Recent Posts

  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Print Triangle Pattern Flowchart
  • RAPTOR Editions
  • Flowgorithm Conditional Breakpoint Statement
  • Flowgorithm Read Numbers from File Example
  • Search Text File Flowchart Example
  • Flowgorithm Turtle Graphics Symbols
  • Draw Circle using Flowgorithm Turtle

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version