SE 109 - Lecture 1: Introduction to OOP Concepts
Outline of Lecture 1
- Differentiate between procedural and object-oriented programming.
- Understand the Concepts of O.O.P
Programming Methodologies
The main programming methodologies discussed are:
- Procedural programming.
- Object-Oriented programming (OOP).
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:
- Programs are divided into smaller procedures/functions.
- Focus is on procedures (actions) rather than data.
- Data is often shared globally or passed between functions.
- Execution typically follows a sequential flow, calling procedures as needed.
- Modules can be interdependent.
(Slide 8 showed a diagram illustrating a program calling different procedures that access shared variables A, B, C).
Features of Procedural Programming
- Large programs are divided into smaller programs (modules/procedures).
- Most of the data is shared and can be accessed from anywhere within the program (global data).
- Procedures can be interdependent, making reuse in different applications difficult.
Advantages of Procedural Programming
- Programs can be easy to read and maintain for smaller projects.
- Code can be organized into various procedures.
- It's relatively straightforward to trace the program flow.
Limitations of Procedural Programming
- If data structures change, many functions that access that data might need modification.
- Functions are often interdependent, making it hard to isolate or reuse them.
- Reusability of code across different applications is generally not well supported.
- Data is often exposed (globally accessible), making it vulnerable to unintended modifications.
- Maintenance can become problematic as the code size increases due to interdependencies and exposed data.
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
- Real-world programming: Models real-world entities and their interactions more accurately than procedural programming.
- Reusability of code: Classes defined once can be reused in multiple applications (through instantiation or inheritance).
- Modularity of code: Objects are self-contained units, making them easier to maintain independently.
- Resilience to change (Flexibility): Changes within a class's implementation details often don't affect other parts of the system that use the class's public interface. Easier to evolve software.
- Information hiding (Encapsulation): Protects an object's internal state from unintended external access, ensuring data security and integrity.
Applications of OOP
- Character User Interface (CUI) based Applications: While less common now, OOP principles can structure command-line tools.
- Graphical User Interface (GUI) based Applications: GUI frameworks heavily rely on OOP (e.g., windows, buttons, menus as objects).
- Computer Aided Designing/Manufacturing (CAD/CAM): Creating reusable graphical and numerical building blocks.
- Web Applications, Mobile Apps, Games, Enterprise Systems, and many more.
Object-Oriented Languages
- Support an O-O view of problem solving.
- Objects are categorized into classes.
- Objects interact with each other.
- Objects have behavior (actions they can perform - methods).
- Objects maintain state (data they hold - attributes/variables).
- Behavior and state are intimately related within an object.
- Software reuse is facilitated (through classes and inheritance).
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
- State: Indicated by the set of attributes (variables) and their current values.
- Behavior: How an object acts and reacts, defined by its methods, often involving changes to its state.
- Identity: Each object is unique, even if it has the same state as another object of the same class. (Think of two identical physical objects - they are still distinct 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.
- This can be to request information (calling a 'getter' method).
- This can be to request an action or change state (calling a 'setter' or action method).
- Often involves Client/Server relationships (one object, the client, requests a service from another object, the server).
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
- Variables: Named storage locations for holding values (primitive types like
int
, double
, or references to objects).
- Objects: Instances of classes (user-defined types). They reside in memory, and variables hold references (memory addresses) pointing to these objects.
- A variable can be reassigned to refer to a different object of a compatible type, or it can be
null
(referring to no object).
- Variables holding primitive types contain the actual value directly. Variables holding object types contain a reference (address).
Summary of Lecture 1
- O-O problem solving involves identifying classes, objects, and their interactions.
- Objects maintain state (data) and exhibit class-defined behavior (methods).
- Instances (objects) of the same class behave in similar ways (share methods) but have unique identities and potentially unique states.
- Message passing (method calls) illustrates object interaction.
- Messages can include arguments (parameters) for providing additional information.
- Messages can return results (return values) to the caller.