Java Language – 12 – Classes and Objects

Object-Oriented Programming (OOP) – Classes and Objects
Introduction

Object-oriented programming (OOP) is a fundamental paradigm in Java and many other programming languages. At the core of OOP are classes and objects, which provide a structured way to model and represent real-world entities and their behaviors. In this guide, we’ll explore the concepts of classes and objects in Java.

Classes in Java

A class is a blueprint for creating objects. It defines the attributes (data) and behaviors (methods) that the objects of the class will have. Classes are a way to organize and structure code in a modular and reusable manner.


public class Car {
    // Class attributes
    String make;
    String model;
    int year;

    // Class method
    void start() {
        System.out.println("The car is starting.");
    }
}

In this example, we’ve defined a ‘Car’ class with attributes ‘make,’ ‘model,’ and ‘year,’ and a method ‘start’ that represents the action of starting the car.

Objects in Java

Objects are instances of classes. They are created from class blueprints and represent specific entities with their own set of attributes and behaviors. You can create multiple objects from the same class, each with its unique data.


Car myCar = new Car(); // Create a Car object
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2022;

Car anotherCar = new Car(); // Create another Car object
anotherCar.make = "Honda";
anotherCar.model = "Accord";
anotherCar.year = 2021;

Here, we’ve created two ‘Car’ objects, ‘myCar’ and ‘anotherCar,’ with different attribute values.

Attributes and Methods

Attributes (also known as instance variables) represent the properties of objects, while methods define their behaviors. In OOP, attributes are often private or protected, and access to them is controlled through methods (getters and setters).


public class Person {
    private String name;
    private int age;

    // Constructor to initialize attributes
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter methods
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    // Setter methods
    public void setName(String name) {
        this.name = name;
    }

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

In this example, the ‘Person’ class has private attributes ‘name’ and ‘age’ with getter and setter methods to access and modify them.

Constructors

Constructors are special methods used to create and initialize objects when they are instantiated. They have the same name as the class and do not have a return type.


public class Book {
    private String title;
    private String author;

    // Parameterized constructor
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

In this ‘Book’ class, we’ve defined a parameterized constructor that takes ‘title’ and ‘author’ as arguments to initialize the object’s attributes when it’s created.

Inheritance

Inheritance is a key concept in OOP that allows you to create new classes based on existing ones. The new class inherits attributes and behaviors from the base class, promoting code reuse and flexibility.


public class Student extends Person {
    private int studentId;

    public Student(String name, int age, int studentId) {
        super(name, age);
        this.studentId = studentId;
    }
}

Here, the ‘Student’ class inherits from the ‘Person’ class, gaining access to its attributes and methods while adding its specific attribute ‘studentId.’

Conclusion

Classes and objects are the foundation of object-oriented programming in Java. They provide a structured way to model real-world entities and their behaviors, leading to more organized and maintainable code. Understanding how to create classes, define attributes, and implement methods is essential for building object-oriented Java applications.