Creating and Calling Classes in Java: A Comprehensive Guide
Java, being one of the most popular programming languages, offers a robust object-oriented programming (OOP) model. One of the fundamental concepts in Java is the creation and usage of classes. In this article, we will delve into the intricacies of creating and calling classes in Java, providing you with a comprehensive guide to master this essential skill.
Understanding Classes and Objects
Before we dive into the creation and calling of classes, it’s crucial to understand the difference between a class and an object. A class is a blueprint or a template for creating objects. An object, on the other hand, is an instance of a class. To put it simply, a class defines the properties and behaviors of an object, while an object is a specific entity that has those properties and can perform those behaviors.
Let’s take a look at a basic example:
public class Car { String brand; int year; public Car(String brand, int year) { this.brand = brand; this.year = year; } public void displayInfo() { System.out.println("Brand: " + brand + ", Year: " + year); }}
In this example, we have a class named “Car” with two properties: “brand” and “year”. The constructor initializes these properties, and the “displayInfo” method prints the car’s information.
Creating a Class
Creating a class in Java is relatively straightforward. You start by defining the class using the “class” keyword, followed by the class name. The class name should start with an uppercase letter, as per Java naming conventions. Inside the class, you can define properties (variables) and methods (functions).
Here’s how you can create the “Car” class from the previous example:
public class Car { String brand; int year; public Car(String brand, int year) { this.brand = brand; this.year = year; } public void displayInfo() { System.out.println("Brand: " + brand + ", Year: " + year); }}
Creating Objects
Once you have a class, you can create objects from it. To create an object, you use the “new” keyword followed by the class name and parentheses. If the class has a constructor, you can pass arguments to it within the parentheses.
Here’s how you can create an object of the “Car” class:
Car myCar = new Car("Toyota", 2020);
In this example, we have created an object named “myCar” with the brand “Toyota” and the year 2020.
Calling Methods
Once you have an object, you can call its methods to perform actions. To call a method, use the dot notation, which consists of the object name followed by a dot and the method name.
Here’s how you can call the “displayInfo” method on the “myCar” object:
myCar.displayInfo();
This will output:
Brand: Toyota, Year: 2020
Access Modifiers
Java provides access modifiers to control the visibility of class members (variables and methods). The four access modifiers in Java are:
Access Modifier | Description |
---|---|
public | Accessible from anywhere |
protected | Accessible within the same package and subclasses |
default | Accessible within the same package |
private | Accessible only within the class |
Here’s an example of how to use access modifiers in a class:
public class Car { public String brand; protected int year; private String model; public Car(String brand, int year) { this.brand = brand; this.year = year; } public void displayInfo() { System.out.println("Brand: " + brand + ", Year: " + year