11. Structure of Classes and Objects in Java
Java is an object-oriented programming language, which means that it uses objects to represent data and methods to operate on that data. The structure of classes and objects is fundamental to understanding how Java works. Let's explore this structure step by step.
Classes in Java
A class is a model or blueprint for objects. It defines a data type, specifying the data that an object can contain and the methods that can operate on that data. Classes in Java are defined using the class
keyword.
public class Car {
// Class attributes
private String tag;
private String model;
private int ano;
// Class constructor
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
// Class methods
public void accelerate() {
System.out.println("The car is accelerating.");
}
// Getters and Setters
public String getMarca() {
return mark;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
In this example, the Car
class has three attributes (make, model, and year), a constructor to initialize these attributes, and a accelerate
method to perform an action. Additionally, we have getters and setters methods that are used to access and modify the values of private attributes.
Objects in Java
An object is an instance of a class. When we create an object, we are creating an entity that has the attributes and behaviors defined by the class. To create an object in Java, we use the new
keyword and call the class constructor.
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Toyota", "Corolla", 2020);
// Calling an object method
myCar.accelerate();
}
}
Here, myCar
is an object of class Car
, and we can access its methods and attributes using dot notation.
Encapsulation
Encapsulation is one of the fundamental principles of object-oriented programming. It refers to the practice of hiding the internal details of a class and exposing only what is necessary through a public interface. This is done using access modifiers such as private
, public
and protected
.
Attributes of a class are often marked private
to prevent direct access from outside the class, and public
methods are provided to access and modify these values in a controlled.
Inheritance
Inheritance is a mechanism by which a class can inherit attributes and methods from another class. The class it inherits from is called a subclass or derived class, and the class from which it inherits is called a superclass or base class. In Java, this is done using the extends
keyword.
public class Vehicle {
// Attributes and methods common to all vehicles
}
public class Car extends Vehicle {
// Attributes and methods specific to a car
}
In this example, Car
inherits from Vehicle
, which means that a Car
object will have all the attributes and methods of a Vehicle
, in addition to your own.
Polymorphism
Polymorphism is the ability of a method to have multiple forms. In Java, this can be achieved through method overloading (methods with the same name but different parameter lists) and method overriding (when a subclass redefines a superclass method).
public class Vehicle {
public void move() {
System.out.println("The vehicle is moving.");
}
}
public class Car extends Vehicle {
@Override
public void move() {
System.out.println("The car is moving.");
}
}
Here, the move
method is overwritten in the Car
class to provide a car-specific implementation.
Interfaces
An interface in Java is a kind of contract that defines a set of abstract methods that a class must implement. Interfaces are defined using the interface
keyword, and classes implement them using the implements
keyword.
public interface Motorized {
void callMotor();
}
public class Car implements Motorized {
@Override
public void callMotor() {
System.out.println("Engine on.");
}
}
In this example, the Car
class implements the Motorized
interface, committing toProvide an implementation for the ligarMotor
method.
Conclusion
The structure of classes and objects in Java is essential for developing robust and scalable applications. Through the use of classes, objects, encapsulation, inheritance, polymorphism and interfaces, we can create code that is well organized, reusable and easy to maintain. By mastering these concepts, you will be well equipped to face the challenges of programming in Java.