Unit 1: Programming
Unit code: D/615/1618
Unit level: 4
Content Prepared by:
School of Computing and Informatics 1
2
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Object Oriented paradigm contains features such as:
▪ Class
▪ Object
▪ Attributes
▪ Methods
▪ Constructors
▪ Encapsulation
➢ Getters
➢ Setters
▪ Polymorphism
➢ Override
➢ Overload
▪ Inheritance
▪ Abstraction
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 3
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 4
It is unusual for programmers to
A key problem with procedural
“start fresh” on each new project
programming is that the program
and write similar software “from
units do not effectively mirror real-
scratch”, this wastes time and money,
world entities, so these units are not
as people repeatedly “reinvent the
particularly reusable.
wheel.”
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 5
Advantages of OOPs over Procedure programming
▪ It is easy to develop and maintain the code with
OOP, whereas it is not easy to manage the code in
Procedural programming if code grows with the size There are a lot
of the project. more advantages to
▪ OOP provides data hiding, whereas, in a Procedural mention
programming, we can access global data from
anywhere.
▪ OOP provides the ability to simulate real-world
events much effectively as compared to Procedural
programming. We can easily solve the real-world
problems if we are using the OOP.
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 6
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 7
The objective of OOP concepts is to save time, gain security and
ease of use. To achieve the primary goal of OOP we should follow
the OOP best practices.
The best practices while using OOPs concepts are:
▪ DRY (Don’t Repeat Yourself): You should never try to have
two blocks of identical/same code in two different places of
the program or application. Instead, we should use one
method for various applications.
▪ Encapsulate methods and variables using private: If
you expect that your Java code can change in the future,
you should encapsulate it by making all variables and
OOP ADVANTAGES methods private. As there are some changes in the code,
you can increase the access to “protected” as needed, but
don't use public.
▪ Single Responsibility Principle: This is another best
practice for OOP concepts in Java. This principle says that a
class should always have only one functionality. That way,
we can call it or extend it on its own whenever new uses
arise without providing coupling between different
functionalities.
▪ Open Closed Design: We should try to make all the
methods and classes Closed for any modification but open
for an extension. That way, the tested code can remain
static, but we can modify it to perform new tasks as
required.
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 8
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 9
Class
OOP MAIN
ASPECTS
Object
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 10
11
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
We live in a world of objects. There are cars, planes, people, animals, buildings, traffic lights, elevators.
Everything in Java is associated with classes and objects
Classes are the core of the OOP where objects are created from classes.
Real world objects can be represented using classes then many objects can be created of a
particular class.
Class is a template for objects, and an object is an instance of a class.
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 12
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 13
A class is a blueprint for creating objects.
It contains the following items:
Properties (Attributes orVariables) Behavior (Methods or Functions)
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 14
Methods Attributes
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 15
Class Name
Car
model
Attributes
color
year
start
Methods stop
beep
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 16
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 17
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 18
Class identifier Class Name
You can create as
many classes as you
// Class body want in a project
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 19
Attributes
Class body contains
Attributes and methods
Methods
20
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 21
22
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Objects are instances that are made from a particular class.
When an object is created, it inherit all the variables and
methods from the class.
Each Object has its own copy of the class’s attributes
and methods.
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 23
An object inherits all
the variables and methods
from the class.
24
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Each Object has its own
copy of the class’s
attributes and methods.
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 25
https://medium.com/@techGrid/oops-concepts-in-java-with-real-time-examples-1e8110a76462
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 26
Objects are created in
the Main Class
Main Class
Car Class
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
27
An object Class
name Constructor
New
keyword
A Class you want
to create an object
from it 28
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed
Three objects from
Car Class are created
Each object is
an independent
instance from the
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 29
others
▪ Each object has its own version of
attributes
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 30
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 31
Modify Attribute:
ObjectName.AttributeName = AttributeValue
Retrieve Attribute:
ObjectName.AttributeName
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 32
Assigning values and retrieving values of object car1
Assigning values and retrieving values of object car2
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 33
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed 34
35
Prepared by: Ms. Dania Alsaid, Ms. Hana Alrasheed