Introduction
In this lesson, we will delve into the foundational concepts of Object-Oriented Programming (OOP) in Java, specifically focusing on classes and objects. Understanding these fundamental building blocks is crucial for creating robust and maintainable Java applications. We will define classes and objects, differentiate between instance variables and methods, demonstrate object instantiation, explore encapsulation using access modifiers, and understand constructor functionality in object creation.
What are Classes and Objects?
A class in Java serves as a blueprint for creating objects. It encapsulates data (attributes or instance variables) and behavior (methods) related to a specific concept or entity. An object is an instance of a class; it represents a specific item or concept that has attributes and behaviors derived from the class.
Defining a Class
Here’s a simple example of a class definition:
public class Car {
/* Instance variables (attributes) */
String color;
String model;
int year;
/* Method (behavior) */
void displayInfo() {
System.out.println("Car Model: " + model + ", Color: " + color + ", Year: " + year);
}
}
Creating Objects from Classes
Once a class is defined, we can create objects from it. To instantiate an object, we use the new keyword:
public class Main {
public static void main(String[] args) {
/* Instantiating a Car object */
Car myCar = new Car();
myCar.color = "Red";
myCar.model = "Toyota";
myCar.year = 2020;
/* Calling the method to display car info */
myCar.displayInfo();
}
}
Output:
Car Model: Toyota, Color: Red, Year: 2020
In this example, we defined a class Car, instantiated an object myCar from it, set its attributes, and called the method displayInfo() to output the car's details.
Instance Variables vs. Methods
Instance Variables
These are the properties of a class that represent the object’s state. In the Car class example above, color, model, and year are instance variables.
Methods
Methods define the behavior of an object. In our example, displayInfo() is a method of the Car class, demonstrating how we operate on the object’s attributes.
Encapsulation and Access Modifiers
Encapsulation is one of the four pillars of OOP, and it involves restricting access to certain components of an object. In Java, we achieve encapsulation using access modifiers:
- private: Accessible within the same class.
- public: Accessible from any other class.
- protected: Accessible within the same package and subclasses.
Example of Encapsulation
Let’s modify the Car class to encapsulate its attributes:
public class Car {
private String color;
private String model;
private int year;
/* Constructor to initialize object */
public Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
/* Method to display car information */
public void displayInfo() {
System.out.println("Car Model: " + model + ", Color: " + color + ", Year: " + year);
}
}
Creating an Object with a Constructor
In Java, a constructor is a special method that initializes new objects. In our modified Car class, we created a parameterized constructor:
public class Main {
public static void main(String[] args) {
/* Using constructor to create a Car object */
Car myCar = new Car("Blue", "Honda", 2021);
/* Displaying the car's info */
myCar.displayInfo();
}
}
Output:
Car Model: Honda, Color: Blue, Year: 2021
The constructor allows setting the object's initial state during instantiation.
Summary / Key Takeaways
In this lesson on Understanding Classes and Objects, you have learned:
- Classes and Objects serve as the core concepts of OOP in Java, acting as blueprints and instances respectively.
- Instance Variables represent the state of an object, while Methods define its behavior.
- Objects are instantiated using the
newkeyword, which calls the class constructor. - Encapsulation helps protect an object's data by restricting access through access modifiers.
- A Constructor is a specialized method used to initialize object variables upon instantiation.