STE Computer Programming Q4 MODULE 4 5
STE Computer Programming Q4 MODULE 4 5
STE Computer Programming Q4 MODULE 4 5
Computer
Programming
Quarter IV – Module 4-5:
Concepts of Objects and Classes
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.
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:
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.
iii
At the end of this module you will also find:
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:
a. Arff!
b. AttributeError:
c. Woof!
d. Walking
a. CanineError
b. Arff!
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
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.
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).
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:
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)"
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.
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.
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
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.
______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.
Multiple Choice. Read the following statements carefully. Choose your answer from
the options given. Encircle the letter of the correct answer.
a. a distraction
b. an instance
c. a blueprint
d. a nuisance
a. a class function
b. a factory
c. an operation
d. a method
a. Arff!
b. AttributeError:
c. Woof!
d. Walking
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. methods c. template
b. strings d. interface
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