11.1 Structure of Classes and Objects in Java: Class Definition
Java is an object-oriented programming language, which means it uses the concept of "objects" to model the real world. Objects are created from "classes", which are the fundamental building blocks of Java programming. To understand how to program in Java, it is essential to understand what classes are and how they work.
What is a Class?
A class in Java is a blueprint or template from which objects are created. The class defines the state and behavior that objects of the type created from it will have. An object's state is represented by "attributes" (also known as fields or instance variables), and behavior is represented by "methods" (class functions or procedures).
Classes are defined using the class
keyword followed by the class name and a brace-delimited block of code {}
that contains the definition of the attributes and methods .
public class Car {
// Attributes
String tag;
String template;
int year;
// Methods
void accelerate() {
// Code to speed up the car
}
void brake() {
// Code to brake the car
}
}
Attributes of a Class
Attributes are the variables that each object created from the class will have as part of its state. They are defined inside the class but outside of any methods. Attributes can be of any data type, including primitive types (such as int
, double
, boolean
) or object types (such as String
or objects from other classes).
One-Class Methods
Methods are the functions that define the behavior of class objects. They are used to manipulate the attributes of objects or to perform other tasks that are related to the object. Methods are defined within the class and can have parameters and a return type (or be void
if they return nothing).
Builders
In addition to attributes and methods, classes in Java can have "constructors", which are special blocks of code that are called when a new object of the class is created. Constructors have the same name as the class and can be used to initialize the object's attributes or to execute any necessary configuration code.
public class Car {
String tag;
String template;
int year;
// Constructor
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
}
Instancing Objects
To create an object from a class, we use the new
keyword followed by the class name and the required arguments to the constructor, if any.
Car myCar = new Car("Toyota", "Corolla", 2020);
When you create a new object, an instance of the class is created in memory and a reference to that instance is assigned to the variable (in this case, myCar
).
Encapsulation
Encapsulation is one of the fundamental principles of object-oriented programming. It concerns restricting direct access to class components, protecting internal data and forcing interaction with them through methods. In Java, this is done using access modifiers such as private
, protected
and public
.
When defining attributes as private
, they cannot be accessed directly from outside the class. Instead, public methods, known as "getters" and "setters", are used to get and set the attribute values.
public class Car {
private String tag;
private String model;
private int ano;
// Getter and Setter for tag
public String getMarca() {
return mark;
}
public void setBrand(String brand) {
this.brand = brand;
}
// Other getters and setters...
}
With this understanding of class definition in Java, you will be well equipped to begin modeling your own objects and creating more complex, well-structured programs.