Object Oriented Programming: Course Teacher: Md. Mahadi Hassan Associate Professor, CSE, IIUC
Object Oriented Programming: Course Teacher: Md. Mahadi Hassan Associate Professor, CSE, IIUC
Lectures 1
Prepared by MMH1
Contents
Software, Programming language, Algorithm,
Flowchart
OOP, Features of OOP
Classes and Objects
Difference between Structured Programming and
Object Oriented Programming
Difference between C/C++/Java
History of Java
Local variable, global variable
etc
2
Software (Program)
A software or a program can be defined as a
complete set of written instructions written by
the programmers which enables the computer
to obtain the solution of a problem.
3
Classification of Software
4
Open Question
Some other S/W: Malware, Adware, spyware,
Greyware, Freeware, Shareware, Nagware,
Slimeware, Webware
Difference between a Virus, Worm and Trojan
Horse?
5
Programming Languages
(Machine Language Assembly Language High-Level Language)
1101101010011010
6
Programming Languages
(Machine Language Assembly Language High-Level Language)
…
ADDF3 R1, R2, R3
Assembler …
1101101010011010
…
…
7
Programming Languages
(Machine Language Assembly Language High-Level Language)
8
Popular High-Level Languages
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language developed by Microsoft)
Delphi (Pascal-like visual language developed by Borland)
C++ (an object-oriented language, based on C)
C# (a Java-like language developed by Microsoft)
Java (We use it here)
9
Compiling Source Code
A program written in a high-level language is called a source
program. Since a computer cannot understand a source program.
Program called a compiler is used to translate the source program
into a machine language program called an object program. The
object program is often then linked with other supporting library
code before the object can be executed on the machine.
Library Code
10
What is OOP?
Modelling real-world objects in software
Why design applications in this way?
We naturally classify objects into different types.
By attempting to do this with software aim to make it
more maintainable, understandable and easier to
reuse
In a conventional application we typically:
decompose it into a series of functions,
define data structures that those functions act upon
there is no relationship between the two other than the
functions act on the data
11
What is OOP?
How is OOP different to conventional programming?
Decompose the application into abstract data types by
identifying some useful entities/abstractions
An abstract type is made up of a series of behaviours
and the data that those behaviours use.
Similar to database modelling, only the types have
both behaviour and state (data)
12
Object-Oriented Programming
There are several concepts underlying OOP:
Abstract Types (Classes)
Encapsulation (or Information Hiding)
Aggregation
Inheritance
Polymorphism
13
Abstract Data Types
Identifying abstract types is part of the modelling/design process
The types that are useful to model may vary according to the
individual application
For example a payroll system might need to know about
Departments, Employees, Managers, Salaries, etc
An E-Commerce application may need to know about Users,
Shopping Carts, Products, etc
Object-oriented languages provide a way to define abstract data
types, and then create objects from them
It’s a template (or ‘cookie cutter’) from which we can create new objects
For example, a Car class might have attributes of speed, colour, and
behaviours of accelerate, brake, etc
An individual Car object will have the same behaviours but its own
values assigned to the attributes (e.g. 30mph, Red, etc)
14
----- ----- -----
----- ----- -----
----- ----- -----
----- ----- -----
" C o n v e n t i o n a l P r o g r a m m in g " - -
F u n c t io n s o r P r o c e d u r e s o p e r a t in g o n in d e p e n d e n t d a t a
" O O P r o g r a m m in g " - - 15
A b s t r a c t T y p e s c o m b in e d a t a a n d b e h a v io u r
Encapsulation
The data (state) of an
object is private – it cannot
be accessed directly.
The state can only be " T h e D o u g h n u t D ia g r a m "
S h o w in g t h a t a n o b je c t h a s
changed through its p r iv a t e s t a t e a n d p u b lic
behaviour, otherwise b e h a v io u r . S t a t e c a n o n ly b e
c h a n g e d b y in v o k in g s o m e
known as its public b e h a v io u r
interface or contract P r iv a t e D a t a
This is called
encapsulation
P u b lic I n t e r f a c e
16
Encapsulation
Main benefit of encapsulation
Internal state and processes can be changed independently
of the public interface
Limits the amount of large-scale changes required to a
system
17
What is an OO program?
What does an OO program consist of?
A series of objects that use each others behaviours in order
to carry out some desired functionality
When one object invokes some behaviour of another it
sends it a message
In Java terms it invokes a method of the other object
A method is the implementation of a given behaviour.
OO programs are intrinsically modular
Objects are only related by their public behaviour (methods)
Therefore objects can be swapped in and out as required
(e.g. for a more efficient version)
This is another advantage of OO systems
18
Aggregation
Aggregation is the ability to create new classes out of existing
classes
Treating them as building blocks or components
Aggregation allows reuse of existing code
“Holy Grail” of software engineering
Two forms of aggregation
Whole-Part relationships
Car is made of Engine, Chassis, Wheels
Containment relationships
A Shopping Cart contains several Products
A List contains several Items
19
Inheritance
Inheritance is the ability to define a new class in terms of an
existing class
The existing class is the parent, base or superclass
The new class is the child, derived or subclass
The child class inherits all of the attributes and behaviour of its
parent class
It can then add new attributes or behaviour
Or even alter the implementation of existing behaviour
20
Polymorphism
Means ‘many forms’
Difficult to describe, easier to show, so we’ll look at this one in a
later lesson
In brief though, polymorphism allows two different classes to
respond to the same message in different ways
E.g. both a Plane and a Car could respond to a ‘turnLeft’
message,
however the means of responding to that message (turning
wheels, or banking wings) is very different for each.
Allows objects to be treated as if they’re identical
21
Summary!
In OO programming we
Define classes
Create objects from them
Combine those objects together to create an application
Benefits of OO programming
Easier to understand (closer to how we view the world)
Easier to maintain (localised changes)
Modular (classes and objects)
Good level of code reuse (aggregation and inheritance)
22
Why Java?
The answer is that Java enables users to develop
and deploy applications on the Internet for servers,
desktop computers, and small hand-held devices.
The future of computing is being profoundly
influenced by the Internet, and Java promises to
remain a big part of that future. Java is the Internet
programming language.
23
What is Java?
A general purpose, high-level programming
language with support for object-oriented
programming.
A collection of wide-ranging application
programming interfaces (APIs).
A self-contained runtime system.
A complete set of development tools.
24
Java, Web, and Beyond
Java can be used to develop Web
applications.
Java Applets
Java Web Applications
Java can also be used to develop
applications for hand-held devices such as
Palm and cell phones
25
Web and many devices..
26
Java is not…
An internet-only programming language.
Difficult.
JavaScript.
27
Java’s Main Features
Uses virtual machine model to assure true
“write once, run anywhere” programs.
Built-in support for GUIs.
Built-in networking.
Built-in security features.
Built-in support for multi-threaded
programming.
Self-documenting.
28
Java applications
Applets
Stand-alone programs
Network servers
Network clients
Embedded designs
Mobile telephones
Portable Digital Assistants (PDAs)
Set-top boxes
Digital Signal Processing (DSP)
29
Java Virtual Machine
The conventional compiled-code model
object code
Microprocessor
system
30
Java Virtual Machine
The virtual machine model
bytecode
Microprocessor
system
31
Java Virtual Machine
Advantages
Compiled bytecode can run without
modification on any platform that has a Java
Virtual Machine.
“Compile once, run anywhere”.
32
Java Virtual Machine
Disadvantages
Compiled bytecode runs a little slower than
conventional compiled code.
Advances in JVM technology are closing the
gap.
Not all low-level system manipulations are
available through standard Java APIs.
Low-level system manipulations are available
in the form of native (non-Java) extensions.
33
Java Language Features
Syntax is similar to C++.
Full support for OOP
Classes and objects
Information hiding/access control
Inheritance, method overriding
Polymorphism
Abstract methods and classes
Interfaces
34
Java Language Features
Everything that is not a primitive is an object.
No pointers! Objects are accessed through
reference variables.
Built-in memory management and garbage
collection.
35
Java API Features
Support for the essentials
Classes and objects
I/O
Threads
Applet support
Conventions used by applets
36
Java API Features
Graphic User Interface (GUI) support
Abstract Windowing Toolkit (AWT)
Windows, dialogs, file dialogs
Buttons, checkboxes
Menus, menu bars
Scrollbars
etc.
New generation GUI toolkit: Swing
All the above, plus...
Platform independent and run-time changeable
look-and-feel.
And more. 37
Java API Features
Networking
TCP/IP
UDP
URLs
Internationalization
Programs can automatically adapt to specific
locales and be displayed in the appropriate
language.
38
Java API Features
Security
Electronic signatures
Public and private key management
Access control
Certificates
Java Database Connectivity (JDBCTM)
Provides uniform access to a wide range of
relational databases.
39
Java API Features
The Java platform also has APIs for
2D and 3D graphics
Accessibility
Servers
Collaboration
Telephony
Speech
Animation
and more.
40
Benefits of Programming in Java
Get started quickly
Although the Java programming language is a
powerful object-oriented language, it's easy to learn,
especially for programmers already familiar with C or
C++.
Write less code
Comparisons of program metrics suggest that a
program written in the Java programming language
can be four times smaller than the same program in
C++.
41
Benefits of Programming in Java
Write better code
The Java programming language encourages
good coding practices.
Garbage collection helps you avoid memory
leaks.
Its object orientation and wide-ranging, easily
extendible API let you reuse other people's
tested code and introduce fewer bugs.
42
Benefits of Programming in Java
Develop programs faster
Development time may be as much as twice as fast
compared to writing the same program in C++
because you write fewer lines of code and it is a
simpler programming language than C++.
43
Benefits of Programming in Java
Avoid platform dependencies.
You can keep your program portable by avoiding the
use of libraries written in other languages and native
methods.
Write once, run anywhere.
Because 100% pure Java programs are compiled
into machine-independent bytecodes, they run
consistently on any Java platform.
44
Benefits of Programming in Java
Distribute software more easily.
Upgrade applets easily from a central server. Applets
take advantage of the feature of allowing new classes
to be loaded "on the fly," without recompiling the
entire program.
45
History of Java
James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
HotJava - The first Java-enabled Web browser
Early History Website:
http://java.sun.com/features/1998/05/birthday.html
46
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
47
Characteristics of Java
Java Is Simple Java is partially modeled on C++, but greatly
Java Is Object-Oriented simplified and improved. Some people refer to
Java Is Distributed Java as "C++--" because it is like C++ but
with more functionality and fewer negative
Java Is Interpreted
aspects.
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
48
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Java Is Object-Oriented Although many object-oriented languages
Java Is Distributed began strictly as procedural languages,
Java was designed from the start to be
Java Is Interpreted
object-oriented. Object-oriented
Java Is Robust programming (OOP) is a popular
Java Is Secure programming approach that is replacing
Java Is Architecture-Neutral traditional procedural programming
Java Is Portable
techniques.
Java's Performance
One of the central issues in software
Java Is Multithreaded development is how to reuse code. Object-
Java Is Dynamic oriented programming provides great
flexibility, modularity, clarity, and
reusability through encapsulation,
inheritance, and polymorphism.
49
Characteristics of Java
Java Is Simple Distributed computing involves several
Java Is Object-Oriented computers working together on a network.
Java Is Distributed Java is designed to make distributed
computing easy. Since networking
Java Is Interpreted
capability is inherently integrated into
Java Is Robust Java, writing network programs is like
Java Is Secure sending and receiving data to and from a
Java Is Architecture-Neutral file.
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
50
Characteristics of Java
Java Is Simple You need an interpreter to run Java
Java Is Object-Oriented programs. The programs are compiled into
Java Is Distributed the Java Virtual Machine code called
bytecode. The bytecode is machine-
Java Is Interpreted
independent and can run on any machine
Java Is Robust that has a Java interpreter, which is part of
Java Is Secure the Java Virtual Machine (JVM).
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
51
Characteristics of Java
Java Is Simple Java compilers can detect many problems
Java Is Object-Oriented that would first show up at execution time
Java Is Distributed in other languages.
Java Is Interpreted
Java has eliminated certain types of error-
Java Is Robust prone programming constructs found in
Java Is Secure other languages.
Java Is Architecture-Neutral
Java Is Portable
Java has a runtime exception-handling
feature to provide programming support
Java's Performance
for robustness.
Java Is Multithreaded
Java Is Dynamic
52
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java implements several security
Java Is Secure mechanisms to protect your system against
Java Is Architecture-Neutral
harm caused by stray programs.
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
53
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable Write once, run anywhere
Java's Performance
Java Is Multithreaded With a Java Virtual Machine (JVM),
Java Is Dynamic you can write one program that will
run on any platform.
54
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Because Java is architecture neutral,
Java Is Multithreaded
Java programs are portable. They can
Java Is Dynamic be run on any platform without being
recompiled.
55
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java’s performance Because Java is
Java Is Multithreaded
architecture neutral, Java programs
Java Is Dynamic are portable. They can be run on any
platform without being recompiled.
56
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic Multithread programming is smoothly
integrated in Java, whereas in other
languages you have to call procedures
specific to the operating system to enable
multithreading.
57
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java was designed to adapt to an evolving
Java Is Dynamic environment. New code can be loaded on the
fly without recompilation. There is no need for
developers to create, and for users to install,
major new software versions. New features can
be incorporated transparently as needed.
58
A Simple Java Program
Welcome
Run
59
Creating and Editing Using NotePad
To use NotePad, type
notepad Welcome.java
from the DOS prompt.
60
Creating and Editing Using WordPad
To use WordPad, type
write Welcome.java
from the DOS prompt.
61
Creating, Compiling, and Running
Programs
Create/Modify Source Code
Result
62
If runtime errors or incorrect result
Compiling Java Source Code
You can port a source program to any machine with appropriate compilers.
The source program must be recompiled, however, because the object
program can only run on a specific machine. Nowadays computers are
networked to work together. Java was designed to run object programs on
any platform. With Java, you write the program once, and compile the source
program into a special type of object code, known as bytecode. The bytecode
can then run on any computer with a Java Virtual Machine, as shown below.
Java Virtual Machine is a software that interprets Java bytecode.
Java Bytecode
Java Virtual
Machine
Any
Computer
63
Trace a Program Execution
Enter main method
64
Trace a Program Execution
Execute statement
65
Trace a Program Execution
67
Compiling and Running Java from
TextPad
See Supplement II.A on the Website for details
68
Sample Problems:
Write a program to display “Hello World”
Write a program to find the addition, subtraction,
multiplication and division of two integers A and B.
Write a program to read a length in inch scale and
convert it in the centimeter scale.
Write a program to convert a temperature reading in
degree Fahrenheit to degree Celsius scale using the
formula: C = (5/9)* (F-32)
Write a program to read the radius of a circle and
calculate its area and circumference.
X, Y, Z are the marks of a student. Write a program to
find the total and average marks of the student.
69