Classes are a fundamental concept in the Java programming language. They are used to define objects and their properties and behaviors. In this post, we will learn what classes are and how to create and use them in Java.
First, let’s define what a class is. A class is a blueprint or template for creating objects. It defines the properties (also known as instance variables or fields) and behaviors (also known as methods) that the objects created from the class will have.
For example, let’s say we want to create a class for a car. This class might include properties such as the car’s make, model, and year, as well as behaviors like starting the engine and honking the horn. The class serves as a blueprint for creating individual car objects, each with their own unique set of properties and behaviors.
Here is an example of a simple class in Java:
public class Car {
// properties (fields)
private String make;
private String model;
private int year;
// behaviors (methods)
public void startEngine() {
System.out.println("Engine started");
}
public void honkHorn() {
System.out.println("Beep beep!");
}
}
In this example, the Car
class has three properties: make
, model
, and year
. It also has two methods: startEngine
and honkHorn
. The private
keyword before the properties indicates that they can only be accessed within the Car
class itself.
To create an object from this class, we can use the new
keyword followed by the class name and constructor. A constructor is a special method that is used to create and initialize an object. In Java, every class has a constructor, even if it is not explicitly defined. Here is an example of how to create a new Car
object:
Car myCar = new Car();
Now that we have created a Car
object, we can access its properties and behaviors using dot notation. For example, we can set the make
and model
properties like this:
myCar.make = "Ford";
myCar.model = "Mustang";
And we can call the object’s methods like this:
myCar.startEngine();
myCar.honkHorn();
Classes can also have constructors with parameters, which allow us to specify the values of the object’s properties when it is created. Here is an example of a Car
class with a constructor that takes three parameters:
public class Car {
// properties
private String make;
private String model;
private int year;
// constructor with parameters
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// behaviors
public void startEngine() {
System.out.println("Engine started");
}
public void honkHorn() {
System.out.println("Beep beep!");
}
}
To create a new Car
object with this constructor, we would pass in three arguments when calling the new
keyword, like this:
Car myCar = new Car("Ford", "Mustang", 2021);
keyword, like this:
Copy codeCar myCar = new Car("Ford", "Mustang", 2021);
In this example, the values “Ford”, “Mustang”, and 2021 are passed as arguments to the constructor, and are used to initialize the make
, model
, and year
properties of the Car
object.
Another important concept in Java classes is inheritance. Inheritance allows one class to inherit the properties and behaviors of another class. This is useful because it allows us to create a base class with common properties and behaviors that can be shared among multiple subclasses.
For example, let’s say we have a base class called Vehicle
with common properties such as make
and model
, and behaviors such as startEngine
and stopEngine
. We can then create a subclass called Car
that inherits from Vehicle
, and adds additional properties and behaviors specific to cars. Here is an example of how to do this in Java:
public class Vehicle {
// properties
private String make;
private String model;
// behaviors
public void startEngine() {
System.out.println("Engine started");
}
public void stopEngine() {
System.out.println("Engine stopped");
}
}
public class Car extends Vehicle {
// additional property
private int year;
// additional behavior
public void honkHorn() {
System.out.println("Beep beep!");
}
}
In this example, the Car
class extends the Vehicle
class, which means it inherits all of the properties and behaviors of Vehicle
. The Car
class can then add additional properties and behaviors of its own.
To create a new Car
object, we can use the new
keyword just like before, but we must now specify that it is a Car
object, not a Vehicle
object:
Car myCar = new Car();
We can then access the inherited properties and behaviors of the Vehicle
class using dot notation, as well as the additional properties and behaviors of the Car
class:
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 2021;
myCar.startEngine();
myCar.stopEngine();
myCar.honkHorn();
In summary, classes are a powerful tool in Java for defining objects and their properties and behaviors. They can be used to create objects with unique sets of data and functionality, and they can be extended through inheritance to share common properties and behaviors among multiple subclasses.