Design Program Logic - 9
Design Program Logic - 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.
1. What is an object?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
2. What is encapsulation?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
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!).
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
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.
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:
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.
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)
Inheritance
CONCEPT: Inheritance allows a new class to extend an existing class. The new class inherits the members of
the class it extends.
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:
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.
1. What does it mean to say there is an “is a” relationship between two objects?
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________
__________________________________________________________________________________________