Anda di halaman 1dari 6

J.E.D.I.

Introduction to Object Oriented


Programming
.1 Objectives
In this section, we will be discussing the fundamentals of object oriented programming,
how one views the problem and solves it under this paradigm, and the basic concepts
that will be crucial to designing and coding object oriented programs.

At the end of the lesson, the student should be able to:

Define what object oriented programming is


Understand how a problem is modeled and solved using the object oriented
approach
Know the key concepts of object oriented programming such as classes and
objects, encapsulation, inheritance, polymorphism, and application programming
interfaces (API)

.2 Introduction
Although it was said that a program consists of commands or instructions relagated to
the computer to perform certain tasks towards the solution to a problem, the way a
solution and code is implemented may change depending on the paradigm adhered to by
the language.

Object-Oriented programming or OOP revolves around the concept of objects as the


basic elements of your programs. When we compare this to the physical world, we can
find many objects around us, such as automobiles, lion, people and so on. These objects
are characterized by their properties (or attributes) and behaviors.

For example, an automobile object has the properties, type of transmission,


manufacturer and color. Its behaviors are turning, braking and accelerating. Similarly,
we can define different properties and behavior of a lion. Please refer to the table below
for the examples.

Object Properties Behavior


Automobile type of transmission
manufacturer
turning
color braking
accelerating
Lion Weight roaring
Color sleeping
hungry or not hunting

Introduction to Programming I 1
J.E.D.I.

Object Properties Behavior

hungry
tamed or wild
Table 1: Example of Real-life Objects

With these descriptions, the objects in the physical


world can easily be modeled as software objects using
the properties as data and the behaviors as
methods. These data and methods could even be
used in programming games or interactive software to
simulate the real-world objects! An example would be
a automobile software object in a racing game or a lion
software object in an educational interactive software
zoo for kids.
.3 Classes and Objects
In the software world, an object is a software component whose structure is similar to
objects in the real world. Each object is composed of a set of data
(properties/attributes) which are variables describing the essential characteristics of the
object, and it also consists of a set of methods (behavior) that describes how an object
behaves. Thus, an object is a software bundle of variables and related methods. The
variables and methods in a Java object are formally known as instance variables and
instance methods to distinguish them from class variables and class methods, which
will be discussed later.

The class is the fundamental structure in object-oriented programming. It can be


thought of as a template, a prototype or a blueprint of an object. It consists of two
types of members which are called fields (properties or attributes) and methods.
Fields specifiy the data types defined by the class, while methods specify the operations.
An object is an instance of the class.

To differentiate between classes and objects, let us discuss an example. What we have
here is an Automobile Class which can be used to define several Automobile Objects. In
the table shown below, Automobile A and Automobile B are objects of the Automobile
class. The class has fields plate number, color, manufacturer, and current speed which
are filled-up with corresponding values in objects Automobile A and Automobile B. The
Automobile has also some methods Accelerate, Turn and Brake.

Automobile Class Object Automobile A Object Automobile B

Plate Number ABC 111 XYZ 123


Variables
Instance

Color Blue Red


Manufacturer Mitsubishi Toyota
Current Speed 50 km/h 100 km/h

Introduction to Programming I 2
J.E.D.I.

Automobile Class Object Automobile A Object Automobile B

Accelerate Method
Instance

Methods
Turn Method
Brake Method

Table 2: Example of Automobile class and its objects

When instantiated, each object gets a fresh set of state variables. However, the method
implementations are shared among objects of the same class.

Classes provide the benefit of reusability. Software programmers can use a class over
and over again to create many objects.

.4 Encapsulation
Going back to the automobile example, we are now aware that a automobile has certain
properties such as plate number and color and methods such as accelerate and break.
Assuming that we know how to use (i.e. drive) it without undergoing usual driving
lessons (which usually entails knowing troubleshooting and understanding a automobile's
underlying mechanics), would you really know how an automobile performs a speed up
when you hit the accelerator? Or how it slows down to a halt when you hit the breaks?
Or how come it is so easy to turn the heavy wheels and axles with little effort by a
simple (well, most of the time) turn of the steering wheel? But nonetheless, we can
drive automobiles regardless if we know the engineering and physics behind the
mechanics. In short, we need not really know the intricacies of the mechanisms of an
automobile in order for us to use it.

In relation to the concept of object oriented programming, although there is that


intention of reusability, there are times that we would want to limit implementation
details on certain aspects of the code. Given this, we would just give them some way to
use certain methods and access certain properties without fully divulging everything in
the program. This particular concept is called encapsulation.

Encapsulation is the method of hiding certain elements of the implementation of a


certain class. By placing a boundary around the properties and methods of our objects,
we can prevent our programs from having side effects wherein programs have their
variables changed in unexpected ways.

We can prevent access to our object's data by declaring them declaring them in a certain
way such that we can control access to them. We will learn more about how
encapsulation can be implemented in Java as we discuss more about classes.

Introduction to Programming I 3
J.E.D.I.

.5 Inheritance
Now, we are also aware that an automobile can come in different colors, have different
plate numbers per unit. Take also into account that the acceleration mechanism may
entail custom details such as the inclusion of nitro and turbo, and the breaks may come
with anti-lock braking systems. But nonetheless, the concept of an automobile having a
certain color and plate number and that it accelerates and brakes is preserved
throughout every make and model of an automobile regardless of the customizations
present. In OOP, this is exhibited in a concept called inheritance.

The concept of inheritance in OOP is exhibited in that it allows a class to be derived


from another class . This mechanism will have the derived class inherit certain properties
and behaviors from the parent class. The parent class from which a class is derived is
termed as a superclass while the derived class is termed as a subclass, and the
derivation process has the superclass be extended to create a subclass.

The advantage of the concept of inheritance allows specialization of classes, i.e. classes
might be based from a superclass and furthermore provide special mechanisms to the
subclass .

.6 Polymorphism
An automobile can be built differently: it can be a car, a van, a jeep, a bus, or even a
truck. This connotes that there are added mechanisms to an automobile depending on
the build. But regardless of whatever automobile build, we can still refer to them as
"automobiles" (although that will be a mouthful and kind of awkward). In fact, they are
automobiles.

The concept of polymorphism comes to mind when we put this in object oriented
perspective. Polymorphism allows objects of subclasses to be treated as an object of
their superclass. As you can see, polymorphism is closely tied to inheritance. Later on,
we will also see that polymorphism will give a superclass object the ability to change
behavior based on what subclass object it is holding.

.7 Application Programming Interfaces (API)

There is not only one autmobile on the road at any given time. In fact, there are too
many nowadays that traffic congestion has become a major headache but that's beside
the point. However, there are certain mechanisms on an automobile that allows the
driver to communicate to other drivers certain actions to avoid inconveniences and
accidents, namely the signal light and the horn. Also, we could also factor in that the

Introduction to Programming I 4
J.E.D.I.

road itself has certain mechanisms to aid drivers: street signs, lamp posts, and traffic
lights to name a few. All of these are given to ensure (somehow) that automobiles
perform seamlessly while on the road.

This is also applicable to the OOP context, you would not usually limit yourself to one
class or one program only. Especially in actual software development, you will have to
use several classes or objects, some of which have been made by other programmers.
This is where the concept of application programming interface or API for short will
really help.

An API is a collection of pre-defined programs that are used to support certain


procedures that cannot be solely carried out by a solitary program. Let it be noted
though that the concept of an API is not unique to OOP, but the way an API is structured
in OOP is certainly different. Later, we will see how we can use certain APIs to help us
create programs in Java, as well as create our very own APIs.

.8 Exercise
.8.1 Humanity, from an object oriented perspective
Let's assume that there is a class called Human. Relate each of these object oriented
concepts as to how they can be applied to our Human class:

1. Properties
2. Behaviors

Introduction to Programming I 5
J.E.D.I.

3. Encapsulation
4. Inheritance
5. Polymorphism
6. API

.8.2 Freestyle OO
Now on your own, think of anything and imagine it as a class. Based on the previous
exercise, relate how each of the six concepts aforementioned can be applied to the class
you have thought of.

Introduction to Programming I 6

Anda mungkin juga menyukai