Day 1: Introduction to Java

icon picker
Day 7: Object-Oriented Programming (OOP) Basics

Welcome to Day 7 of our Java programming journey! Today, we'll dive deep into the world of Object-Oriented Programming (OOP) fundamentals. OOP is a paradigm that allows us to model real-world entities in our code as objects with attributes and behaviors. Understanding OOP is essential for writing efficient, organized, and maintainable Java code.

Classes and Objects

In Java, a class is a blueprint for creating objects. It defines the structure and behavior of objects that belong to it. An object, on the other hand, is an instance of a class. It represents a real-world entity or concept.
Here's a simple example of a Java class:
javaCopy code
public class Car {
// Attributes
String brand;
int year;
// Methods
void start() {
System.out.println("The " + brand + " is starting.");
}
void drive() {
System.out.println("Driving the " + brand + ".");
}
}

In this example, we have a Car class with attributes (brand and year) and methods (start and drive). To create an object of this class:
javaCopy code
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year = 2022;

myCar.start();
myCar.drive();

Constructors

A constructor is a special method in a class that is called when an object is created. It is used to initialize the object's attributes or perform any other setup. Constructors have the same name as the class and do not have a return type.
Here's an example of a constructor for our Car class:
javaCopy code
public class Car {
String brand;
int year;
// Constructor
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
}
// Methods
void start() {
System.out.println("The " + brand + " is starting.");
}
void drive() {
System.out.println("Driving the " + brand + ".");
}
}

Now, you can create a Car object with the constructor:
javaCopy code
Car myCar = new Car("Toyota", 2022);
myCar.start();
myCar.drive();

Encapsulation

Encapsulation is one of the key principles of OOP. It involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit called a class. It also includes controlling access to the data by using access modifiers like public, private, and protected.
In our Car class, we can use encapsulation to make the brand and year attributes private and provide getter and setter methods to access and modify them safely.
javaCopy code
public class Car {
private String brand;
private int year;
// Constructor
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
}
// Getter for brand
public String getBrand() {
return brand;
}
// Setter for brand
public void setBrand(String brand) {
this.brand = brand;
}
// Getter for year
public int getYear() {
return year;
}
// Setter for year
public void setYear(int year) {
this.year = year;
}
// Methods
void start() {
System.out.println("The " + brand + " is starting.");
}
void drive() {
System.out.println("Driving the " + brand + ".");
}
}

Now, you can access and modify the brand and year attributes using the getter and setter methods.
javaCopy code
Car myCar = new Car("Toyota", 2022);
myCar.start();
myCar.drive();

myCar.setBrand("Honda");
System.out.println("New brand: " + myCar.getBrand());

Inheritance

Inheritance is another fundamental concept in OOP that allows you to create a new class based on an existing class. The new class (subclass or child class) inherits attributes and methods from the existing class (superclass or parent class). This promotes code reuse and hierarchy.
Here's a simple example of inheritance in Java:
javaCopy code
public class ElectricCar extends Car {
// Additional attribute
int batteryCapacity;
// Constructor
public ElectricCar(String brand, int year, int batteryCapacity) {
super(brand, year); // Call the parent class constructor
this.batteryCapacity = batteryCapacity;
}
// New method
void charge() {
System.out.println("Charging the " + getBrand() + " electric car.");
}

}

In this example, we have created an ElectricCar class that inherits from the Car class. It adds a new attribute batteryCapacity and a new method charge.
javaCopy code
ElectricCar myElectricCar = new ElectricCar("Tesla", 2023, 100);
myElectricCar.start();
myElectricCar.drive();
myElectricCar.charge();

Exercise Questions

What is a class in Java, and how does it relate to objects?
Explain the purpose of constructors in Java classes.
What is encapsulation, and why is it important in OOP?
How do you make an attribute in a Java class private, and why might you want to do so?
How does inheritance work in Java, and what is the benefit of using it in your code?

Exercise Answers

In Java, a class is a blueprint or template for creating objects. It defines the structure and behavior of objects that belong to it. Objects are instances of classes, and they represent real-world entities or concepts.
Constructors in Java are special methods in a class that are used to initialize objects when they are created. They have the same name as the class and do not have a return type. Constructors are essential for setting up the initial state of objects.
Encapsulation in Java is the practice of bundling data (attributes) and methods (functions) that operate on the data into a single unit called a class. It also involves controlling access to the data by using access modifiers like public, private, and protected. Encapsulation is important because it helps maintain data integrity and provides a clear interface for interacting with objects while hiding their internal details.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.