14
Process oriented model OOP
Organizes a program around its code. Organizes a program around its data (that is, objects).
A program is written as a series of linear A program is written as a collection of classes and
steps. objects
Program contains procedures. Program contains classes, objects.
Ex: C uses this approach Ex: Java uses this approach.
Procedure oriented model Object oriented model
OOP principles / components
Definition: OOP is a programming methodology that helps organize complex programs
through the use of inheritance, encapsulation, and polymorphism.
Components:
1. Classes
2. Objects
3. Abstraction
15
4. Encapsulation (Data Encapsulation)
5. Inheritance
6. Polymorphism
7. Access Levels
8. Interfaces
9. Packages
Three OOP Principles
1. Encapsulation
2. Inheritance
3. Polymorphism
Class: A class defines the structure and behavior (data and code) that will be shared by a
set of objects. A class is a structure that defines the data and the methods (functions in
other languages) to work on that data.
In Java, all program data is wrapped in a class.
import [Link];
class ExampleProgram
{
public static void main(String[] args)
{
[Link](“Hello World");
}
}
Object: An instance of a class is called as an Object.
1) There can be any number of objects of a given class in memory at any one time.
2) Software objects are conceptually similar to real-world objects.
3) Objects consist of state and behavior.
4) An object stores its state in fields (variables in some programming languages) and
exposes its behavior through methods (functions in some programming languages).
16
A software object A bicycle modeled as a software object
Abstraction:
a) An essential element of object-oriented programming is abstraction.
b) Abstraction is defined as hiding the complexities of the system.
c) A powerful way to manage abstraction is through the use of hierarchical
abstractions (classifications).
d) Humans manage complexity through abstraction.
e) For example, people do not think of a car as a set of tens of thousands of individual
parts. They think of it as a well-defined object with its own unique behavior.
Hierarchical classifications
CAR
Steering Seat belts
Brakes Sound
Radio MP3 player
CD Player
17
Three OOP Principles
1. Encapsulation:
Encapsulation is the mechanism that binds together code and the data it manipulates,
and keeps both safe from outside interference and misuse.
i. Encapsulation is sometimes called as Information hiding.
ii. In Java, the basis of encapsulation is the class.
iii. Using Access Modifiers (private, public,protected,default), the members of the
class can be protected from misuse by the outside classes.
iv. private members can only accessed by the members of the class.
v. public members can be accessed by any members of the class or outside members
of the class.
vi. Programs should interact with object data only through the object’s methods.
vii. Benefits of Encapsulation are: reuse and reliability.
Example:
18
2. Inheritance:
Inheritance is the process by which one object acquires the properties of another object.
i. Inheritance defines relationships among classes in an object-oriented language.
ii. Inheritance allows classes to inherit commonly used state and behavior from other
classes.
iii. It supports the concept of hierarchical classification.
iv. Benefits are : Code reuse and Decrease of Program size
v. In the Java programming language, all classes descend from [Link], that
is, Object is the superclass for all classes.
Example:
a) Different kinds of objects often have a certain amount in common with each other.
Mountain bikes, road bikes, and tandem bikes, for example, all share the
characteristics of bicycles (current speed, current pedal cadence, current gear).
b) Yet each also defines additional features that make them different: tandem bicycles
have two seats and two sets of handlebars; road bikes have drop handlebars; some
mountain bikes have an additional chain ring, giving them a lower gear ratio.
Superclass
subclasses
19
Syntax for creating subclasses:
The syntax for creating a subclass is simple. At the beginning of your class declaration, use
the extends keyword, followed by the name of the class to inherit from:
class MountainBike extends Bicycle { Also contains the same fields and
methods as Superclass Bicycle,
// new fields and methods defining but that code will not appear.
// a mountain bike would go here
Subclass = Same attributes of super class + any attributes of subclass
➢ In the Java programming language, each class is allowed to have one direct
superclass, and each superclass has the potential for an unlimited number
of subclasses.
3. Polymorphism: Polymorphism in Greek, meaning “many forms”.
Definition:
Polymorphism in java is a concept by which we can perform a single action by
different ways.
a) In JAVA, More generally, the concept of polymorphism is often expressed by the
phrase “one interface, multiple methods.”
b) We can perform polymorphism in java by method overloading and method
overriding.
Types of Polymorphism:
There are two types of polymorphism in java:
1. Compile time polymorphism and
2. Runtime polymorphism.
Example
i. You might have a program that requires three types of stacks.
ii. One stack is used for integer values, one for floating point values, and one for
characters.
iii. The algorithm that implements each stack is the same, even though the data
being stored differs.
iv. In a non–object-oriented language, you would be required to create three
different sets of stack routines, with each set using different names.