Module 2

Object-Oriented Programming in Java

25 mins read

Lesson 1: Classes and Objects

1.1 Introduction to Classes and Objects

In Java, classes are blueprints for creating objects, which are instances of classes. A class defines the properties (fields) and behaviors (methods) of objects. Objects encapsulate data and behavior within a single unit. Objects interact with each other through methods and can communicate by calling methods on other objects. Classes promote code reusability and maintainability by allowing you to create multiple instances (objects) based on the same blueprint (class).

JAVA
// Class definition
public class Car {
    // Fields (properties)
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void displayInfo() {
        System.out.println("Car: " + make + " " + model + " " + year);
    }
}

// Creating objects
Car myCar = new Car("Toyota", "Camry", 2020);
myCar.displayInfo();
1.2 Constructors and Initialization

Constructors are special methods used for initializing objects when they are created. They have the same name as the class and do not have a return type. Constructors can accept parameters to initialize object properties during instantiation. Initialization ensures that objects are properly configured with initial values before they are used in the program. In Java, every class has at least one constructor. If no explicit constructor is defined, Java provides a default constructor.

JAVA
// Class definition with constructor
public class Student {
    String name;
    int age;

    // Constructor with parameters
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display student info
    public void displayInfo() {
        System.out.println("Student Name: " + name + ", Age: " + age);
    }
}

// Creating object using constructor
Student student1 = new Student("Alice", 20);
student1.displayInfo();

Lesson 2: Inheritance and Polymorphism

2.1 Inheritance in Java

Inheritance is a mechanism in Java where a class (subclass or child class) inherits properties and behaviors from another class (superclass or parent class). The subclass extends the superclass, inheriting fields and methods defined in the superclass. It promotes code reuse and allows for the creation of hierarchical relationships between classes. Inherited members can be accessed directly in the subclass using the super keyword.

JAVA
// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class (subclass)
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}
2.2 Polymorphism in Java

Polymorphism allows objects of different classes to be treated as objects of a common superclass through inheritance. It enables flexibility and extensibility in the code by allowing methods to be overridden in subclasses to provide specific implementations. Polymorphism is achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism). It enhances code readability and reusability by promoting the use of superclass references to refer to subclass objects.

JAVA
// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class 1 (subclass)
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

// Child class 2 (subclass)
class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows");
    }
}

Lesson 3: Abstract Classes and Interfaces

3.1 Abstract Classes in Java

Abstract classes are classes that cannot be instantiated directly and are meant to be subclassed. They may contain abstract methods (methods without a body) that must be implemented by subclasses. Abstract classes can also have concrete methods (methods with a body) that are inherited by subclasses. They provide a way to define common behavior and enforce method implementation in subclasses. Abstract classes are declared using the abstract keyword.

JAVA
// Abstract class
abstract class Shape {
    // Abstract method (no body)
    abstract void draw();
    
    // Concrete method (with body)
    void display() {
        System.out.println("Displaying shape");
    }
}

// Concrete subclass
class Circle extends Shape {
    // Implementation of abstract method
    void draw() {
        System.out.println("Drawing circle");
    }
}
3.2 Interfaces in Java

Interfaces in Java are similar to abstract classes but cannot contain instance fields or concrete methods. They define a contract for classes that implement them, specifying methods that must be implemented. Interfaces support multiple inheritance by allowing classes to implement multiple interfaces. They provide a way to achieve abstraction and polymorphism in Java programs. Interfaces are declared using the interface keyword and can include constants and method declarations.

JAVA
// Interface
interface Printable {
    void print();
}

// Class implementing interface
class Document implements Printable {
    // Implementation of interface method
    public void print() {
        System.out.println("Printing document");
    }
}