Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications. It emphasizes encapsulation, inheritance, and polymorphism, making code more modular, reusable, and easier to maintain. Understanding classes and objects is the foundation of OOP in Java.
In this tutorial, we'll explore the core concepts of Object-Oriented Programming (OOP) in Java, focusing on classes and objects. We'll see how these building blocks help us create more organized and efficient code. By the end of this section, you'll be able to define your own classes, create objects, and understand their relationships.
Think of a class as a template for making cookies. The recipe (class) describes how to make the cookie, including its ingredients and shape. Each cookie made from this recipe (object) has the same basic structure but can have unique characteristics like different flavors or decorations.
Let's start by creating a simple class in Java. We'll define a Car class with some attributes and methods.
1public class Car {2// Attributes3String make;4String model;5int year;67// Constructor8public Car(String make, String model, int year) {9this.make = make;10this.model = model;11this.year = year;12};1314// Method to display car details15public void displayInfo() {16System.out.println("Car: " + make + " " + model + ", Year: " + year);17}18}
Now that we have a Car class, we can create objects based on this class.
1public class Main {2public static void main(String[] args) {3// Create Car objects4Car myCar = new Car("Toyota", "Corolla", 2020);5Car friendCar = new Car("Honda", "Civic", 2019);67// Display car information8myCar.displayInfo();9friendCar.displayInfo();10}11}
Car: Toyota Corolla, Year: 2020 Car: Honda Civic, Year: 2019
A constructor is a special method that is called when an object is created. It initializes the object's attributes.
In the Car class example, we defined a constructor that takes three parameters (make, model, and year) to initialize these attributes when a new Car object is created.
Access modifiers control the visibility of classes, methods, and variables. The most common access modifiers in Java are:
1public class Car {2// Private attributes3private String make;4private String model;5private int year;67// Constructor8public Car(String make, String model, int year) {9this.make = make;10this.model = model;11this.year = year;12}1314// Public method to display car details15public void displayInfo() {16System.out.println("Car: " + make + " " + model + ", Year: " + year);17}18}
Encapsulation is the practice of hiding the internal state of an object and requiring all interaction to be performed through an object's methods. This is achieved using private access modifiers for attributes and public getter/setter methods.
1public class Car {2// Private attributes3private String make;4private String model;5private int year;67// Constructor8public Car(String make, String model, int year) {9this.make = make;10this.model = model;11this.year = year;12}1314// Getter and Setter for make15public String getMake() {16return make;17}1819public void setMake(String make) {20this.make = make;21}2223// Getter and Setter for model24public String getModel() {25return model;26}2728public void setModel(String model) {29this.model = model;30}3132// Getter and Setter for year33public int getYear() {34return year;35}3637public void setYear(int year) {38this.year = year;39}4041// Public method to display car details42public void displayInfo() {43System.out.println("Car: " + make + " " + model + ", Year: " + year);44}45}
Let's create a more comprehensive example that includes encapsulation and additional methods.
1public class Car {2// Private attributes3private String make;4private String model;5private int year;67// Constructor8public Car(String make, String model, int year) {9this.make = make;10this.model = model;11this.year = year;12}1314// Getter and Setter for make15public String getMake() {16return make;17}1819public void setMake(String make) {20this.make = make;21}2223// Getter and Setter for model24public String getModel() {25return model;26}2728public void setModel(String model) {29this.model = model;30}3132// Getter and Setter for year33public int getYear() {34return year;35}3637public void setYear(int year) {38this.year = year;39}4041// Public method to display car details42public void displayInfo() {43System.out.println("Car: " + make + " " + model + ", Year: " + year);44}4546// Method to start the car47public void start() {48System.out.println(make + " " + model + " is starting.");49}5051// Method to stop the car52public void stop() {53System.out.println(make + " " + model + " is stopping.");54}55}
1public class Main {2public static void main(String[] args) {3// Create Car objects4Car myCar = new Car("Toyota", "Corolla", 2020);5Car friendCar = new Car("Honda", "Civic", 2019);67// Display car information and perform actions8myCar.displayInfo();9myCar.start();10myCar.stop();1112friendCar.displayInfo();13friendCar.start();14friendCar.stop();15}16}
Car: Toyota Corolla, Year: 2020 Toyota Corolla is starting. Toyota Corolla is stopping. Car: Honda Civic, Year: 2019 Honda Civic is starting. Honda Civic is stopping.
| Concept | Description |
|---|---|
| Class | A blueprint for creating objects. Defines attributes and methods. |
| Object | An instance of a class. Represents real-world entities. |
| Constructor | A special method to initialize object attributes. |
| Access Modifiers | Control the visibility of classes, methods, and variables. |
| Encapsulation | Hiding internal state and requiring interaction through methods. |
In the next section, we'll delve deeper into OOP concepts such as inheritance and polymorphism. These principles will help you create more complex and flexible applications. Stay tuned!