Lecture 01 (6)
Lecture 01 (6)
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 1
OOAD?
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 2
What is Object-Oriented Paradigm?
• Definition: A programming model where software design is
organized around objects rather than functions and logic.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 3
Why Object-Oriented Programming (OOP)?
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 4
Why Object-Oriented Programming (OOP)?
Modularity
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 5
Historical Evolution of Object Model (1)
• Simula (1960s):
• First Object-Oriented Language.
• Introduced Classes and Objects for simulation.
• Key feature: Inheritance for reusing code.
• Smalltalk (1970s):
• Developed by Alan Kay.
• Added Messaging and formalized Encapsulation.
• First to introduce Polymorphism.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 6
Historical Evolution of Object Model (2)
• C++ (1980s):
• Added object-oriented features to C.
• Inheritance, Polymorphism, and Abstraction became widely used.
• Java (1990s):
• Widespread popularity with cross-platform support.
• Garbage collection and strong OOP features.
— Garbage collection in Java is the automated process of deleting code that's no longer
needed or used.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 7
Fundamental Concepts of OOP (1)
• Abstraction:
• Hides complex implementation details, exposes only what’s
necessary.
• Example:
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 8
Abstraction – Example:
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 9
Fundamental Concepts of OOP (2)
• Encapsulation (Information Hiding):
• Combines data (attributes) and methods (behavior) into a single
unit (class).
• Controls access to data through getter and setter methods.
• Example:
Bank account with balance (private), accessed only via deposit and
withdraw methods.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 10
Encapsulation - Example
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 11
Fundamental Concepts of OOP (3)
• Inheritance:
• Enables new classes to inherit properties and methods from
existing ones.
• Parent-Child relationship: Child class extends or overrides
behavior.
• Example:
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 12
Inheritance – Example:
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 13
Fundamental Concepts of OOP (4)
• Polymorphism: Poly (many) + morphism (forms)
• Allows objects to be treated as instances of their parent class
while retaining their own unique methods.
• Two types:
• Compile-time (method overloading)
• Run-time (method overriding)
• Example:
A Shape class with subclasses Circle and Square, each having its own
implementation of draw() method.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 14
Polymorphism – Example:
Speak();
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 15
What is an Object?
• Definition: A real-world entity modeled in code.
Object = State + Behavior.
• State: Represented by attributes (data fields).
• Behavior: Represented by methods (functions).
• Example:
• Object: A Car
• State: Color, Model, Speed How to draw the structure
• Behavior: Drive, Stop of this object?
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 16
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 17
What is a Class?
• Class Definition: A blueprint for creating objects.
• Components:
• Attributes: Variables that hold data.
• Methods: Functions that define behaviors.
• Example:
• Class Car with attributes color, model, and methods
drive(), stop().
How to draw the structure
of this object?
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 18
Draw the structure of this
What is a Class? class????
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 19
Object Relationships: Association
Association:
• A general relationship between objects of different classes.
• Example: A student attends multiple courses; each course
has multiple students.
• Represented as:
Student Course
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 20
Object Relationships: Aggregation
Aggregation:
• A "has-a" relationship where one object contains other objects.
• Weak relationship: Contained objects can exist independently.
• Example: A Library has Books, but the Books can exist without
the Library.
• Represented as:
Library Books
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 21
Object Relationships: Composition
Composition:
• A stronger form of aggregation.
• Strong relationship: Contained objects cannot exist without
the container.
• Example: A House and Rooms – if the house is destroyed, the
rooms no longer exist.
• Represented as:
House Rooms
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 22
Object Relationships: Inheritance
Inheritance:
• A relationship where one class (child) inherits properties and
methods from another class (parent).
• Is-a relationship: A specific type of general class.
• Example: Dog is a Mammal, so it inherits characteristics from
the Mammal class.
• Represented as:
Mammal Dog
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 23
Key points
• OOP is a paradigm based on objects.
• Key concepts: Abstraction, Encapsulation, Inheritance, Polymorphism.
• Objects and Classes form the foundation of OOP.
• Relationships between objects enhance the structure of a system.
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 24
In class activity:
Identify Real-World Objects
• Instructions:
• Get into small groups (5 members for each group).
• Pick a real-world object (e.g., car, bank account, library, smartphone).
• Identify:
• Attributes
• Behaviors
• Class
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 25
Ms. Chathurangi Dhanushika Weerasinghe, MSc UCSC(Col), BSc (Ruh) Saegis Campus 26