Classes and Objects

java-logo

Classes and objects are fundamental concepts in Object-Oriented Programming (OOP) which is a programming paradigm based on the concept of “objects”. An object is an instance of a class, and a class is a blueprint or template that defines the properties and behavior of objects of its type.

Creating Classes

In Java, a class is created using the class keyword followed by the name of the class. The class body contains the definitions of the data members (fields) and member functions (methods) of the class. Here’s an example of a simple class in Java:

public class Car {
    private String make;
    private String model;
    private int year;

    public void setMake(String make) {
        this.make = make;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public int getYear() {
        return year;
    }
}

In the above example, we have defined a class named Car with three private fields and six member functions. The setMake, setModel, and setYear functions are used to set the values of the private fields, while the getMake, getModel, and getYear functions are used to retrieve the values of the private fields. Private fields are only accessible within the same class.

Understanding Access Modifiers

Java provides four access modifiers for class members:

  • public: accessible from any class.
  • private: accessible only from the same class.
  • protected: accessible within the same package and subclasses.
  • default (no modifier specified): accessible within the same package.

Creating Objects

Once we have defined a class, we can create objects of that class using the new operator. Here’s an example of how to create an object of the Car class:

Car myCar = new Car();

In the above example, we have created an object named myCar of the Car class. We can create multiple objects of the same class, each with their own set of values.

Accessing Class Members

We can access the members (fields and methods) of a class using the dot (.) operator. Here’s how we can set the values of the fields of the myCar object:

myCar.setMake("Toyota");
myCar.setModel("Corolla");
myCar.setYear(2020);

And here’s how we can retrieve the values of the fields of the myCar object:

System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());

This will output:

Make: Toyota
Model: Corolla
Year: 2020

Conclusion

In summary, classes and objects are fundamental concepts in Java programming language. Classes define the properties and behavior of objects, while objects are instances of classes. We create classes using the class keyword and create objects using the new operator. We can access the members of a class using the dot (.) operator. Access modifiers define the scope of the class members.

Total
0
Shares
Previous Post
java-logo

Arrays: Explanation of arrays in Java programming language, how to create, initialize, and access them.

Next Post
java-logo

Inheritance in Java Programming Language

Related Posts