0% found this document useful (0 votes)
22 views

Design Program Logic - 9

This document provides an overview of object-oriented programming concepts including: 1. Compares procedural and object-oriented programming, noting that OOP is centered on objects that encapsulate both data and functions. 2. Discusses object reusability as a benefit of OOP where pre-built objects can be used by multiple programs. 3. Provides an example of modeling a real-world object, an alarm clock, in an OOP framework including its fields, public methods, and private methods.

Uploaded by

lemuel sardual
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Design Program Logic - 9

This document provides an overview of object-oriented programming concepts including: 1. Compares procedural and object-oriented programming, noting that OOP is centered on objects that encapsulate both data and functions. 2. Discusses object reusability as a benefit of OOP where pre-built objects can be used by multiple programs. 3. Provides an example of modeling a real-world object, an alarm clock, in an OOP framework including its fields, public methods, and private methods.

Uploaded by

lemuel sardual
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Claret College of Isabela

Senior High School


P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865

NAME: ____________________________________________ DATE: ____________________


GRADE and STRAND: ______________________________ OUTPUT NO: 9

Object-Oriented Programming
Lesson
LESSON OBJECTIVE:
1. Compares procedural and object-oriented programming practices.
2. The student learns how to model classes with UML and how to find the classes in a particular
problem.

VALUES INTEGRATION:
Integrity witnesses of faith, upholds our Claretian principle and lives a moral and dignified
life.
Excellence strives for perfection and holiness, pursues academic excellence in achieving
holistic transformation.

EXPLORE and EXPLAIN:

Define the following statements.

1. What is an object?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________

2. What is encapsulation?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________

3. Why is an object’s internal data usually hidden from outside code?


_________________________________________________________________________________________
_________________________________________________________________________________________
_________________________________________________________________________________________
___

4. What are public methods? What are private methods?


_________________________________________________________________________________________
_________________________________________________________________________________________
_________________________________________________________________________________________
___
1 Design Program Logic - Sardual-09755544188
Claret College of Isabela
Senior High School
P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865

Chapter 14. Object-Oriented Programming

Procedural and Object-Oriented Programming


CONCEPT: Procedural programming is a method of writing software. It is a programming practice centered on
the procedures or actions that take place in a program. Object-oriented programming is centered
on the object. Objects are created from abstract data types that encapsulate data and functions
together.

A procedure is simply a module or function that performs a specific task such as gathering input from the user,
performing calculations, reading or writing files, displaying output, and so on. The programs that you have
written so far have been procedural in nature.

Object Reusability

In addition to solving the problems of code/data separation, the use of OOP has also been encouraged by the
trend of object reusability. An object is not a stand-alone program, but is used by programs that need its service.
For example, Sharon is a programmer who has developed an object for rendering 3D images. She is a math
whiz and knows a lot about computer graphics, so her object is coded to perform all of the necessary 3D
mathematical operations and handle the computer’s video hardware. Tom, who is writing a program for an
architectural firm, needs his application to display 3D images of buildings. Because he is working under a tight
deadline and does not possess a great deal of knowledge about computer graphics, he can use Sharon’s object to
Perform the 3D rendering (for a small fee, of course!).

An Everyday Example of an Object


Think of your alarm clock as an object. It has the following fields:
 The current second (a value in the range of 0–59)
 The current minute (a value in the range of 0–59)
 The current hour (a value in the range of 1–12)
 The time the alarm is set for (a valid hour and minute)
 Whether the alarm is on or off (“on” or “off”)

As you can see, the fields are merely data values that define the state that the alarm clock is currently in. You,
the user of the alarm clock object, cannot directly manipulate these fields because they are private. To change a
field’s value, you must use one of the object’s methods. The following are some of the alarm clock object’s
methods:
 Set time
 Set alarm time
 Turn alarm on
 Turn alarm off
Each method manipulates one or more of the fields. For example, the Set time method allows you to set the
alarm clock’s time. You activate the method by pressing a button on top of the clock. By using another button,
you can activate the Set alarm time method.
In addition, another button allows you to execute the Turn alarm on and Turn alarm off methods. Notice that all
of these methods can be activated by you, who are outside of the alarm clock. Methods that can be accessed by
entities outside the object are known as public methods. The alarm clock also has private methods, which are
part of the object’s private, internal workings. External entities (such as you, the user of the alarm clock) do not
have direct access to the alarm clock’s private methods. The object is designed to execute these methods
automatically and hide the details from you. The following are the alarm clock object’s private methods:
 Increment the current second
 Increment the current minute
 Increment the current hour
 Sound alarm

2 Design Program Logic - Sardual-09755544188


Claret College of Isabela
Senior High School
P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865
Every second, the Increment the current second method executes. This changes the value of the current second
field. If the current second field is set to 59 when this method executes, the method is programmed to reset the
current second to 0, and then cause the Increment the current minute method to execute. This method adds 1 to
the current minute, unless it is set to 59. In that case, it resets the current minute to 0 and causes the Increment
the current hour method to execute. (Note that the Increment the current minute method compares the new time
to the alarm time. If the two times match and the alarm is turned on, the Sound alarm method is executed.)

Classes
CONCEPT: A class is code that specifies the fields and methods for a particular type of object.

The programmer determines the fields and methods that are necessary, and then creates a class. A class is code
that specifies the fields and methods of a particular type of object. Think of a class as a “blueprint” that objects
may be created from. It serves a similar purpose as the blueprint for a house. The blueprint itself is not a house,
but is a detailed description of a house.

Creating a Class, Step by Step


The general format that we will use to write a class definition in pseudocode is as follows:
Class ClassName
Field declarations and method definitions go here…
End Class
The first line starts with the word Class, followed by the name of the class. In most languages, you follow the
same rules for naming variables as when naming classes. Next, you write the declarations for the class’s fields
and the definitions of the class’s methods. (In general terms, the fields and methods that belong to a class are
referred to as the class’s members.) The words End Class appear at the end of the class definition.

Now we will demonstrate how a class is typically created in an object-oriented language. Because classes have
several parts, we will not show the entire class all at once. Instead, we will put it together in a step-by-step
fashion. Suppose we are designing a program for Wireless Solutions, a business that sells cell phones and
wireless service. The program will be used to keep track of the cell phones that the company has in inventory.
The data that we need to keep for a cell phone is as follows:
 The name of the phone’s manufacturer
 The phone’s model number
 The phone’s retail price

If we were designing a procedural program, we would simply use variables to hold this data. In this example,
we are designing an object-oriented program, so we will create a class that represents a cell phone. The class
will have fields to hold these items of data. The pseudocode in Class Listing 14-1 shows how we will start
writing the
class definition:

Accessor and Mutator Methods


As mentioned earlier, it is a common practice to make all of a class’s fields private and to provide public
methods for accessing and changing those fields. This ensures that the object owning those fields is in control of
all changes being made to them. A method that gets a value from a class’s field but does not change it is known
as an accessor method. A method that stores a value in a field or changes the value of a field in some other way
is known as a mutator method. In the CellPhone class, the methods getManufacturer, getModelNumber, and
getRetailPrice are accessors, and the methods setManufacturer, setModelNumber, and getRetailPrice are
mutators.

NOTE: Mutator methods are sometimes called “setters” and accessor methods are sometimes called “getters.”

Constructors
A constructor is a method that is automatically called when an object is created. In most cases, a constructor is
used to initialize an object’s fields with starting values. These methods are called “constructors” because they
help construct an object. In many programming languages, a constructor has the same name as the class that the
constructor is in.
NOTE: In Visual Basic, constructors are named New.
3 Design Program Logic - Sardual-09755544188
Claret College of Isabela
Senior High School
P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865

Default Constructors
In most object-oriented languages, when an object is created its constructor is always called. But what if we do
not write a constructor in the class that the object is created from? If you do not write a constructor in a class,
most languages automatically provide one when the class is compiled. The constructor that is automatically
provided is usually known as the default constructor. The actions performed by the default constructor vary
from one language to another. Typically, the default constructor assigns default starting values to the object’s
fields.

Using the Unified Modeling Language to Design Classes


CONCEPT: The Unified Modeling Language (UML) is a standard way of drawing diagrams that describe
object-oriented systems.

When designing a class, it is


often helpful to draw a UML
diagram. UML stands for
Unified Modeling Language.
It provides a set of standard
diagrams for graphically
depicting object-oriented
systems. Figure 14-10 shows
the general layout of a UML
diagram for a class. Notice
that the diagram is a box that
is divided into three sections.
The top section is where you
write the name of the class.
The middle section holds a list
of the class’s fields. The
bottom section holds a list of
the class’s methods.

Data Type and Method Parameter Notation


The UML diagram in Figure 14-11 shows only basic information about the CellPhone class. It does not show
details such as data types and the method’s parameters. To indicate the data type of a field, place a colon
followed by the name of the data type after the name of the field. For example, the manufacturer field in the
CellPhone class is a String. It could be listed in the UML diagram as follows:

manufacturer : String

The return type of a method can be listed in the same manner. After the method’s name, place a colon followed
by the return type. The CellPhone class’s getRetailPrice method returns a Real, so it could be listed in the UML
diagram as follows:

getRetailPrice() : Real

Parameter variables and their data types may be listed inside a method’s parentheses. For example, the
CellPhone class’s setManufacturer method has a String parameter named manufact, so it could be listed in the
UML diagram as follows:

setManufacturer(manufact : String)

Access Specification Notation

4 Design Program Logic - Sardual-09755544188


Claret College of Isabela
Senior High School
P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865
The UML diagrams in Figures 14-11 and 14-12 list all of the fields and methods in the CellPhone class but do
not indicate which are private and which are public. In a UML diagram, you have the option to place a -
character before a field or method name to indicate that it is private, or a + character to indicate that it is public.

Inheritance
CONCEPT: Inheritance allows a new class to extend an existing class. The new class inherits the members of
the class it extends.

Generalization and Specialization


In the real world, you can find many objects that are specialized versions of other more general objects. For
example, the term insect describes a very general type of creature with numerous characteristics. Because
grasshoppers and bumblebees are insects, they have all the general characteristics of an insect. In addition, they
have special characteristics of their own. For example, the grasshopper has its jumping ability, and the
bumblebee has its stinger. Grasshoppers and bumblebees are specialized versions of an insect. This is illustrated
in Figure 14-17.

Inheritance and the “Is a” Relationship When one object is a specialized version of another object, there is an
“is a” relationship between them. For example, a grasshopper is an insect. Here are a few other examples of the
“is a” relationship:
 A poodle is a dog.
 A car is a vehicle.
 A flower is a plant.
 A rectangle is a shape.
 A football player is an athlete.
When an “is a” relationship exists between objects, it means that the specialized object has all of the
characteristics of the general object, plus additional characteristics that make it special. In object-oriented
programming, inheritance is used to create an “is a” relationship among classes. This allows you to extend the
capabilities of a class by creating another class that is a specialized version of it. Inheritance involves a
superclass and a subclass. The superclass is the general class and the subclass is the specialized class. You can
think of the subclass as an extended version of the superclass. The subclass inherits fields and methods from the
superclass without any of them having to be rewritten. Furthermore, new fields and methods may be added to
the subclass, and that is what makes it a specialized version of the superclass.

NOTE: Superclasses are also called base classes, and subclasses are also called derived classes. Either set of
terms is correct. For consistency, this text will use the terms superclass and subclass.

Polymorphism
CONCEPT: Polymorphism allows you to create methods with the same name in different classes (that are
related through inheritance) and gives you the ability to call the correct method depending on the type of object
that is used to call it.

The term polymorphism refers to an object’s ability to take different forms. It is a powerful feature of object-
oriented programming. In this section, we will look at two essential ingredients of polymorphic behavior:

5 Design Program Logic - Sardual-09755544188


Claret College of Isabela
Senior High School
P.O. Box 8692 Roxas Avenue, Isabela City, Basilan Province, 7300 Philippines
Tel. No. (062) 200 - 7697 / 200 – 7009 Telefax: (062) 200 - 3865
1. The ability to define a method in a superclass, and then define a method with the same name in a
subclass. When a subclass method has the same name as a superclass method, it is often said that the subclass
method overrides the superclass method.

2. The ability to declare a class variable of the superclass type, and then use that variable to reference
objects of either the superclass or the subclass types.

NAME:___________________________________________ DATE: ____________

GRADE and STRAND: ______________________________ OUTPUT NO: 9

EXTEND AND ELABORATE

1. What does it mean to say there is an “is a” relationship between two objects?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________

2. What does a subclass inherit from its superclass?


__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________

3. What are classes’ responsibilities?


__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
EVALUATE: Short Answer
1. You hear someone make the following comment: “A blueprint is a design for a house. A carpenter can use
the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the
same blueprint.” Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or
does it represent an object?

6 Design Program Logic - Sardual-09755544188

You might also like