Programming Paradigms
Widely used:
Imperative: do this and next do that
Examples: Fortran, Algol, Pascal, Basic, C
Object-Oriented: send messages between objects
OBJECT ORIENTED Examples: Smalltalk, Java, C++
PROGRAMMING WITH
JAVA
Introduction M. El Dick - I2211
Programming Paradigms Imperative Programming
Not widely used: Oldest and most obvious approach
Functional: modeled on mathematical functions Focuson : What it has to do
Example: Lisp Program is a series of instructions
Code reuse with functions
Logical: use of mathematical logic
Example: Prolog
M. El Dick - I2211 M. El Dick - I2211
Object Oriented Programming Object Oriented Programming
(OOP) (OOP)
Newer than imperative programming Many languages support multiple paradigms:
Now the most widely used paradigm Java : imperative or OO
Focus on : Except for very simple programs, OO way is
What objects the system contains much better
How they interact
M. El Dick - I2211 M. El Dick - I2211
Imperative vs. Object
Oriented What code can I reuse?
Library classes
1. Understand the problem
2. Break it into sub-problems Code written by others
3. Write algorithm to solve sub-problems Your own code!
1. Understand the whole problem
domain
Monolithic, 2. Write a system which models the
non-reuseable solution domain
Set of components which can be reused,
to solve different problems
within the domain
M. El Dick - I2211 M. El Dick - I2211
Key Concepts of OOP Classes and Objects
Classes and objects Class Object
Abstraction
Represent
Encapsulation Define a type of “things/objects”
Polymorphism object from the real world
Example: “the red
Inheritance Example: “car” car in the parking”
Also represent things
like arrays,
messages, errors
M. El Dick - I2211 M. El Dick - I2211
Classes and Objects Abstraction
Car Ignore details when necessary
Class String brand
String color
int speed Think about what an object/class does,
int size not how it does it
Treat it as a black box
is instance of is instance of
Example: car
MyCar JohnCar We only need its size and color in the problem
Objects brand “honda” brand “kia” We design a class with information only about the size and the
color “blue” color “black” color
speed 65 speed 65
size 100 size 80
M. El Dick - I2211 M. El Dick - I2211
Encapsulation Encapsulation
Hide implementation details Class 1 Class 1
Class2
Group related data and operations in a class/object
Interface
Other classes/objects : public methods
Can interact with its public information or interface
Method1 private
Are denied access to its private information Variable methods private data
1
Method2 Variable
2
… …
Variable
n
Method n
M. El Dick - I2211 M. El Dick - I2211
Encapsulation Inheritance
Extend a class and create subclasses
Car
String brand
String color
access A subclass can :
Private fields denied
int speed
Other objects inherit fields and methods of its superclass
and methods int size
can add new fields and methods
change_gear() can redefine (override) methods of its superclass
brake) access
Public methods allowed
accelerate()
M. El Dick - I2211 M. El Dick - I2211
Inheritance Polymorphism
Superclass Person
Implement same interface in different ways
Literally: “many shapes”
Informally: same instruction means different things to
Subclasses Student Employee different agents
E.g. “write a note” is similar but different on:
Computer
Paper
Master
Manager
student
M. El Dick - I2211 M. El Dick - I2211
Polymorphism What is Java?
SportPlayer
Superclass
Interface Java technology is both :
play()
…
a programming language
and
a platform
Subclasses BasketballPlayer FootballPlayer
play() play()
… Different behaviors …
M. El Dick - I2211 M. El Dick - I2211
Java the Programming Language Java is Portable
Simple and easy to use syntax “Write once and run everywhere”
Secure Regardless of the OS or the hardware
No pointers like in C++ and arbitrary memory access Any platform: Windows, MacOS, Linux, etc.
Portable
Many libraries (in the form of packages) thanks to Java Virtual Machine (JVM)
New libraries are introduced constantly
Environment in which Java programs execute
Object oriented
Software that is implemented on top of hardware
Designed for distributed systems
and OS
M. El Dick - I2211 M. El Dick - I2211
Java the Platform Java the Platform
Platform : hardware/software environment in which 2 components :
programs run Java Virtual Machine (JVM)
Windows, Linux, Mac OS Java Application Programming Interface (API)
Java platform
MyProgram.java
Software-only platform that runs on top of hardware platforms
API
Java
Set of programs to develop and run programs written in Java Java Virtual Machine
Platform
language
Hardware-based Platform
M. El Dick - I2211 M. El Dick - I2211
Java API Java Platform Versions/Editions
Java SE - Standard Edition
Large collection of ready-made software components
Java EE - Enterprise Edition
Grouped into libraries of related classes and
interfaces
Java ME - Micro Edition
Libraries are known as packages
For mobile phones
JDBC, AWT, RMI, etc.
M. El Dick - I2211 M. El Dick - I2211
Java SE Java EE
JRE – Java Runtime Environment Java EE – Enterprise Edition
For users For enterprises
JVM + standard library JDK + more libraries for servers (JDBC, RMI,
email)
JDK - Java Development Kit For distributed deployment (Enterprise’s
For developers intranet)
JRE + Java compiler + other programming
tools
M. El Dick - I2211 M. El Dick - I2211
Java Platform Diagram from Sun The Simplest Java Program
public class Hello {
public static void main(String[] args) {
// Display a message to the screen
System.out.println("Hello World\n");
}
}
The program is contained in a file called Hello.java
M. El Dick - I2211 M. El Dick - I2211
Compiling a Java Program Running a Java Program
javac Hello.java java Hello
Create file Hello.class : bytecode This command will:
1. Start the JVM
Bytecode 2. Load the bytecode from the file Hello.class into the JVM
cannot be run directly by the machine 3. The Java Virtual Machine will execute the loaded bytecode
can be run by Java Virtual Machine (JVM)
Same .class file can run on Windows, Solaris, Linux, or
Mac OS
M. El Dick - I2211 M. El Dick - I2211
Compiling and Running Java
Program
Source
code javac Bytecode java Code is executed
Hello.java Hello.class
Hello
library files
M. El Dick - I2211