Getting Started with Object-Oriented Programming (OOP)

 


Java is built entirely on the Object-Oriented paradigm. Understanding Classes, Objects, and Encapsulation provides the core foundation for building robust Java applications.

Encapsulation and Getters/Setters

Encapsulation protects internal class variables by marking them private and exposing public access methods.

Java
public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        if (age > 0) {
            this.age = age;
        }
    }
}

Post a Comment

0 Comments