SE 109 - Lecture 1: Introduction to OOP Concepts

Outline of Lecture 1

Programming Methodologies

The main programming methodologies discussed are:

1. Procedural Programming

Procedural programming involves dividing a large program into a set of modules (procedures, functions, subroutines) to perform specific tasks. Languages like Pascal and C utilize this approach.

Key Characteristics:

(Slide 8 showed a diagram illustrating a program calling different procedures that access shared variables A, B, C).

Features of Procedural Programming

Advantages of Procedural Programming

Limitations of Procedural Programming

2. Object-Oriented Programming (OOP)

In OOP, a large application is broken down into independent components called objects. These objects encapsulate data and behavior and interact with each other to form a complete program.

Advantages of OOP

Applications of OOP

Object-Oriented Languages

Classes and Objects

Class: A blueprint or template that defines the structure (attributes/state) and behavior (methods) for a type of object. It's a user-defined data type. Example: A `Cat` class defines properties like name, age, type and behaviors like meow, play, eat.

Object: An instance of a class. It's a basic building block of OOP. An object has its own state (specific values for attributes) but shares the behavior defined by its class. You access the data (state) of an object through its methods (behavior). Example: `myCat` and `yourCat` could be two distinct objects of the `Cat` class, each with potentially different names and ages.

(Slides 19 & 20 showed diagrams illustrating the Cat class blueprint and multiple cat object instances created from it, each with unique state but shared behavior).

Characteristics of Objects

Objects of the same class share the same behavior (methods), may or may not have the same state, but will always have a unique identity.

(Slide 22 showed characteristics: Objects can be physical (Shirt) or conceptual (Online Account). Properties include Size, Price, Color. Behaviors include Shop, Put item in cart, Pay).

(Slide 23 reiterated the Class as a blueprint/recipe and Objects as instances).

Object Interaction

Objects communicate by sending messages to each other. In programming terms, this usually means one object calling a method on another object.

Creating an Object

Objects are typically created using the new keyword followed by a call to the class's constructor.

Example (Conceptual): Defining a variable and constructing an object.

// An illustration of object creation.
class A {
    public static void main(String[] args){
        // Define a variable 'b' that can refer to
        // an object of class B.
        B b;

        // Construct a new B object and make 'b' refer to it.
        b = new B(); // Assumes class B exists with a constructor B()
    }
}

Sending a Message (Calling a Method)

Use the object variable followed by a dot (.) and the method name with parentheses ().

Example (Conceptual): Calling the `report()` method on object `b`.

// An illustration of message sending.
class C{
    public static void main(String[] args){
        // Make b refer to a new B object.
        B b = new B(); // Create instance

        // Send the 'report' message to the object b refers to.
        b.report(); // Assumes class B has a method report()
    }
}

Passing Arguments with Messages

Some methods require additional information (arguments or parameters) to perform their task. These are passed inside the parentheses.

Example (Conceptual): Setting the speed of a `Car` object.

class Truck {
    public static void main(String[] args){
        Car c = new Car(); // Assume Car class exists

        c.move(); // Assume move() method exists

        // Ask the Car object 'c' to change its speed to 90.
        // 90 is the argument passed to the setSpeed method.
        c.setSpeed(90); // Assume setSpeed(int speed) method exists

        c.move();
    }
}

Receiving Replies (Return Values)

Methods can return information back to the caller. The result is often stored in a variable.

Example (Conceptual): Getting the current speed from a `Car` object.

class Truck{
    public static void main(String[] args){
        Car c = new Car(); // Assume Car class exists

        double currentSpeed; // Variable to store the reply

        // Ask the car 'c' what its current speed is.
        // The getSpeed() method returns a value (presumably a double).
        currentSpeed = c.getSpeed(); // Assume getSpeed() returns a double

        // Increase the car's speed based on the received value.
        c.setSpeed(currentSpeed + 1); // Use the returned value

        c.move();
    }
}

Variables and Objects

Summary of Lecture 1