Structure of Classes and Objects in Java: Attributes and Methods
The Java programming language is heavily based on the concept of objects, which makes it one of the most popular languages for object-oriented programming (OOP). OOP is a programming paradigm that uses "objects" – which can contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods – for designing computer applications and programs. In this chapter, we will explore the structure of classes and objects in Java, focusing on its two main components: attributes and methods.
11.3.1. The Concept of Class in Java
A class in Java is a kind of "blueprint" or "template" for creating objects. It defines the state and behavior that objects of the class type will have. The state is represented by attributes and the behavior is represented by methods. A class is defined using the class
keyword, followed by the class name and a pair of braces that encapsulate its members.
public class Car {
// Attributes
String tag;
String template;
int year;
double price;
// Methods
void accelerate() {
// code to speed up the car
}
void brake() {
// code to brake the car
}
}
11.3.2. Attributes of a Class
Attributes, also known as fields or instance variables, are the variables that belong to a class. They are used to store information about the state of an object. Each object created from the class will have its own copy of these attributes. Attributes can be of any data type, such as integers, booleans, strings or even references to other classes.
Attributes can have multiple access modifiers, such as public
, private
, protected
, or none (package private). The choice of access modifier is crucial for encapsulation, which is a fundamental principle of OOP. Encapsulation means keeping the implementation details of a class hidden and exposing only the methods and attributes necessary to use the class.
Private attributes (private
) are accessible only within the class itself and are hidden from other classes. To allow access to these attributes, public methods, known as getters and setters, are often provided.
11.3.3. Methods of a Class
Methods are functions defined within a class that operate on data (attributes) and are used to express the behavior of objects. A method can have parameters, which are data passed to it, and can return a value. In Java, each method must specify the data type of the value it returns. If a method does not return any value, its return type is void
.
Just like attributes, methods can also have access modifiers, which determine where they can be called. public
methods can be called by any object of any class. private
methods can only be called from within the class itself. protected
methods can be called within the class itself and by subclasses.
Methods can be overloaded (overloaded) or overridden (overridden). Method overloading occurs when two or more methods in the same class have the same name but different parameter lists. Method overriding is when a subclass provides a specific implementation for a method that is already provided by one of its parent classes.
11.3.4. Builders
A special aspect of the structure of a class are constructors. Constructors are special methods that are called when an object is instantiated (created). They have the same class name and do not have a return type, not even void
. Constructors are used to initialize an object's attributes with specific values.
public class Car {
String brand;
String template;
int year;
double price;
// Constructor
public Carro(String brand, String model, int year, double price) {
this.brand = brand;
this.model = model;
this.year = year;
this.price = price;
}
}
11.3.5. 'this' keyword
In Java, the this
keyword is used inside methods and constructors to reference the current object. It is mainly useful when we have method parameters or constructors with the same name as the class attributes, to differentiate them.
11.3.6. Encapsulation and Abstraction
Encapsulation is one of the main features of object-oriented programming. It allows you to hide the internal implementation of a class and expose only what is needed to the outside world. This is achieved by using access modifiers to restrict access to class attributes and methods. Abstraction is a related concept that refers to the practice of reducing complexity by exposing only the details necessary for an object's use, while hiding the internal implementation.
11.3.7. Practical Example
Let's consider a practical example of a ContaBancaria
class with its attributes and encapsulated methods:
public class ContaBancaria {
private String holder;
private double balance;
public Bank Account(String holder, double initial balance) {
this.holder = holder;
this.saldo = initialbalance;
}
public double getBalance() {
return balance;
}
public void deposit(double value) {
if (value > 0) {
balance += value;
}
}
public boolean withdraw(double value) {
if (value > 0 && balance >= value) {
balance -= value;
return true;
}
return false;
}
}
In this example, the holder
and balance
attributes are private, which means they cannot be accessed directly from outside the class. Instead, the getSaldo
, deposit
, and cash
methods are provided to interact with these attributes in a controlled manner.
Conclusion
Understanding the structure of classes and objects in Java is fundamental to developing object-oriented software. Attributes define the state of objects, while methods define their behavior. Proper use of access modifiers and practicing encapsulation and abstraction are essential for creating robust and maintainable programs. With this knowledge, you are well equipped to begin designing and implementing your own classes in Java.