0% found this document useful (0 votes)
5 views8 pages

Ceng325 6 Oop

The document explains the principles and advantages of Object-Oriented Programming (OOP) compared to traditional procedural programming. It highlights how OOP allows for higher levels of abstraction and reusability through the use of objects, classes, and inheritance, making it easier to manage and extend software applications. Examples in Java illustrate the practical application of OOP concepts, such as creating classes for vehicles and a simple calculator.

Uploaded by

shehabhasan393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Ceng325 6 Oop

The document explains the principles and advantages of Object-Oriented Programming (OOP) compared to traditional procedural programming. It highlights how OOP allows for higher levels of abstraction and reusability through the use of objects, classes, and inheritance, making it easier to manage and extend software applications. Examples in Java illustrate the practical application of OOP concepts, such as creating classes for vehicles and a simple calculator.

Uploaded by

shehabhasan393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Object-oriented Programming (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.

Object-Oriented Programming Languages


Object-oriented programming (OOP) languages are designed to overcome these
problems.

1. Object-oriented programming (OOP) involves programming using objects.


2. An object represents an entity in the real world that can be distinctly identified by
its state ((properties), and its behavior (actions).
3. OOP languages permit higher level of abstraction for solving real-life problems.
The traditional procedural language (such as C and Pascal) forces you to think in
terms of the structure of the computer (e.g. memory bits and bytes, array,
decision, loop) rather than thinking in terms of the problem you are trying to
solve. The OOP languages (such as Java, C++ and C#) let you think in the
problem space, and use software objects to represent and abstract entities of the
problem space to solve the problem.
A real-word example
Supposing a business of a vehicle parts manufacturer that needs to update its inventory
system. You are asked to design a program for two branches, cars and trucks.

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

 Object is an instance of a class  class is the template or the blueprint from


which the objects are created
 Examples:
 Object: House
State: location, color, area, etc.
Behavior: Close/open door, close/open window, repaint the house, etc.
 Object: Car
State: color, make, engine size, transmission type
Behavior: change color, accelerate, slow down, etc.
 To invoke a method on an object is to ask the object to perform an action.

Java classes, objects and instances


In the real world, you'll often find many individual objects all of the same kind.

 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;

void changeCadence(int newValue) {cadence = newValue; }

void changeGear(int newValue) {gear = newValue; }

void speedUp(int increment) {speed = speed + increment; }

void applyBrakes(int decrement) {speed = speed -


decrement; }

void printStates() {System.out.println("cadence:" +


cadence + "
speed:" + speed + " gear:" + gear); }
}
Typically, the Bicycle class does not contain a main method. That's because it's not a
complete application; it's the blueprint for bicycles that might be used in an
application. The responsibility of creating and using new Bicycle objects belongs to
some other class in the application.
class BicycleDemo {
public static void main(String[] args) {

// Create two different Bicycle objects


Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();

// Invoke methods on those objects


bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}

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 {

double add(double a, double b) {


double answer = a + b;
return answer;}

double subtract(double a, double b) {


double answer = a - b;
return answer;}

double multiply(double a, double b) {


double answer = a * b;
return answer;}

double divide(double a, double b) {


double answer = a / b;
return answer;}

double power(double a, double b) {


double answer = a;

for (int x = 2; x <= b; x++) {


answer *= a;}
return answer;}
}

With the same form:


The code of the + button action will be:
Double num1 = Double.parseDouble(num1text.getText());
Double num2 = Double.parseDouble(num2text.getText());
MyMath Maths = new MyMath();
Double res = Maths.add(num1, num2);

restext.setText(Double.toString(res));

You might also like