0% found this document useful (0 votes)
10 views

CS532 Chapter I Spring 2024

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CS532 Chapter I Spring 2024

Uploaded by

dawit kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

HiLCoE

School of Computer
Science & Technology
Object Oriented Programming (with Java) – CS 532
Tariku Worku
Spring 2024
Chapter I: Introduction to Programming Paradigms & OOP

▪ In this chapter:
– What is OOP
– Overview of Programming Paradigms
– Evolution of OOP (SIW)
– Basic Concepts in OOP
– Benefits of OOP
– Setting Up The Development Environment (for Java)
– Types of Application Using Java (Additional)

6/25/2024 2
What is OOP?

▪ Object-oriented programming is a
programming paradigm, or
classification, that organizes a group of
data or attributes or properties with
functions or methods into a unit, known
as a class which gets to be instantiated
to a real-world entity referred to as
OBJECT.

6/25/2024 3
Programming Paradigms

▪ A programming paradigm is a
fundamental style or approach
to programming that guides
the structure and organization
of code. It encompasses a set
of principles, concepts, and
practices that define how
software is designed,
implemented, and managed.

6/25/2024 4
Procedural Vs. Object

PO OO
▪ Program divided into small parts: functions. ▪ Program divided into small parts: objects.
▪ Follows top-down approach. ▪ Follows bottom-up approach.
▪ Has no access controllers. ▪ Has access controllers e.g. private, protected, public...
▪ Hard to add new data and function. ▪ Easy to add new data and function.
▪ Supports encapsulation & inheritance.
▪ No support for encapsulation & inheritance.
▪ Overloading is possible
▪ Overloading not possible.
▪ Data is more important.
▪ Function is more important.
▪ Real-world representation.
▪ Fictional representation.
▪ For large & complex programs & applications.
▪ For medium sized & non-complex programs & applications. ▪ Focus on data abstraction
▪ Focus on procedure abstraction ▪ E.g. C++, Java, Python, C#, etc.
▪ E.g. C, FORTRAN, Pascal, Basic, etc.

6/25/2024 5
Evolution of OOP – (Students’ Reading Work)

6/25/2024 6
Basic Concepts of OOP

Object-Oriented Programming (OOP)


is a programming paradigm that
revolves around the concept of
objects, which are instances of classes
containing both state (attributes) and
behavior (methods). Here are the
basic concepts of OOP

6/25/2024 7
Basic Concepts of OOP … contd.

A class is a blueprint or template that


defines the properties and behaviors
of objects. It serves as a model for
creating objects.
Example Scenario:
Consider a class called Car that defines
the properties of a car such as color,
number of tyres, and brand, as well as
behaviors like accelerating, braking,
and turning.

6/25/2024 8
Basic Concepts of OOP … contd.

An object is an instance of a class. It


represents a real-world entity with
specific attributes and behaviors
defined by its class.
Example Scenario:
An object of the Car class could be a
specific car instance like a red Honda
Civic with license plate XYZ123 that
can accelerate, brake, and turn.
.

6/25/2024 9
Basic Concepts of OOP … contd.

Encapsulation is the bundling of data


(attributes) and methods (functions)
within a class to restrict direct access to
the data and hide the implementation
details.
Example Scenario:
In a banking application, encapsulation
can be implemented by defining a
BankAccount class with private
attributes like account number and
balance, accessed only through getter
and setter methods to ensure data
integrity and security.

6/25/2024 10
Basic Concepts of OOP … contd.

Inheritance is a mechanism where a


class (subclass or child class) can inherit
properties and behaviors from another
class (superclass or parent class),
promoting code reuse and establishing a
hierarchical relationship.
Example Scenario:
Extending the earlier Car example, you
could have subclasses like ElectricCar
and GasolineCar that inherit properties
and behaviors from the Car class but also
have specific attributes and methods
unique to them.

6/25/2024 11
Basic Concepts of OOP … contd.

Polymorphism allows objects of different


classes to be treated as objects of a
common superclass, enabling flexibility in
method implementation and runtime
behavior.
Example Scenario:
In a music streaming application, a class
Song may have a play() method. The play()
method can be customized in subclasses
like Mp3Song and StreamingSong to play
songs differently based on the media type.

6/25/2024 12
Basic Concepts of OOP … contd.

Abstraction refers to the concept of


hiding unnecessary implementation
details and showing only essential
features of an object or class.
Example Scenario:
A mobile phone is an abstraction of a
complex system of hardware and software
components. Users interact with the
phone through its interface without
needing to know the intricate details of its
internal workings.
6/25/2024 13
Basic Concepts of OOP … contd.

Association represents a relationship


between two or more classes, where each
class is independent and can exist without
the other. It can be one-to-one, one-to-
many, or many-to-many.
Example Scenario:
Consider the relationship between a
Library class and a Book class. The Library
class can have an association with multiple
Book objects. The Library and Book
classes are independent entities, and each
can exist without the other. However, the
Library class can have methods to add,
remove, or retrieve Book objects.
6/25/2024 14
Basic Concepts of OOP … contd.

Composition is a strong form of


association where a class is composed of
one or more other classes, and the
composed classes cannot exist
independently without the parent class.
Example Scenario:
Think of a House class that is composed
of multiple Room objects – Bedroom,
LivingRoom, Kitchen, and Bathroom. The
House class owns the Room objects, and if
the House object is destroyed, all
associated Room objects are also
destroyed. The Room objects cannot exist
without being part of the House.
6/25/2024 15
Basic Concepts of OOP … contd.

Aggregation is a weaker form of


association where a class is associated
with another class, but the associated
class can exist independently.
Example Scenario:
Consider a university where a
Department class has an aggregation
relationship with Faculty class.
Departments can have multiple Faculty
members, and each Faculty member can
belong to one or more Departments.
Faculty members can exist independently
of Departments and can be associated
with different Departments over time.
6/25/2024 16
Basic Concepts of OOP … contd.

By understanding and applying these


basic concepts of Object-Oriented
Programming in real-world scenarios,
developers can design modular,
scalable, robust, and maintainable
software solutions that mimic and
model the complexities of the real
world effectively.

6/25/2024 17
Benefits of OOP

S.No Benefit Description Example Scenario


1 Modularity OOP allows for code to be organized into In a banking application, different modules/classes can
modules or classes, making it easier to be created for user authentication, account
manage and update specific parts of the management, transaction processing, and reporting.
code without affecting the entire system. Changes to one module, such as updating transaction
This promotes code reusability and logic, can be done independently without impacting
maintainability. other parts of the application.
2 Encapsulation Encapsulation in OOP bundles data In a healthcare application, patient data can be stored
(attributes) and methods (functions) within as private attributes within a Patient class. Getter and
a class, restricting direct access to data and setter methods can be used to access and update
promoting data integrity. This helps in patient information securely, preventing unauthorized
achieving data hiding and abstraction. access to sensitive data.
3 Inheritance Inheritance allows one class to inherit In a vehicle management system, a base class Vehicle
properties and behavior from another class. can be defined with common attributes and methods
This promotes code reuse and helps in shared by all vehicles. Subclasses like Car, Truck, and
creating a hierarchical relationship between Motorcycle can inherit from the Vehicle class and add
classes. specific attributes and behaviors unique
6/25/2024 18 to each vehicle
type.
Benefits of OOP … Contd.

S.No Benefit Description Example Scenario


4 Polymorphism Polymorphism in OOP allows objects of In a university system, a superclass Person can have a
different classes to be treated as objects of a method speak(). Subclasses like Student and Professor
common superclass, enabling flexibility in can override the speak() method to provide different
method implementation and runtime implementations. When calling the speak() method, the
behavior. system can handle each object based on its specific
type.
5 Code OOP promotes code reusability through n an e-commerce platform, a ShoppingCart class with
Reusability features like inheritance and polymorphism, common shopping cart operations can be reused across
enabling developers to leverage existing different parts of the application, such as user checkout,
code to build new functionalities. order processing, and inventory management, without
duplicating code.
6 Ease of OOP makes it easier to maintain and update In a social media application, introducing a new feature
Maintenance code as changes can be localized to specific like direct messaging can be implemented by adding
classes or modules, reducing the risk of new classes and methods related to messaging,
unintended side effects. keeping the existing code for user profiles and posts
unchanged. 6/25/2024 19
Setting Up The Development Environment

6/25/2024 20
Types of Applications Using Java

6/25/2024 21
Structure of a Simple/Common Java Application

6/25/2024 22
6/25/2024 23

You might also like