Home » Java programming language

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?
Submitted by Preeti Jain, on September 17, 2019

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:

    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:

// 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.