Understanding Class and Its Components in Java
A class is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that an object of that class will have. Think of a blueprint for a house. The blueprint defines the layout, number of rooms, and overall structure of the house. When you build houses based on this blueprint, each house will have the same layout and structure. In programming, a class is like a blueprint for objects. It defines the properties and behaviours that objects of that class will have.
Example: The Car
Class
Contents
Let’s create a class called Car
to represent a car. A car has properties like its color, make, and model, and behaviors like starting, stopping, and accelerating.
Class Car
Java
public class Car {
private String color;
private String make;
private String model;
public Car(String color, String make, String model) {
this.color = color;
this.make = make;
this.model = model;
}
public void start() {
System.out.println("Car started.");
}
public void stop() {
System.out.println("Car stopped.");
}
public void accelerate() {
System.out.println("Car accelerating.");
}
}
Main Class
public class Main {
public static void main(String[] args) {
// Create a car object
Car myCar = new Car("red", "Toyota", "Corolla"); // Call methods on the car object
myCar.start();
myCar.accelerate();
myCar.stop();
}}
Class Variables
Class variables, also known as static variables in Java, are attributes that belong to the class itself, rather than to individual objects of that class. They are shared by all instances of the class.
In our Car
class, we can define a class variable to represent the default number of wheels a car has:
private String color;
private String make;
private String model;
Now, every Car
object will have the above-mentioned attributes.
Class Functions (Methods)
Class functions, also known as methods in Java, are the behaviors or actions that an object of the class can perform. They define what the object can do.
In our Car
class, we have defined three methods: start
, stop
, and accelerate
. These methods represent actions that a car can perform.
Creating Objects (Instances) of a Class
To use a class, we create objects, also known as instances, of that class. Each object is a unique instance of the class and has its own set of values for the class’s attributes.
Car myCar = new Car("red", "Toyota", "Corolla");
Car yourCar = new Car("blue", "Honda", "Civic");
Accessing Class Variables and Methods
Once we have created objects, we can access their attributes and call their methods:
System.out.println(myCar.getColor()); // Output: red
myCar.start(); // Output: Car started.
System.out.println(Car.defaultWheels); // Output: 4
Key Points to Remember
- A class is a blueprint for creating objects.
- Class variables (static variables in Java) are shared by all instances of a class.
- Class functions (methods) define the behaviours of objects.
- Objects are instances of a class.
- You can access class variables and methods using dot notation.