Week 1 Sessions 1-2 Introduction To OOP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Object-Oriented Programming

TOPIC TITLE: Introduction to OOP

Specific Objectives:

At the end of the topic session, the students are expected to:

Cognitive:

1. Identify the approaches in writing computer programs.


2. Define what OOP is.
3. Describe each concept of OOP.

Affective:

1. Listen to others with respect.


2. Participate in class discussions actively.

Psychomotor:

1. Create a Java application based on the previous lessons


learned.

MATERIALS/EQUIPMENT:

o topic slides
o OHP

TOPIC PREPARATION:

o Have the students research on the following:


Definition of OOP
Objects and Classes
Inheritance
Encapsulation
Polymorphism
o Prepare additional examples on the topic presented.
o It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics. The
instructor may use the suggested learning activities below to
facilitate a thorough and creative discussion of the topic.
o Prepare the slides to be presented in class.

TOPIC PRESENTATION:

The topic will introduce the students to the conceptual foundation of


object-oriented programming.

This will be the suggested flow of discussion for the course topic:

1. Call on students to report on their research on OOP.


2. Ask the students if they know any programming paradigm.
3. Discuss the difference between procedural programming and
OOP.
4. Explain the sample figure illustrated in the slides on OOP.
5. Discuss each advantage of OOP over conventional approaches.
6. Discuss what objects and classes in OOP are.
7. Discuss a brief concept of abstraction.
8. Explain what encapsulation, inheritance, and polymorphism in
OOP are.
9. Generally explain what abstract classes and interfaces in OOP

Introduction to OOP Page 1 of 8


Object-Oriented Programming

concept are.

Programming Paradigm Programming Paradigm


Page 1 of 15
There are two most common approaches to writing computer programs.
These are:

Procedural programming
Object-oriented programming

Procedural programming is a style of programming in which the


programming task is broken down into a series of operations (called
procedures) applied to data (or data structures). Examples of
procedural programming languages are C and Pascal.

On the other hand, object-oriented programming breaks down a


programming task into a series of interactions among different entities or
objects. This may be considered as an extension of procedural
programming in that the actions performed by each object are specified
by procedures. Examples of languages that support OOP are Java,
C++, and Smalltalk.

Note that Java and C++ are multi-paradigm programming languages,


i.e., these support multiple programming paradigms or styles.

[Programming Paradigm, Page 1 of 15]

Object-Oriented Programming Object-Oriented Programming


Pages 2-5 of 15
Object-oriented programming or OOP is a type of programming in which
programmers define not only the data structures, but also the types of
operations (methods) that can be applied to the data structure. The data
structure becomes an object that includes both data and methods.

A major advantage of OOP is code reusability. OOP enables


programmers to create modules that do not need to be changed when a
new type of object is added. A new object can simply be created that
inherits many of its features from existing objects. This makes object-
oriented programs easy to understand and modify.

OOP has become the most widely used paradigm. Instead of focusing
on what the system has to do, OOP focuses on what objects the system
contains and how they interact towards solving the programming
problem.

To illustrate further what an OOP is, we will cite an example (refer to the
slide). A car is an object that uses speed and direction to transport
people to a different location. This car object encapsulates all the
functions that it needs to get its job done. It has a switch to turn it on, a
wheel to control its direction, and brakes to slow it down. These
functions directly manipulate the car’s data, including direction, position,
and speed.

Introduction to OOP Page 2 of 8


Object-Oriented Programming

Illustration of OOP

When you travel in a car, however, you don’t have to know the details of
how these operations work. To stop a car, for example, you simply step
on the brake pedal. You don’t have to know how the pedal stops the car.
You simply know that it works.

All these functions and data work together to define the object called a
car. Furthermore, all these functions work very similarly from one car to
the next. You’re not likely to confuse a car with a dishwasher, tree, or
playground. A car is a complete unit — an object with unique properties.

OOP has the following advantages over conventional approaches:

It provides a clear modular structure for programs which makes


it good for defining abstract data types where implementation
details are hidden and the unit has a clearly defined interface.
It makes it easy to maintain and modify existing code as new
objects can be created with small differences from existing ones.
It provides a good framework for code libraries where supplied
software components can be easily adapted and modified by the
programmer. This is particularly useful for developing graphical
user interfaces.

Object-oriented programming enables you to think of program elements


as objects. The six key concepts of OOP include:

Objects
Classes
Abstraction
Inheritance
Encapsulation
Polymorphism

[Object-Oriented Programming, Pages 2-5 of 15]

Introduction to OOP
Page 3 of 8
Object-Oriented Programming

Objects and Classes Objects and Classes


Page 6 of 15
The two most important concepts in object-oriented programming are
the class and the object. In its simplest form, an object is anything that
models “things” in the real world. These “things” may be physical entities
such as cars, or events such as a concert, or abstractions such as a
general-purpose account. Objects are made up of attributes and
methods.

Attributes are the characteristics that define an object. The attributes of


an object are also referred to as its states. They differentiate objects of
the same class type. For example, your car’s attributes include its
make, model, year, and purchase price. All cars can possess the same
attributes, but not necessarily the same values for all those attributes.

Aside from attributes, objects have abilities or methods. Objects use


these methods to accomplish tasks. Programmatically, a method is a
self-contained block of program code that is similar to a procedure or
function. For example, a car can move forward and backward, can be
filled with gasoline, or be washed. Methods can also ascertain
attributes, such as the current speed of a car and the current status of its
gas tank (how much gas it contains). Different cars may have different
ways of performing these abilities.

As an analogy, in grammar, a noun is equivalent to an object and an


adjective is an attribute, and verbs are methods.

A class is a term that describes a group or collection of objects with


common properties (both attributes and methods). A class is an abstract
description of an object – a general plan or blueprint for an object. An
object is created from a class by a process called instantiation. An
object is then said to be an instance of a class. Thus, your blue Honda
Car with the dent can be considered an instance of the class that is
made up of all cars.

The class implementation specifies the data structure for each of its
objects. For example, an Employee class might include data about
exemptions, position, salary, phone extension, and so on. In addition,
each class defines a set of permissible operations that allow access to
and modification of object data. An Employee class might include
operations such as hire, promote, and change phone extension for an
Employee class. Details of the class include the specification that
describes an operation.

[Objects and Classes, Page 6 of 15]

Introduction to OOP
Page 4 of 8
Object-Oriented Programming

Abstraction Abstraction
Page 7 of 15
In OOP, abstraction is one of three central principles along with
encapsulation and inheritance. Abstraction is a programming feature
that allows a programmer to hide all but the relevant information (to the
problem at hand) about an object in order to reduce complexity and
increase efficiency. A programmer would use abstraction, for example,
to note that two functions perform almost the same task and can be
combined into a single function. For example, your Honda car may be
treated as a common car most of the time (e.g., when driving the car), as
a Honda car when attributes specific to Honda cars are required (e.g.,
when replacing specific components/parts), or as your specific Honda
car (e.g., when selling the car).

Abstraction is closely related to two other important techniques


(encapsulation and information hiding).

[Abstraction, Page 7 of 15]

Encapsulation Encapsulation
Pages 8-9 of 15
Encapsulation refers to the hiding of data (attributes) and methods within
an object. It protects an object’s data from corruption. If all programs
could access the data in any way users wished, the data could easily be
corrupted or misused. Encapsulation protects the object’s data from
arbitrary and unintended use.

Encapsulation also hides the details of an object’s internal


implementation from the users of an object. Users understand what
operations (methods) may be requested of the object but, in most cases,
need not know the details of how the operation is performed. All the
specifics of the object’s data and the coding of its operations are tucked
out of sight.

Encapsulation is important, because it separates how an object behaves


from how it is implemented. This allows object implementations to be
modified without requiring the applications that use them to be modified
also.

It is easier to modify programs using encapsulation, because one object


type is modified at a time. If an object type is changed, only the
operations and data structures associated with that object type are
affected and usually only some of those operations and data structures.
The object type’s behavior can be changed and tested, independently of
other object types.

Illustration of encapsulation

Introduction to OOP
Page 5 of 8
Object-Oriented Programming

The diagram illustrated in the slide gives a good visualization on what


encapsulation is all about. It gives the programmer a freedom to
combine attributes (variables) and methods into a single logical unit.
The programmer will be able to simply declare a simple identifier having
the ability to summon methods and change the value of variables
(attributes) within them. For example, the user does not need to know
how the name is stored internally. The user just needs to know the
limitations, e.g., what characters are acceptable and how long the name
can be. The programmer can then later implement a data compression
method on the name to reduce the storage space used just keeping in
mind the agreed limitations on the name.

[Encapsulation, Pages 8-9 of 15]

Inheritance Inheritance
Pages 10-11 of 15
Inheritance is the process by which objects can acquire (inherit) the
properties of objects of another class. In OOP, inheritance provides
reusability. Additional features may be introduced into an existing class
without modifying it. This is achieved by deriving a new class, called a
subclass, from the existing one. The new class will then have the
combined features of both the classes.

For example, consider a Car class that defines everything common to all
cars. A Convertible subclass may then be defined from the Car class. A
Convertible then inherits properties from the Car class. That is, a
Convertible is a Car that has and can do everything a plain Car does.
This, however, has added mechanisms for and an added ability to lower
its top. Note that a Convertible is not an object. It is a class that inherits
from the Car class.

Illustration of inheritance

Another example would be a Student class having Name and Student ID


as its attributes and getID(), getName(), setID() and setName() as its
methods. We can assume that a student will eventually become a
graduate student. Therefore, a graduate student class can be created
from the Student class. When the class Student is inherited, its
attributes and methods will then be copied into the inheriting class.

[Inheritance, Pages 10-11 of 15]

Introduction to OOP
Page 6 of 8
Object-Oriented Programming

Polymorphism Polymorphism
Pages 12-13 of 15
Polymorphism refers to the ability to process objects differently
depending on their data type or class. Specifically, it is the ability to
redefine methods for derived classes. For example, given a base class
Shape with a method to return the area of the shape. Polymorphism
enables the programmer to define different area methods for any
number of derived classes from Shape, such as circles, rectangles, and
triangles. No matter what shape an object is, applying the area method
to it will return the correct results.

One of the strength of polymorphism is that a request for an operation


can be made without knowing which specific method should be invoked.
These implementation details are hidden from the user. Instead, the
responsibility rests with the selection mechanism of the OO
implementation.

Illustration of polymorphism

In the given figure (refer to the slide), both Student and Car have an
instance existing in class Application Form. Notice that both Student
class and Car class have the isRegistered() method. However, the way
a student registers is different from the way a car registers. For
instance, when a student number is required in the process when a
student registers. On the other hand, the registration of a car does not
need information such as a student number but rather say the chassis
and engine numbers.

[Polymorphism, Pages 12-13 of 15]

Abstract Classes Abstract Classes


Page 14 of 15
An abstract class is a class that is not used to create (instantiate)
objects. It is designed to act as a base class (to be inherited by other
classes). The abstract class is a design concept in program
development and provides a base upon which other classes are built.
After declaring an abstract class, it cannot be instantiated on its own. It
must be used in creating new classes making its members (attributes
and methods) inherited. Abstract classes can only specify members
that should be implemented by all inheriting classes.

For example, you can create an abstract class Shape which will be the
base class of different polygons. All members of the class Shape will be
inherited by the subclasses created from Shape. However, you cannot
create an object directly from the abstract class Shape.

[Abstract Classes, Page 14 of 15]

Introduction to OOP
Page 7 of 8
Object-Oriented Programming

Interfaces Interfaces
Page 15 of 15
Interfaces allow you to create definitions for component interaction.
They provide another way of implementing polymorphism. Through
interfaces, you specify methods that a component must implement
without actually specifying how the method is implemented. You just
specify the methods in an interface and leave it to the class to implement
those methods.

In simple terms, an interface just specifies what methods should be


defined in a class that implements the interface. A class implementing
an interface will then have to define all the methods specified by the
interface.

[Interfaces, Page 15 of 15]

EVALUATION/GENERALIZATION:

o Ask the students to write a brief essay outlining what they have
learned from the topic discussed.
o Ask the students to perform the laboratory exercise.

REFERENCES:

Wu, C. Thomas, (2004), An introduction to object-oriented


rd
programming with java (3 ed.), McGraw Hill
Farrell, Joyce, (2006), Java programming (3rd ed.), Thomson Course
Technology
Retrieved, August 24, 2007, www.webopedia.com
Retrieved, August 28, 2007,
http://java.sun.com/docs/books/tutorial/java/concepts/object.html

Introduction to OOP
Page 8 of 8

You might also like