STE Computer Programming Q4 MODULE 4 5

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

10

Computer
Programming
Quarter IV – Module 4-5:
Concepts of Objects and Classes

"Designed by macrovector / Freepik"


Computer Programming – Grade 10
Self-Learning Module
First Edition, 2020

Republic Act 8293, section 176 states that: No copyright shall subsist in any work of
the Government of the Philippines. However, prior approval of the government agency or office
wherein the work is created shall be necessary for exploitation of such work for profit. Such
agency or office may, among other things, impose as a condition the payment of royalties.

Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from their
respective copyright owners. The publisher and authors do not represent nor claim ownership
over them.

Published by the Department of Education – Regional Office VIII


Regional Director: Ramir B. Uytico EdD, CESO IV
Assistant Regional Director: Arnulfo M. Balane, CESO V

Development Team of the Module


Writers: JAYSON C. CAJATE – Borongan City Division, SFNHS
ELEANOR B. DE LA VEGA – Borongan City Division, ESNCHS
Language Editors: Jeanilyn Escoto - Borongan City Division, SFNHS
Content Editors: Name
Illustrators: Name
Layout Artist: Name
Management Team:
Rosemarie M. Guino EdD, OIC – Chief, CLMD
Ryan R. Tiu EdD, EPS, CLMD – Science
Joy B. Bihag, EPS, CLMD – LRMS
Name, Chief, CID
Name, EPS, CID – Science
Name. EPS, CID - LRMS

Printed in the Philippines by ________________________

Department of Education – Regional Office VIII

Office Address: Government Center, Candahug, Palo, Leyte

Telefax: 053 - 3233156


E-mail Address: [email protected].
Introductory Message
This Self-Learning Module (SLM) is prepared so that you, our dear learners,
can continue your studies and learn while at home. Activities, questions, directions,
exercises, and discussions are carefully stated for you to understand each lesson.

Each SLM is composed of different parts. Each part shall guide you step-by-
step as you discover and understand the lesson prepared for you.

At the end of each module, you need to answer the test to self-check your
learning. Answer keys are provided for each activity and test. We trust that you will
be honest in using these.

In addition to the material in the main text, Notes to the Teacher are also
provided to our facilitators and parents for strategies and reminders on how they can
best help you on your home-based learning.

Please use this module with care. Do not put unnecessary marks on any part
of this SLM. Use a separate sheet of paper in answering the exercises and tests. And
read the instructions carefully before performing each task.

If you have any questions in using this SLM or any difficulty in answering the
tasks in this module, do not hesitate to consult your teacher or facilitator.

Thank you.

ii
For the learner:

Welcome to the Research 7 Self – Learning Module 1on (Lesson Title)!

The hand is one of the most symbolized part of the human body. It is often used to
depict skill, action, and purpose. Through our hands we may learn, create, and
accomplish. Hence, the hand in this learning resource signifies that you as a learner
is capable and empowered to successfully achieve the relevant competencies and
skills at your own pace and time. Your academic success lies in your own hands!

This module was designed to provide you with fun and meaningful opportunities for
guided and independent learning at your own pace and time. You will be enabled to
process the contents of the learning resource while being an active learner.

This module has the following parts and corresponding icons:

This will give you an idea of the skills or


Explore
competencies you are expected to learn in the
module. A brief drill or review to help you link
the current lesson with the previous one. The
new lesson will also be introduced to you in
various ways such as a story, a song, a poem, a
problem opener, an activity, or a situation.
This section provides a brief discussion of the
Learn
lesson. This aims to help you discover and
understand new concepts and skills.

What’s More This comprises activities for independent


practice to solidify your understanding and skills
of the topic. You may check the answers to the
exercises using the Answer Key at the end of the
module.
This includes questions or blank
Apply sentence/paragraph to be filled into process
what you learned from the lesson.

Assess This is a task which aims to evaluate your level


of mastery in achieving the learning competency.

This contains answers to all activities in the


Answer Key module.

This contains the learner’s reflection. Learners


Reflect
are encouraged to think about the lessons
particularly the parts that went well (they have
understood) and the parts that were weak (they
have difficulty) and write about it briefly.
Learners can share their thoughts and feeling
about the lessons.

iii
At the end of this module you will also find:

References This is a list of all sources used in developing


this module.
The following are some reminders in using this module:

1. Use the module with care. Do not put unnecessary mark/s on any part of the
module. Use a separate sheet of paper in answering the exercises.
2. Read the instruction carefully before doing each task.
3. Observe honesty and integrity in doing the tasks and checking your answers.
4. Finish the task at hand before proceeding to the next.
5. Return this module to your teacher/facilitator once you are through with it.
If you encounter any difficulty in answering the tasks in this module, do not
hesitate to consult your teacher or facilitator. Always bear in mind that you are
not alone.

We hope that through this material, you will experience meaningful learning and
gain deep understanding of the relevant competencies. You can do it!

iv
Explore

Introduction:

Object Oriented Programming or OOP for short, is a programming paradigm


which provides a means of structuring programs so that properties and
behaviors are bundled into individual objects.

An object could represent a person with some properties such as age


and address and with behaviors like walking, talking, breathing and running.
Another object may represent an email with properties like recipient lists,
subject and body, and behaviors like adding attachments and sending.

Put another way, object – oriented programming is an approach for


modeling concrete, real-world things, such as player in a game, as well as
relations between things, like companies and employees, students and
teachers, etc. OOP models real-world entities as software objects, which have
some data associated with them and can perform certain functions.

The learner shall be able to:

Demonstrate the use of objects and classes

After going through this module, you are expected to


1. Determine the difference between objects and classes;
2. Identify the principles to follow in creating a class;
3. Understand and be able to explain the concepts of classes and objects in
OOP;
4. Interpret code for simple classes of object and classes.

Q2_STE_Computer_Programming_ Module 1 Page 1 of 24


PRE-ASSESSMENT

Multiple Choice. Read the following statements carefully. Choose your


answer from the options given. Encircle the letter of the correct answer.

1. A class is ________________ for a concrete object.


a. a distraction
b. an instance
c. a blueprint
d. a nuisance

2. Object and classes attributes are accessed using ____ notation.


a. (=)
b. (/)
c. (?)
d. (.)

3. A function within a class definition is called a________________.


a. a class function
b. a factory
c. an operation
d. a method

4. What is the output of the following code snippet?

a. Arff!
b. AttributeError:
c. Woof!
d. Walking

5. What’s the output of the following code snippet?

a. CanineError
b. Arff!

Q2_STE_Computer_Programming_ Module 1 Page 2 of 24


c. Woof!
d. *walking!

6. Common behavior can be defined in a superclass and inherited into


subclass using the __________ keywords.

a. extends c. method
b. interface d. behavior
7. How many classes can be defined in a single program?
a. Only 1 c. only 999
b. Only 100 d. As many as you want
8. Which feature of OOP illustrate the code reusability?
a. inheritance c. encapsulation
b. polymorphism d. abstraction
9. Real-world objects contains ________ and _______
a. state and behavior c. class and objects
b. clusters and characteristics d. data and measure
10. A software object’s behavior is exposed through ________________.

a. methods c. template
b. strings d. interface

Q2_STE_Computer_Programming_ Module 1 Page 3 of 24


Learn

What is an Object?
An object is an instance or given specimen of a class. An object in OOPS is nothing
but a self-contained component which consists of methods and properties to make
a particular type of data useful.

o State - all properties of an object


o Behavior - how an object reacts to interactions, such as calling a
certain method. In OOP speak: Response of an object when sending it
messages
o Identity - multiple objects can have the same state and behavior, but
each one is a unique entity. Structure and Behavior of similar objects
is defined by their class.

Objects exist in memory at runtime. Just like objects of primitive types (integers,
floating-point numbers). We can interpret 4 bytes of data as integer number. We
can interpret 8 bytes of data as floating-point number. In C we have structs to
create composite types containing multiple primitive types. In C++ and other OOP
languages this is further extended by associating behavior to a chunk of data
through specifying methods to manipulate that data.

Lifetime of an Object
Allocation
Allocation
Allocate enough memory to store object data/state
Initialization
Initialization
Set an initial object state

Usage Usage
Interact with objects through methods
Access and modify object data

Cleanup Cleanup
Make sure that everything is in order before deletion

Deletion
Memory is freed, object ceases to exist Deletion
What is a Class?
Class is an extensible program code template for creating objects, providing initial
values for state (member variables) and implementations of behavior (member
function or methods).

Q2_STE_Computer_Programming_ Module 1 Page 4 of 24


Class defines memory structure of objects; how it reacts to interactions; how it can
interact with other objects.

o Member Variable - A variable in the scope


of a class; All instances allocate memory for
their variables
o Member Method - A method which can be
called for an object of the class. Can access
and modify the object state by manipulating
member variables
o Interface - All methods which can be
called on an object from outside.

How does class and objects work?


Let us take for example a door as example of real world object. Most doors have
limited functionality. They maybe opened and closed, and locked and unlocked. In
procedural programming, we might design functions to open, close, lock and
unlock door such as;

Object oriented programming combines code and data, so that, rather than having
separate functions act on doors, we design doors that have methods that can act on
themselves. Methods represent something the object can do, and are typically using
verbs. Object-oriented door pseudocode might look like:

Objects may also have attributes, something the objects is or has, and are typically
defined using nouns or adjectives. Door attributes might include:

Q2_STE_Computer_Programming_ Module 1 Page 5 of 24


When we write code to define a generic door, we would create a door class. The door
class would contain all of the methods a door can perform an all the attributes a door
might have. We would then create instances of the class (objects) to represent specific
doors, such as front door, back door or room door on the house or left and right door
of a car.

Q2_STE_Computer_Programming_ Module 1 Page 6 of 24


Message and Method OOP uses "messages" instead of function calls. Sending
a message to an object causes that object to perform an operation on itself. In that
case, the object receiving the message and performing the operation is known as the
"receiver". The receiver looks at the message, figures out the appropriate operation
to perform, and executes that operation on itself.
The receiver knows what operations it can perform, because it knows its class,
and the class defines all the operations for its instances. The code corresponding to
a particular message is known as the "method" for that message. A message is just
a string like "pop()". The method for pop() is the code in the Stack class which is
triggered by the "pop()" message. The C++ specific term for method is "member
function".
// Traditional programming // Call the foo() operation, and pass it the data to operate on
foo(x);
// OOP programming // Send a "foo()" message to the object -- the "receiver" of the message.
// The receiver gets the message, finds the matching method code in its class, // and the method code
executes against the receiver. x "foo()"
The method that executes depends on the class of the receiver. If you send the
print() message to a Stack object, it executes the print() method in the Stack class.

Message Send Syntax In Java and C++, the syntax for sending a message looks
like appending the message to the desired receiver with a dot:
x.foo(); // Send the "foo()" message to the receiver "x"
Receiver Relative Code We say that "the method executes against the receiver."
This just means that the method code operates on the data of the receiver. So in code
like the following...
x "removeAllElements()"
y "addElement(12)"

Q2_STE_Computer_Programming_ Module 1 Page 7 of 24


If you send that same print() message to a HashTable object, you get the
(different) print() method in the HashTable class. It's what you say, and the class of
the receiver that you say it to.

Principles of Building an Object

“Abstraction” In plain English, abstract is a concept or idea not associated


with any specific instance and does not have a concrete existence. Abstraction in
Object Oriented Programming refers to the ability to make a class abstract.
Abstraction captures only those details about an object that are relevant to the
current perspective, so that the programmer can focus on a few concepts at a time.

Java provides interfaces and abstract classes for describing abstract types. An
interface is a contract or specification without any implementation. An interface can't
have behavior or state. An abstract class is a class that cannot be instantiated, but
has all the properties of a class including constructors. Constructor of an abstract
class is invoked by a subclass from its constructor using super keyword (e.g. super()).
Abstract classes can have state and can be used to provide a skeletal
implementation.

"Encapsulation" refers to protecting the internals of an object from direct


manipulation by the client. The client can send messages, but the client cannot
change the bits in the object directly. In C , it's just a convention that the client
should not mess with or depend on the implementation of an Abstract Data Type
(ADT). With encapsulation, the compiler enforces the separation. The class
implementation can indicate which parts of the implementation are protected so that
client code accessing those parts will not compile.
Or put another way: the client cannot mess with the object's state — the client
can only send messages. The object's state is only touched by its own methods. Once
those methods are correct and debugged, the object can be given to any client, and
that client should not be able to perform an operation on the object which puts it in
an incorrect state. This depends on the methods being correct which is still difficult.
But when they are correct at last, the client should not be able to mess things up.
"Inheritance" is the process by which a class inherits the properties of its
super classes. Methods in particular are inherited. When an object receives a
message, it checks for a corresponding method in its class. If one is found, it is
executed. Otherwise the search for a matching method travels up the tree to the
superclass of the object's class. This means that a class automatically responds to
all the messages of its super classes.

Q2_STE_Computer_Programming_ Module 1 Page 8 of 24


Heirarchy

Hierarchy Classes in OOP are arranged in a tree-like hierarchy. A class'


"superclass" is the class above it in the tree. The classes below a class are its
"subclasses." The semantics of the hierarchy are that classes have all the properties
of their super classes. In this way the hierarchy is general up towards the root and
specific down towards its leaves. The hierarchy helps add logic to a collection of
classes. It also enables similar classes to share properties through "inheritance"
below. A hierarchy is useful if there are several classes which are fundamentally
similar to each other. In C++, a "base class" is a synonym for superclass and "derived
class" is a synonym for subclass.

Q2_STE_Computer_Programming_ Module 1 Page 9 of 24


Overriding

When an object receives a message, it checks its own methods first before
consulting its superclass. This means that if the object's class and its superclass
both contain a method for a message, the object's method takes precedence. In other
words, the first method found in the hierarchy takes precedence. This is known as
"overriding," because it gives a class an easy way to intercept messages before they
get to its superclass. Most OOP languages implement overriding based on the run-
time class of objects. In C++, run-time overriding is an option invoked with the
"virtual" keyword.

Polymorphism

A big word for a simple concept. Often, many classes in a program will respond to
some common message. In a graphics program, many of the classes are likely to
implement the method "drawSelf()." In the program, such an object can safely be sent
the drawSelf() message without knowing its exact class since all the classes
implement or inherit drawSelf(). In other words, you can send the object a message
without worrying about its class and be confident that it will just do the right thing.
Polymorphism is important when the code is complex enough that you are no longer
sure of the exact class of an object — you can just send it a message and rely on it
doing the right thing at run time based on its class.

Q2_STE_Computer_Programming_ Module 1 Page 10 of 24


Setters & Getters

A setter is a method used to change the value of an attribute and a getter is a


method used to get the value of an attribute. There is a standard naming
convention for getters and setters, but Java compiler won't complain even
otherwise.

Q2_STE_Computer_Programming_ Module 1 Page 11 of 24


Engage

Activity 1. Complete Me

Direction: Using the cryptogram below, match the letters with their assigned
numbers to complete the words un each item.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 1 11 1 1 14 1 16 17 18 19 20 21 22 2 24 2 2
0 2 3 5 3 5 6

O J 1. Is a run-time value that stores state


and belongs to some class
16 2 10 5 3 20

I E R A E 2. Is the process by which a class


inherits the properties of its super
classes
9 14 8 5 18 9 20 1 14 3 5

R G A M N G 3. the act or job of creating computer


programs
16 18 15 7 18 1 13 13 9 14 7

L S 4. a user defined blueprint or prototype from which


objects are created.
3 12 1 19 19

J V set of computer software and specifications that provides a


system for developing application software in computing
10 1 22 1 platform

Q2_STE_Computer_Programming_ Module 1 Page 12 of 24


Apply

Direction: Read the following statement carefully. Write O if the statement is True
and write C if the statement if False, then underline the word which make it a false
statement.
______1. In object-oriented programming, a class is a data type and an object an
instance of such a type.

______2. Object-oriented languages directly support abstract data types through


information hiding.

______3. Methods are typically used to construct objects of a particular class type,
whereas constructors are typically used to define operations appropriate to
class types.

______4. Class members can have protected scope but the class that encapsulates
such members cannot have protected scope.

______5. A standard class with package scope is visible only to classes in the same
Package.

______6. Every interface must declare at least one method.

______7. A programmer-defined class can have only package scope.

______8. Object-oriented programming is associated with a design technique known


as top-down, functional decomposition.

______9. An object obj1 sends a message to an object obj2 by invoking an obj2


method.

______10. Encapsulation means the same thing as information hiding.

Q2_STE_Computer_Programming_ Module 1 Page 13 of 24


Assess

Multiple Choice. Read the following statements carefully. Choose your answer from
the options given. Encircle the letter of the correct answer.

1. A class is ________________ for a concrete object.

a. a distraction
b. an instance
c. a blueprint
d. a nuisance

2. Object and classes attributes are accessed using ____ notation.


a. (=)
b. (/)
c. (?)
d. (.)

3. A function within a class definition is called a________________.

a. a class function
b. a factory
c. an operation
d. a method

4. What is the output of the following code snippet?

a. Arff!
b. AttributeError:
c. Woof!
d. Walking

5. What’s the output of the following code snippet?

Q2_STE_Computer_Programming_ Module 1 Page 14 of 24


a. CanineError
b. Arff!
c. Woof!
d. *walking!

6 Common behavior can be defined in a superclass and inherited into


subclass using the __________ keywords.

a. extends c. method
b. interface d. behavior
7. How many classes can be defined in a single program?
a. Only 1 c. only 999
b. Only 100 d. As many as you want
8. Which feature of OOP illustrate the code reusability?
a. inheritance c. encapsulation
b. polymorphism d. abstraction
9. Real-world objects contains ________ and _______

a. state and behavior c. class and objects


b. clusters and characteristics d. data and measure

10. A software object’s behavior is exposed through ________________.

a. methods c. template
b. strings d. interface

Q2_STE_Computer_Programming_ Module 1 Page 15 of 24


Reflect

Today, I have learned that__________________________________


________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
____________________________.
I have difficulty in
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
__________________________________.

Q2_STE_Computer_Programming_ Module 1 Page 16 of 24


Page 17 of 24 Q2_STE_Computer_Programming_ Module 1
Asses Apply
1. C 1. O
2. O
2. D 3. O
4. C
3. D
5. O
4. D 6. O
7. O
5. B 8. O
9. O
6. A 10. O
7. D
8. A
9. A
10. A
Engage Explore
1. Objects 1. C
2. Interface
2. D
3. Programming
4. Class 3. D
5. Java
4. D
5. B
6. A
7. D
8. A
9. A
10. A
Answer Key
References

Online:
https://www.guru99.com/java-oops-class-objects.html/122020
https://press.rebus.community/progrmmingfundamentals/chapter/obj
ects-and-classes/122020
https://tealsl12gitbook.io/apcsa/lesson-501/122020
https://teachinglondoncomputing.org/object-oriented-programming-
week-2-resources/122020
https://study.com/academy/lesson/oop-object-oriented-programming-
objects-classes-interface.html/122020
https://www.scribd.com/document/375132626/1-Questions-Answers-
on-OOPs-Concept-features/122020
https://caveofprogramming.com/java/beginners-java-test-your-
knowledge-of-classes-and-objects.html/122020
http://www.w3shools.com/java/java_classes.asp/122020
https://www.cs.auckland.ac.nz/references/java/javaOO/QandE/objects
-answers.html/ 122020
http://javajee.com/object-oriented-programming-oop-concepts-with-
examples/122020

For inquiries or feedback, please write or call:

Department of Education – Regional Office VIII – Curriculum and Learning


Management Division (CLMD) - Learning Resources Management Section (LRMS)

Government Center, Candahug, Palo, Leyte, 6501

Telefax: (053) 323-3156; 323-3854; 824-4627

Email Address: *[email protected]


*[email protected] *[email protected]

Q2_STE_Computer_Programming_ Module 1 Page 18 of 24

You might also like