3. 1.1. Overview of OO principles
Overview of programming paradigm
• Programming paradigm is a way of conceptualizing
what it means to perform computation and how tasks
to be carried out and organized on a computer.
• Common programming paradigm
i. Unstructured Paradigm
ii. Structured (Procedural) Paradigm
iii. Object Oriented Paradigm
3
4. ii. Structured Programing
Problem solving would involve the analysis of processes in
terms of the procedural tasks carried out and the
production of a system whose representation is based on
the procedural flow of the processes.
data is separate from code.
programmer is responsible for organizing everything in to
logical units of code/data
4
5. A procedural program is divided into functions, and (ideally, at least) each function
has a clearly defined purpose & a clearly defined interface to the other functions in
the program.
Programs with Structural Programming
1. Unrestricted Access
Functions have unrestricted access to global data.
2. Real-World Modeling
Unrelated functions and data, the basics of the procedural paradigm, provide a
poor model of the real world.
3. Difficult of Creating New DataTypes
Traditional languages are not extensible because they will not let you create new
data types.
5
6. iii. OOP Approach
A modern programming paradigm that allows the Programmer
to model a problem in a real-world fashion as an object.
Major objective is to eliminate some of the unfavorable features
encountered in the procedural approach.
Ties data more closely to the functions that operate on it and
protects it from unintentional modification by other functions.
Allows us to decompose a problem into a number of entities
called objects and then build data and functions (methods)
around them.
6
7. OOP Concepts
In object-oriented programming (OOP), programs are
organized into objects
The properties of objects are determined by their
class , i.e a collection of objects will form a
class.
Objects act on each other by passing messages
7
8. Object
Definition: An object is a software bundle that has State
and Behavior. It is an instance of a class.
Software Objects are often used to model real-world
objects.
Example: Dogs have:
States (name, color, hungry, breed) and
Behaviors (bark, fetch, and wag tail).
8
9. Object Examples
Example 1: Dogs
States: name, color, breed, and “is hungry?”
Behaviors: bark, run, and wag tail
Example 2: Cars
States: color, model, speed, direction
Behaviors: accelerate, turn, change gears
9
10. OOP’s fundamental building blocks:
I. Object
II. Class
III. Inheritance
IV. Polymorphism
V. Data Abstraction and Encapsulation
10
11. Class
Definition:A class is a blueprint that defines the
states and the behaviors common to all objects of a
certain kind.
In the real world, you often have many objects of the
same kind. For example, a guard dog, snoop dog . . .
Class is a user defined data type.
11
12. Abstraction
Abstraction refers to the act of representing essential
features without including the background details.
The abstraction concept is usually used during
analysis:
i.e deciding only with application domain, not making
design and implementation decision.
Two popular abstraction:
Procedure abstraction
Data abstraction
Abstraction in Java refers to hiding the implementation
details of a code and exposing only the necessary
information to the user. It provides the ability to simplify
complex systems by ignoring irrelevant details and reducing
complexity.
12
13. Procedure abstraction- is to divide a single problem
into many simple sub-tasks.
Data abstraction is to collect essential elements
composing to a compound data.
Objective:-
Reusable
replaceable
13
Phone
Properties:
Manufacturer
Type
Opening
mechanisms
Abstraction...
14. Encapsulation
It refers to the process of combining data and methods
into a single unit called class.
By this method one cannot access data directly.
Data is accessible only through the methods present
inside the class.
Data Encapsulation provides data hiding.
Each object-oriented object has a shield round it.
14
15. Inheritance
It is the process of deriving new classes from existing
classes.
The derived classes contain:-
all attributes and functions of existing classes plus
its own attributes and functions.
Existing class is called as Base class or Super class
and inherited class is called as Derived class or
Sub class.
15
17. Polymorphism
It means the ability to take more than one form.
To allow an entity such as variable, function or an
object to have more than one form.
Kinds of Polymorphism
-A variable with a given name may be allowed to
have different forms.
- E.g: a variable named EmployeeId can be either
Int, or String.
17
18. Polymorphism Cont’d...
- A named function can also vary depending on the
parameters it is given.
- The number and name of parameters may differ.
- Function name is the same.
- Example: Area(int height,int width) for rectangle and
Area(double width) for square.
18
19. Benefits of OOP
Reusability: Functions and modules that are written by
a user can be reused by other users without any
modification.
Inheritance: Through this, we can eliminate redundant
code and extend the use of the existing classes.
Data hiding: The programmer can hide the data and
functions in a class from other classes.
It helps the programmer to build secured programs.
19
20. Reduces complexity of problems: the given problem
can be viewed as a collection of different objects.
Each object is responsible for a specific task.
Easy to maintain and upgrade: new objects can be
created with small differences to the existing ones.
Message passing: the techniques of message
communication b/n objects makes the interface with
the external system easier.
20
Benefits of OOP...
21. Applications of OOP
Data comm. And computer networking
Simulation and modelling (network simulator)
Object-Oriented database
Computer games and computer animation
Artificial Intelligence
Web based programming
Computer network security
......
21
22. Object-oriented paradigm features
• Emphasis is on data rather than procedure
• Programs are divided into what are known as objects
• Methods that operate on the data of an object are tied together in the data structure
• Data is hidden and can’t be accessed by external functions
• Objects may communicate with each other through methods
• New data and methods can be easily added whenever necessary
• Follows bottom-up approach in program design
• First, the individual base elements of a program are specified (Objects with their data structures
and methods)
• Then they are linked together to form larger subsystems (using algorithm)
• Structured approach is top-down (algorithm comes first, then the data structure)
22
23. Basic concepts of OOP
Objects and Classes
A. Object: is a software bundle that has State and Behavior and it
occupies memory
Example: dogs have states (name, color, hungry, breed) and behaviors (bark, fetch,
and wag tail).
• Software Objects are often used to model real-world objects.
B. Class: is the template or blueprint that defines the states and the
behaviors common to all objects of a certain kind.
• It is a collection of objects of similar type.
• Classes are user-defined data types & behave like the built-in types of programming
language.
23
24. Message
Software objects interact and communicate with each other by
sending messages to each other.
The process of programming in object-oriented language, therefore,
involves the following three basic steps:
i. Creating classes that define objects and their behavior.
ii. Creating objects from class definitions.
iii. Establishing communication among objects.
• A message for an object is a request for execution of a procedure,
and therefore will invoke a method (procedure) in the receiving
object that generates the desired result.
24
25. I. Data Abstraction & Encapsulation
The wrapping up of data and methods into a single unit (called
class) is known as encapsulation.
This insulation of the data from direct access by the program is
called data hiding.
Abstraction refers to the set of representing essential features
without including the background details or explanations
Classes use the concept of data abstraction, and they are known
as Abstract Data Types (ADT)
They hide the private data structure details and communicate
with the outside using public methods which act as interfaces
25
26. II. Inheritance
• Is the process by which objects of one class acquire the properties of
objects of another class.
• It provides the idea of reusability
III. Polymorphism
• Is the ability to take more than one form.
• Plays an important role in allowing objects having different internal structures to
share the same external interface.
Three types of polymorphism:
Overloading methods
Overriding methods, and
Dynamic method binding
26
27. Overloaded methods: methods with the same name
signature but either a different number of parameters or
different types in the parameter list
Overridden methods are methods that are redefined
within an inherited or subclass
They have the same signature and the subclass definition is used
Dynamic binding means that the code associated with
a given procedure call is not known until the time of the
call at runtime.
Memory is allocated at runtime not at compile time.
27
28. Object Orientation
is a set of tools and methods that enable software engineers to
build reliable, user friendly, and maintainable, well documented,
reusable software systems that fulfills the requirements of its users.
It is claimed that object-orientation provides software developers
with new mind tools to use in solving a wide variety of problems.
A software system is seen as a community of objects that
cooperate with each other by passing messages in solving a
problem.
28
29. Summary of OOP
When writing an object-oriented program, we define
classes, which describe categories of objects, and the
states and behaviors that they have in common.
We then create objects which belong to classes, and
share the common features of their class.
Objects interact with each other by passing
messages.
You will be creating your own classes and objects
soon!
29
31. Java Overview
Java is a general purpose, object-oriented programming language
developed by Sun Microsystems of USA in 1991.
The java object-oriented programming framework promotes reusability
of software and code.
The Java foundation class libraries provide for windowing and
graphical user interface programming, network communications,
and Multimedia facilities. Together, they demonstrate the practical and
productive work done in Java.
31
32. Java Features
I. Compiled and Interpreted
Java combines both these approaches thus makes it a two-
stage system.
Java compiler translates the source code into bytecode
instructions
Java interpreter generates the machine code that can be
directly executed by the machine that is running the java
program.
32
33. 33
Run-time Environment
Compile-time Environment
Java
Interpreter
Just in
Time
Compiler
Runtime System
Operating System
Hardware
Java
Virtual
machine
Java
Bytecodes
move locally
or through
network
Java
Source
(.java)
Java
Compiler
Java
Bytecod
e
(.class )
Class
Loader
Bytecode
Verifier
Java
Class
Libraries
How it Works?
34. Java Virtual Machine (JVM)
Java compiler produces an intermediate code known as bytecode
for a machine that does not exist. This machine is known as Java
Virtual Machine (JVM) & it exists only inside computer memory.
It is a simulated computer within the computer and does all major
functions of the real computer.
The JVM is responsible in interpreting bytecode to machine
understandable code (Machine code).
Just In Time compiler (JIT), part of the JVM, is responsible for
compiling code as it is needed, during execution.
34
35. II. Platform independent and Portable
“compile once and execute many times in many platforms”
Most significant contribution of java over other languages is its
portability. i.e. Java programs can be easily moved from one
computer system to another, anywhere and anytime.
Changes and upgrades in operating systems, processors and system
resources will not force any changes in java programs.
Java ensures portability in two ways:
i. Java compiler generates bytecode instructions that can be implemented
on any machine
ii. The size of the primitive data types are machine-independent
35
36. III. Object-oriented
Java is true object-oriented programming language.
All program code and data reside within objects and classes.
IV. Robust and Secure
It has strict compile time and run-time checking for data types.
It is designed as a garbage-collected language relieving the programmers
virtually all memory management problems.
Garbage collection in Java is the automated process of deleting
code that's no longer needed or used.
It also incorporates the concept of exception handling which captures
serious errors and eliminates any risk of chasing the system.
Java systems ensure that no viruses are communicated within applet
programs
36
37. V. Distributed
Java is designed as a distributed language for creating applications on
networks.
It has the ability to share both data and programs.
Java applications can open and access remote objects on Internet as
easily as they can do in a local system.
VI. Simple, Small and Familiar
Java is a small and simple language even easier than C and C++.
Java doesn’t use pointers, preprocessor header files, goto statement and
many others. To make the language look familiar to the existing
programmers, java uses many constructs of C and C++.
37
38. VII. Multithreaded and interactive
Multithreaded means handling multiple tasks simultaneously.
Java supports multithreaded programs:We don’t need to wait for the application to
finish one task before beginning another task.
The java runtime comes with tools that support multiprocessor synchronization and
construct smoothly running interactive systems.
VIII. Dynamic and Extensible
Java allocates memory at runtime not at compile time.
Java is capable of dynamically linking in new class libraries, methods, and objects. Java
program supports functions written in other languages such as C and C++. Such
functions are know as
native methods
Native methods are linked dynamically at runtime.
38
39. Disadvantages of Java
Running bytecode through the interpreter is not as fast as
running machine code, which is specific to that platform.
Because it is platform independent, it is difficult to use
platform specific features (e.g.,Windows taskbar, quick launch)
in Java.
Java interpreter must be installed on the computer in order to
run Java programs.
39
40. Java Environment
Java environment includes a large number of development tools and
hundreds of classes and Methods.
The development tools are part of the system known as Java
Development Kit (JDK) and the classes and methods are part of the
Java Standard Library (JSL), also known as Application Programming
Interface (API).
JDK comes with a collection of tools that are used for developing
and running java programs:
appleviewer (for viewing java applets )
javac (java compiler)
java (java interpreter)
javap (java disassembler)
javah (for C header files)
javadoc (for creating HTML documents)
jdb (Java debugger)
40
41. Java API
It includes hundreds of classes and methods grouped into several
functional packages.
Most commonly used packages are:
Language Support Package: a collection of classes and methods required
for implementing basic features of java.
Utilities Package: a collection of classes to provide utility functions such as
date and time functions.
Input/output Package: a collection of classes required for input/output
manipulation.
Networking Package: a collection of classes for communicating with other
computers via Internet.
AWT Package (Abstract Window Tool kit package): contains classes that
implements platform-independent graphical user interface.
Applet Package: includes set of classes that allows us to create java applets.
41
42. Java is a general-purpose, object-oriented programming language.
We can develop two types of Java programs namely:
Stand-alone application
Web applets
Executing stand-alone Java program involves two steps:
• Compiling source code into byte code javac compiler.
• Executing the bytecode program using java interpreter.
Applets are small programs developed for Internet applications.
An applet located on a distant computer (server) can be
downloaded via Internet and executed on a local computer
(client) using a Java-capable browser.
42
44. Simple Java Program
The simplest way to learn a new language is to write a few
simple example programs and execute them.
public class Sample
{
public static void main (String args [])
{
System.out.print(“Hello world”);
}
}
44
45. Let us discuss the program line by line:
• Class Declaration: the first line class Sample declares a class,
which is an object constructor. Class is keyword and declares a
new class definition and Sample is a java identifier that specifies
the name of the class to be defined.
• Opening Brace “{“: Every class definition in java begins with an
opening brace and ends with a closing brace “}”.
• The main line: the third line public static void main(String args[])
defines a method named as main.
• Is the starting point for the interpreter to begin the
execution of the program.
45
46. Note:
A java program can have any number of classes but only one of
them must include the main method to initiate the execution.
Java applets will not use the main method at all.
The third line uses a number of keywords: public, static and void
Public: is an access specifier that declares the main method as
“unprotected” and therefore making it accessible to all other
classes.
static: declares that this method as one that belongs to the
entire class and not a part of any objects of the class.
Main must always be declared as static since the interpreter
uses this method before any objects are created.
void : states that the main method does not return any value
(but prints some text to the screen.)
46
47. Contd.
All parameters to a method are declared inside a pair of
parenthesis. Here, String args[ ] declares a parameter named
args, which contains array of objects of the class type String.
The Output Line: The only executable statement in the
program is System.out.println(“Java is better than C++.”);
This is similar to cout<< constructor of C++.
The println method is a member of the out object, which is a
static data member of System class.
47
48. Java Program Structure
48
Documentation Section
Package Statement
Import Statements
Interface Statements
Class Definitions
Main Method class
{
Main Method Definition
}
Suggested
Optional
Optional
Optional
Optional
Essential
49. Documentation Section
comprises a set of comment lines giving the name of the
program, the author and other details, which the programmer
would like to refer at a later stage.
Java supports three types of comments:
i. Single line comment //
ii. Multiple line comment /*………………
………………*/
iii. Documentation comment /**….*/
• This form of comment is used for generating
documentation automatically.
49
50. Package Statement
Is the first statement in Java file and is optional.
It declares a package name and informs the compiler that the
classes defined here belong to this package.
Example: package student;
import statements
Next to package statements (but before any class definitions) a
number of import statements may exist. This is similar to #include
statements in C or C++.
Using import statements we can have access to classes that are
part of other named packages.
Example: import java.lang.Math;
50
51. interface Statements
An interface is like a class but includes a group of method
declarations.
is also an optional section.
is used only when we wish to implement the multiple inheritance
features in the program
Class Definitions
A Java program may contain multiple class definitions.
Classes are the primary and essential elements of a Java program.
These classes are used to map objects of real-world problems.
The number of classes depends on the complexity of the problem.
51
52. Main Method Class
Since every Java stand-alone program requires a main method as
its starting point, this class is the essential part of a Java program.
A simple Java program may contain only this part.
The main method creates objects of various classes and
establishes communications between them.
On reaching the end of main, the program terminates and control
passes back to the operating system.
52
53. Let us discuss the program line by line:
• Class Declaration: the first line class Sample declares a class,
which is an object constructor. Class is keyword and declares a
new class definition and Sample is a java identifier that specifies
the name of the class to be defined.
• Opening Brace “{“: Every class definition in java begins with an
opening brace and ends with a closing brace “}”.
• The main line: the third line public static void main(String args[])
defines a method named as main.
• Is the starting point for the interpreter to begin the
execution of the program.
53
54. Note:
A java program can have any number of classes but only one of
them must include the main method to initiate the execution.
Java applets will not use the main method at all.
The third line uses a number of keywords: public, static and void
Public: is an access specifier that declares the main method as
“unprotected” and therefore making it accessible to all other
classes.
static: declares that this method as one that belongs to the
entire class and not a part of any objects of the class.
Main must always be declared as static since the interpreter
uses this method before any objects are created.
void : states that the main method does not return any value
(but prints some text to the screen.)
54
55. Contd.
All parameters to a method are declared inside a pair of
parenthesis. Here, String args[ ] declares a parameter named
args, which contains array of objects of the class type String.
The Output Line: The only executable statement in the
program is System.out.println(“Java is better than C++.”);
This is similar to cout<< constructor of C++.
The println method is a member of the out object, which is a
static data member of System class.
55
56. Package Statement
Is the first statement in Java file and is optional.
It declares a package name and informs the compiler that the
classes defined here belong to this package.
Example: package student;
import statements
Next to package statements (but before any class definitions) a
number of import statements may exist.This is similar to #include
statements in C or C++.
Using import statements we can have access to classes that are
part of other named packages.
Example: import java.lang.Math;
56
57. interface Statements
An interface is like a class but includes a group of method
declarations.
is also an optional section.
is used only when we wish to implement the multiple inheritance
features in the program
Class Definitions
A Java program may contain multiple class definitions.
Classes are the primary and essential elements of a Java program.
These classes are used to map objects of real-world problems.
The number of classes depends on the complexity of the problem.
57
58. Main Method Class
Since every Java stand-alone program requires a main method as
its starting point, this class is the essential part of a Java program.
A simple Java program may contain only this part.
The main method creates objects of various classes and
establishes communications between them.
On reaching the end of main, the program terminates and control
passes back to the operating system.
58
59. 5%
Assignment One
1. What are the main features of object oriented
programming. (1.5 pt)
2. What is the main difference between Java and C++.(1.5
pts)
3. Install netbeans or eclipse on your computer and write
a java program to display (2 pts)
“Hello every on I am [Your full name ]”
59