Lesson 1
Classes and Objects
Classes and Objects

Let's dive into a foundational concept in Object-Oriented Programming (OOP): Classes and Objects. If you have already explored OOP concepts in other programming languages or previous units, this might serve as a good reminder. If not, no worries, we'll start from the basics.

Classes and objects are the building blocks of OOP. A class acts as a blueprint for creating objects, which are instances of the class. Understanding these basics is essential before moving on to more advanced OOP topics like inheritance, polymorphism, and encapsulation.

Defining a Class

In Java, a class is defined using the class keyword. A class serves as a blueprint to create objects. Here's a simple example:

Java
1// Defining a class named Person 2public class Person { 3 // Data members of the class (name and age) 4 private String name; 5 private int age; 6}

A class can have fields (variables), methods (functions), and constructors. In this snippet, we define a Person class with data members name and age.

What is an Object?

An object is an instance of a class. It represents a specific example of the class and holds the characteristics that define the class.

Objects have three main characteristics:

  • State: The data or attributes of the object. In the Person class, the name and age are the object's state.
  • Behavior: The methods and functions that the object can perform. In the Person class, the display method is an example of behavior.
  • Identity: A unique identifier that distinguishes the object from others, even if they have the same state. This is handled by the memory address in the JVM.

An object is thus a concrete instance of a class that includes state, behavior, and identity.

Using Constructors

Constructors initialize the newly created object's state. In the Person class, we can define a constructor to set the name and age:

Java
1public class Person { 2 private String name; 3 private int age; 4 5 // Constructor that initializes the object with a name and age 6 public Person(String name, int age) { 7 this.name = name; 8 this.age = age; 9 } 10}

In the constructor, this refers to the current instance of the class. It is used to distinguish the class's fields from the parameters with the same names.

Overloading Constructors

Java allows you to have multiple constructors in a class, each with a different parameter list. This is known as constructor overloading. Overloading constructors allows you to create objects in different ways, depending on the parameters provided.

Here's an example of a class with overloaded constructors:

Java
1public class Car { 2 private String brand; 3 private int year; 4 5 // Constructor with both brand and year 6 public Car(String brand, int year) { 7 this.brand = brand; 8 this.year = year; 9 } 10 11 // Constructor with only brand, default year is set 12 public Car(String brand) { 13 this(brand, 2020); // Calls the other constructor 14 } 15 16 // Constructor with no parameters, default values are set 17 public Car() { 18 this("Unknown", 2000); // Calls the constructor with two parameters 19 } 20}

In this example, the Car class has three constructors:

  1. One that takes both brand and year as parameters.
  2. One that takes only brand and sets a default value for year.
  3. One that takes no parameters and sets default values for both brand and year.

Overloading constructors provides flexibility in how you create instances of a class.

Copy Constructors

A copy constructor in Java is a special type of constructor used to create a new object as a copy of an existing object. It takes a single parameter: an object of the same class. The fields of the new object are initialized with the values of the fields of the existing object, effectively cloning the object. This is useful when you want to create a duplicate object with the same state.

Here’s how a copy constructor looks:

Java
1public class Car { 2 private String brand; 3 private int year; 4 5 // Regular constructor 6 public Car(String brand, int year) { 7 this.brand = brand; 8 this.year = year; 9 } 10 11 // Copy constructor 12 public Car(Car other) { 13 this.brand = other.brand; 14 this.year = other.year; 15 } 16}

In the example above, the copy constructor ensures that all fields (brand and year) from the original Car object are accurately copied to the new Car object.

Member Functions

Member functions define the behavior of the object. For the Person class, we can define a method to display the object's data:

Java
1public class Person { 2 private String name; 3 private int age; 4 5 public Person(String name, int age) { 6 this.name = name; 7 this.age = age; 8 } 9 10 // Member function to display the object's data 11 public void display() { 12 System.out.println("Name: " + name + ", Age: " + age); 13 } 14}
Creating Objects from Classes

Once you have defined a class, you can create objects (instances of the class). Here’s how we can create and use objects of the Person class:

Java
1public class Person { 2 // Data members of the class (name and age) 3 private String name; 4 private int age; 5 6 // Constructor that initializes the object with a name and age 7 public Person(String name, int age) { 8 this.name = name; 9 this.age = age; 10 } 11 12 // Member function to display the object's data 13 public void display() { 14 System.out.println("Name: " + name + ", Age: " + age); 15 } 16} 17 18public class Main { 19 public static void main(String[] args) { 20 Person person = new Person("Alice", 30); // Creating an object using the new keyword 21 person.display(); // Displaying the object's data 22 } 23}

Here, we create an object, person, with the name "Alice" and age 30 by using the new keyword along with the Person class's constructor. The inputs "Alice" and 30 are passed directly to the constructor to initialize the object's state.

The object then uses its display method to print its data to the console. This demonstrates how to instantiate a class and call its member functions in a Java program.

Why It Matters

You might wonder why this is important.

Understanding classes and objects is critical because they enable you to model real-world entities in your programs. For instance, a Person class helps you create multiple person objects with different names and ages, enabling you to manage and manipulate data efficiently. This principle is the backbone of complex software systems.

With this knowledge, you will be better prepared to approach more advanced OOP techniques and design patterns, allowing you to write clean, modular, and scalable code. Let's get started with the practice section to gain more hands-on experience.

Ready to code? Let's dive in!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.