Ceng325 6 Oop
Ceng325 6 Oop
Basics
1-Why OOP
When you want to fix your car, it is definitely easier to see it as an assembled car
from parts and components, such as such as chassis, doors, engine, wheels, brake
and transmission. These components are typically reusable, e.g., a wheel can be
used in many cars (of the same specifications).
Also, it would be the same for your computer which is composed of: a
motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, etc.
Hardware, such as computers and cars, are assembled from parts, which are
reusable components, but what about software?
Without OOP, it is very difficult to "assemble" an application from software
components. Hence, for each new application, we have to re-invent the wheels and
write program from scratch.
2-What is OOP?
The easiest example to understand the difference between the traditional procedural
orientation and the object orientation is to see the difference between windows Paint
and Office Power-point(PPT). Let us take the following two figures that are made in
paint (figure1-a) and in PPT (figure1.b).
(a) (b)
Figure1: paint and PPT comparing
Now suppose that someone asks you to remove the star from the two figures using
the paint and PPT for each corresponding file. Which process is more complicated?
(a) (b)
Figure2: paint and PPT comparing: removing a part of the figure
Traditional Procedural-Oriented languages
In traditional procedural-oriented programming
languages (such as C, Fortran, Cobol and Pascal)
programs are made up of functions. Functions
are less reusable. It is very difficult to copy a
function from one program and reuse in another
program (referencing global variables and other
functions). Procedural languages are not suitable
of high-level abstraction for solving real life problems.
For example, C programs uses constructs such as if-
else, for-loop, array, method, pointer, which are low-
level and hard to abstract real problems such as a
Customer Relationship Management (CRM) system
or a computer soccer game.
For cars: We need to record, the color, engine size, transmission type and number of
doors.
For the trucks, we need to record also the color, engine size, transmission type, cab size
and towing capacity.
Creation:
Procedural: we create two separated forms one for the car and the other for the
truck in which we repeat the exact code for the color, the engine size and the
transmission type. The two forms will differ in the remaining characters.
OOP: we create a main vehicle class that will be concerned about color, engine
size and transmission type. We then extend that class with a car class that handle
the number of doors as well and a truck class that handles the cab size and the
towing capacity.
Scenario 1: Let us imagine that after developing the system we are asked to add a
bus form in which we need to record color, engine size, transmission type and number of
passengers.
Procedural: we need to recreate the entire form repeating the same code for
color, engine size and transmission type.
OOP: we simply extend the vehicle class with a bus class adding a new method
about the number of passengers.
Scenario 2: For one reason, the client know is asking to email the color of the car to
him directly after saving it to the database.
Procedural: we will need to change the three different forms, car, truck and bus
to email the color to the client.
OOP: we just change one method in the vehicle class to email the color to the
client and since the three other classes are extended (inherited from) the vehicle
class, so they will be automatically updated.
Scenario 3: The client now asks to add a specific make to each car (e.g. Ford, BMW,
Nissan, etc).
Procedural: We create a new form for each make repeating all the code of the
generic car information and adding the code for each of the car makes.
OOP: we extend the car class with a Nissan class, a BMW class and a Ford class in
which we add the specific information about each of them only to the new classes.
3-OOP in Java
JAVA is an OOP è its main actors are objects
Is supports the following fundamental concepts – Classes, objects, instance,
methods, polymorphism, inheritance, encapsulation.
Objects store data and provide methods for accessing and modifying this data
Object has two main characteristics:
a. State: represents the data (value) of an object ( float, int ,…)
b. Behavior: Methods to manipulate data (create, deposit, withdraw, etc)
Objects of the same type are defined using a common class
All program data is wrapped in a class, whether it is a class you write or a class
you use from the Java platform API libraries (e.g. String class)
An instance is an executable copy of a class. Another name for instance is object.
There can be any number of objects of a given class in memory at any one time.
4- Examples: Java OOP programs
a.Bicycle
There may be thousands of other bicycles in existence, all of the same make
and model. Each bicycle was built from the same set of blueprints so it
contains the same components. In object-oriented terms, we say that your
bicycle is an instance (or an object) of the class of
objects known as bicycles. A class is the blueprint from which individual
objects are created.
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
b.Simple Calculator
instead of instead of putting all the code inside the form, we can create a class called
mymath that will contain all the needed functions and that can be reused in any
other program. We will then only need to include this class in our form class (if it is in
different package) and call its functions there.
public class MyMath {
restext.setText(Double.toString(res));