Java Properties

Properties in Java

Java does not have properties like C#, but it provides a similar concept using private fields and public getter and setter methods. The idea behind using getter and setter methods in Java is similar to the concept of properties in C#. These methods allow controlled access to the private fields of a class, ensuring encapsulation and data hiding.

Why Use Getter and Setter Methods in Java?

Before diving into the implementation, let’s understand why getter and setter methods are used in Java:
1. Encapsulation: By using private fields and public getter and setter methods, you control how the data is accessed and modified. This follows the principle of encapsulation in object-oriented programming, where the internal implementation details are hidden from the outside world.
2. Controlled Access: Getter and setter methods allow you to control the accessibility of the data. For example, you can add validation in the setter method to ensure that only valid data is assigned to the field.

Example: Student Class with a Name Property in Java

In this example, we’ll have a Student class with a private field for the student’s name and public getter and setter methods to access and modify that name.

// Java program to demonstrate encapsulation using getter and setter methods

public class Student {
    // Private field
    private String name;

    // Public getter method
    public String getName() {
        return name; // Getter: retrieves the value
    }

    // Public setter method
    public void setName(String name) {
        this.name = name; // Setter: sets the value
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a new Student object
      Student student = new Student();

        // Use the setter method to set the name
        student.setName(“John”);

        // Use the getter method to get the name
        System.out.println(student.getName()); // Output: John
    }
}

Example:

    ● Private Field: The name field in the Student class is declared as private, meaning it cannot be accessed directly
       from outside the class.
    ● Getter Method: The getName method is a public method that returns the value of the name field. This method is
       equivalent to the getter in a C# property.
    ● Setter Method: The setName method is a public method that allows you to set the value of the name field. This
       method is equivalent to the setter in a C# property.
    ● Main Class: In the Main class, we create a Student object and use the setName and getName methods to modify
       and access the name field, respectively.
This approach in Java achieves the same purpose as properties in C#, providing controlled access to class fields while maintaining encapsulation.

Tasks:
1. Basic Property: Create a class Rectangle with properties length and width. Add a method to calculate and return the area of the rectangle. Create an object of Rectangle in the main method, set the properties, and print the area.
2. Read-Only Property: Create a class Circle with a read-only property radius and a method to calculate and return the circumference. Create an object of Circle in the main method, set the radius through the constructor, and print the circumference.
3. Auto-Implemented Property: Create a class Student with auto-implemented properties name, age, and grade. Create an object of Student in the main method, set the properties, and print them.
4. Property with Validation: Create a class Employee with properties name, age, and salary. Ensure that the age property can only be set if the value is between 18 and 65. Create an object of Employee in the main method, set the properties, and print them.
5. Static Property: Create a class Counter with a static property count that tracks the number of instances created. Increment the count property in the constructor. Create multiple objects of Counter in the main method and print the value of count.
6. Calculated Property: Create a class Temperature with a property celsius and a read-only property fahrenheit that returns the temperature in Fahrenheit. Create an object of Temperature in the main method, set the celsius property, and print both Celsius and Fahrenheit.
7. Private Set Property: Create a class BankAccount with properties accountNumber, balance, and accountHolder. The balance property should have a private set accessor. Add methods to deposit and withdraw money. Create an object of BankAccount in the main method, perform some transactions, and print the balance.
8. Property with Default Value: Create a class Product with properties name, price, and stock. The stock property should have a default value of 100. Create an object of Product in the main method, set the properties, and print them.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

The training covers the Java Properties class, including how to load, store, and manipulate property files with practical examples.

Yes, it includes examples like loading properties from a file, saving them, and accessing key-value pairs.

Yes, the training is beginner-friendly, with step-by-step instructions and practical examples.

A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler is sufficient.

Yes, it covers methods like loadFromXML() and storeToXML() for XML-based configurations.

Yes, the training is self-paced and accessible online anytime.

Yes, it demonstrates using default values with the getProperty() method.

It typically takes a few hours to complete, depending on your learning pace.

Yes, this Java Properties class training is completely free on Iqra Technology Academy’s website.