How to make a write-only class in Java?

Here, we are going to learn that can we make write-only class in Java? If yes, then how to make write-only class in Java? By Preeti Jain Last updated : March 23, 2024

The question is that "can we make a write-only class in Java?"

The answer is: "Yes, we can make write-only in Java."

Defining a write-only class in Java

Now, we will see in few steps, how to make write-only class and the various steps in given below,

We can make a class write-only by making all of the data members private.

Please note:

  • If we made a class write-only then we can modify the properties or data member value of the class.
  • If we made a class write-only then we can only write the properties or data members value of the class.
  • The write-only class will contain setter methods which write the value of the private properties because there is a setter method available in the class.
  • The write-only class can contain getter method if we want to read the value of the private properties after writing.

Now, we will see the objective of the setter method, why it is required?

Few points need to remember about setter methods are given below:

  • As we know that "private" data member of the class is accessible in the same class only.
  • Let suppose we want to access "private" data member of the class in outside class. So, in that case, we need to declare public "setter" methods.
  • The objective of the set method is used to update or set the private variable values.

Syntax to make a write-only class in Java

public void setDataMember_Name(Type var_name);

In the setter method, it is not mandatory the same data member name after set, but it is convenient for our understanding that we should consider the same name as the data member after set.

There are few advantages of Setter Methods are given below:

  • Hiding the internal representation of the private data member.
  • Setter methods provide access level hierarchy.
  • This method adds additional functionality easily later.
  • This class allows getter methods to be protected properties from unexpected changes by outside class.
  • The private data member is updateable from outside the class with the help of setter methods.

Example to make a write-only class in Java

// Java program to demonstrate the example of 
// making write-only class in Java

public class WriteWeeks {
    // Private Data Member Declaration
    private String days;

    // Defining Setter method to write the value of
    // private properties and this method takes an argument 
    // and assign it in the private member.
    public void setDays(String days) {
        this.days = days;
    }

    // Defining Getter method to retrive the value of 
    //private variable

    public String getDays() {
        return days;
    }

    public static void main(String[] args) {
        // Weeks object instanstiation
        WriteWeeks w = new WriteWeeks();

        // Set the value of the private member
        w.setDays("7 Days");

        String result = w.getDays();
        // Display the value of the private properties
        System.out.println("Days in a Week :" + " " + result);

    }
}

Output

Days in a Week : 7 Days


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.