Islamic Emirate of Afghanistan
Ministry of Higher Education
Herat University
Computer Science Faculty
Introduction to Practical Computer Science II (Semester 2)
Introduction to Object-Oriented Concepts in Java Programming
Lecture 04
Lecturer: Mohammed Hamed Amiry
hamed.amir.2015@gmail.com
1
Content…
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
▪
Objectives
Programming Paradigm
Procedural Programming
Object-Oriented Programming.
Why OOP?
Object-Oriented Model
Object
Software Object
Class
Instance of a Class
Class Definition
Class Declaration Syntax
Class Example
Class Body
TV Class Example
Test TV Class
Summary
Reference
2
OBJECTIVES
This lecture introduces the basics of programming paradigms, focusing on
Object-Oriented Programming (OOP). It covers the differences between
procedural and object-oriented approaches, key concepts like classes and
objects, and basic Java syntax to help students build a foundation for
developing scalable and reusable software.
3
PROGRAMMING PARADIGM
A computer program is a sequence of instructions.
A programming style or paradigm is how we organize these instructions, their
data, and the data types needed to organize data.
In other words, a programming paradigm is a style, or “way,” of programming.
Paradigm is a way of doing something.
4
PROGRAMMING PARADIGM
Some languages make it easy to write in some paradigms but not others.
There are two programming paradigms which are popular:
1. Procedural Programming
2. Object-Oriented Programming
5
PROCEDURAL PROGRAMMING
In Procedural Programming:
▪ A program is a list of instructions.
▪ The program is usually broken down into smaller units such as procedures, functions or subroutines.
Procedural Programming methodology is based on creating programs with a
structure, clean flow.
Examples of Procedural Programming are C, Pascal, Fortran and etc.
6
PROCEDURAL PROGRAMMING
It was used for many years in software industry.
Problems with Procedural Programming:
Does not represent real-world problem.
Defining and modeling the problem-set is missing.
Modeling the problem-set is a major blocks in software development projects.
7
OBJECT ORIENTED PROGRAMMING
In the beginning of 1960, attempt is made to design a methodology or
paradigm to represent or simulate real world problems in context of Computer
programs.
The result of these attempts was Object Oriented Methodology.
In Object Oriented Programming, everything is object
8
OBJECT ORIENTED PROGRAMMING
Object-oriented programming enables you to develop large-scale
software and GUIs effectively.
Object-oriented programming is a method for developing reusable
software. Basic features like conditions, loops, methods, and arrays
can solve simple problems, but they are not enough for building
graphical user interfaces (GUI) and large-scale systems. Therefore,
object-oriented concepts are needed for such applications.
9
WHY OOP?
Develop large-scale software
Build GUI
Reusable software
Traditional methods not enough
10
OBJECT ORIENTED PROGRAMMING
We define OOP as follow:
Object-oriented programming is a method of implementation in which programs are organized
as cooperative collections of objects, each of which represents an instance of some class
11
OBJECT ORIENTED PROGRAMMING
The design of this methodology has brought us many benefits:
1.
The programmer is able to create a model of the software beforehand
2.
Simulates real world problems, can be modeled using OO
3.
Enhances code reusability
Object Oriented model can be understand both by client and software developer.
The process in which objects are determined is called Object Oriented Analysis (OOA)
The process in which a model is created is called Object Oriented Design (OOD)
12
OBJECT ORIENTED MODEL (LIBRARY SOFTWARE)
13
OBJECT ORIENTED PROGRAMMING
At the heart of object orientation are two concepts that we have to understand
first: objects and classes.
These form the basis of all programming in object-oriented languages
14
OBJECT
If we look around us, we will find many objects in the real life.
An object is a thing, both tangible and intangible, that we can imagine.
15
OBJECT
All objects have properties (state) and behaviors (actions).
State: Defines object attributes or properties or data that it stores.
Behaviors: Define what the object does.
16
SOFTWARE OBJECT
In object-oriented programming we create software objects that model real
world objects. They also have properties and behavior.
A software object maintains its properties in one or more variables
A software object implements its behavior with methods
An object is a software bundle of variables and related methods
17
SOFTWARE OBJECT
An object is also known as an instance(of a class).
An instance refers to a particular object
A software object has variables that formally known as instance variables.
A software object would have methods which are called instance methods
• Instance variables: light, wheels, color
• Instance methods: brake, change gear
18
CLASS
A class is a blueprint or a template for creating different objects.
A class can contain fields and methods that defines its properties and behaviors.
A class is a template, that defines the variables and the methods common to all
objects of a certain kind.
19
INSTANCE OF A CLASS
20
INSTANCE OF A CLASS
In the previous slide we had a class called “Car”.
We have three instances of class Car.
Each one of them is an instance of Car class.
Each one of them has different properties.
21
OBJECT ORIENTED PROGRAMMING
So we can conclude that:
Object Oriented programming is based on objects that exist in real-world
Each object has its own attributes(variables) and behaviors(methods)
Each object is an instance of a class of his own
22
CLASS DEFINITION
A class definition consists of a signature and a body .
The class signature defines the name of the class and may also specify other
important information.
The body of a class is a set of members enclosed in curly braces.
23
CLASS DECLARATION SYNTAX
A class introduces a developer-defined type into a program.
A class has the following general syntax:
Class names form the nouns of a Java program. Like Employee , Manager, Vehicle, Bicycle , and Account.
By convention, the first letter of a class name is capitalized
24
CLASS EXAMPLE
Object of Employee
25
CLASS BODY
A class can contain:
Fields (Variables)
Methods
Constructor
They are called class members
26
TV CLASS
27
TV CLASS
public class TV {
int channel = 1;
// Default channel is 1
int volumeLevel = 1;
// Default volume level is 1
boolean on = false;
// TV is off
public TV() {
}
public void turnOn() {
on = true;
}
public void turnOff() {
on = false;
}
public void setChannel(int newChannel) {
if (on && newChannel >= 1 && newChannel <= 120)
channel = newChannel;
}
28
public void setVolume(int newVolumeLevel) {
if (on && newVolumeLevel >= 1 && newVolumeLevel <= 7)
}
volumeLevel = newVolumeLevel;
public void channelUp() {
if (on && channel < 120)
channel++;
}
public void channelDown() {
if (on && channel > 1)
channel--; //
}
public void volumeUp() {
if (on && volumeLevel < 7)
volumeLevel++;
}
public void volumeDown() {
}
}
if (on && volumeLevel > 1)
volumeLevel--; //
29
TEST TV CLASS
public class TestTV {
public static void main(String[] args) {
TV tv1 = new TV();
tv1.turnOn();
tv1.setChannel(30);
tv1.setVolume(3);
System.out.println("Channel: " + tv1.channel);
System.out.println("Volume: " + tv1.volumeLevel);
}
}
30
SUMMERY
In this lecture, we explored the concept of programming paradigms and
compared procedural programming with object-oriented programming. We
learned that OOP is a powerful approach for modeling real-world
problems using objects, which contain properties and behaviors. The concept
of classes as blueprints for creating objects was also discussed, along with
basic class structure and syntax in Java. Overall, OOP provides significant
advantages in terms of code reusability, scalability, and maintainability,
making it essential for modern software development.
31
REFERENCE
Introduction to Java Programming and
Data Structures, Comprehensive Version,
Eleventh Edition, Global Edition, Y. Daniel
Liang.
•Chapters : Chapter 9: Objects and Classes
32
Q &A SECTION
33