Handout OOP
Handout OOP
1
Break the chain of COVID19 Infection
Wear face mask
Frequent hand washing
Proper hygiene
Use alcohol and sanitizer
Always observe physical distancing
Cover your mouth when coughing and sneezing
Preliminaries
University Vision
A synergistic multi-campus university producing competent, value laden and globally
competitive graduates who are proactive in promoting the socio-economic prosperity of the
country.
University Mission
The University shall primarily provide advanced education, higher technological
professional instruction and training in the fields of agriculture, arts and sciences, business
and industry, computer and information technology, education, engineering, environmental
sciences, fisheries, forestry, law and criminal justice, medicine and allied sciences and other
related fields of study. It shall intensify its research, extension and production functions and
provide progressive leadership in its areas of specialization.
Core Values
Excellence
Integrity
Accountability
Quality Policy
We commit to provide quality instruction, research, extension and production grounded
on excellence, integrity and accountability as we move towards exceeding stakeholders’
satisfaction in compliance with relevant requirements and well-defined continual improvement
measures
“Dekalidad na edukason, Kinabuhi na mainuswagon”
2
Preliminary Activity:
A. Vision and Mission
What personal and professional qualities should you possess to become
globally competitive graduate?
How will the University prepare you to become professional in your chosen
field of specialization?
B. Course Content, Requirements and Marking System (Please read the course
guide)
Introduction
Object-Oriented Programming (OOP) refers to a type of programming in which
programmers define the data type of a data structure and the type of operations that can be
applied to the data structure. The primary purpose of object-oriented programming is to
increase the flexibility and maintainability of programs. Object oriented programming brings
together data and its behavior (methods) in a single location (object) makes it easier to
understand how a program works.
In this chapter, we will introduce the basic core concepts of object-oriented
programming, its distinction to procedural oriented programming, abstract data types and of
course to better understand the foundations of OOP and its application in creating powerful,
scalable, and dynamic programs.
Learning Outcomes:
At the end of this lesson, you are expected to:
1. Define Object-Oriented Programming.
2. Identify the different core concepts of OOP.
3. Distinguish the difference of procedural oriented programming and object-oriented
programming.
4. Discuss the basic principles OOP using Java programming language.
5. Discuss the abstract data types in Java.
ACTIVITY
At this point, I want you to remember your prevus computer programming 1 and 2
courses/lessons about computing. After this, please accomplish the K-W-L chart. First, answer
the (K) what you know and (W) what you want to know about the lesson. Once you finish
lesson 1, go back to the K-W-L chart again and answer (L) what you learned and need to be
discovered. You may use the template below, or you can re-encode the table.
3
K-W-L Chart
K- what I know W- what I want to know L- what I learned and still
need to learn
LESSON PROPER
What is an Object?
Object is a bundle of data and its behavior (often known as methods). It is an instance of
a Class. It contains properties and can be a combination of variables, functions, and data
structure. This is usually a noun.
An object in object-oriented programming consists of:
1. State: It is represented by attributes of an object. It also reflects the
properties of an object.
2. Behavior: It is represented by methods of an object. It also reflects the
response of an object with other objects.
3. Identity: It gives a unique name to an object and enables one object to
interact with other objects.
Examples:
Object: Dog Object: Car
Identity: Dog Identity: Car
State/Attributes: Breed, Age, Color State/Attributes: Color, Model, Brand
Behavior: Bark, Wag Tail, Eat Behavior: Accelerate, Brake, Slow down
What is a Class?
A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type. In
general, class declarations can include these components, in order:
1. Modifiers: A class can be public or has default access.
2. Class name: The name should begin with an initial letter (capitalized by
convention).
3. Superclass (if any): The name of the class’s parent (superclass), if any,
preceded by the keyword extends. A class can only extend (subclass) one
parent.
4. Interfaces (if any): A comma-separated list of interfaces implemented by the
class, if any, preceded by the keyword implements. A class can implement more
than one interface.
4
5. Body: The class body surrounded by braces, { }.
Example:
What is a Method?
A method is a collection of statements that perform some specific task and return result to
the caller. A method can perform some specific task without returning anything. Methods allow
us to reuse the code without retyping the code. In Java, every method must be part of some
class. Methods are time savers and help us to reuse the code without retyping the code. This
is usually a verb.
In general, method declarations has six components:
1. Access Modifiers: Defines access type of the method i.e. from where it can be
accessed in your application. In Java, there 4 type of the access specifiers.
a. Public: accessible in all class in your application.
b. Protected: accessible within the package in which it is defined and in
its subclass(es) (including subclasses declared outside the package).
c. Private: accessible only within the class in which it is defined.
d. Default (declared/defined without using any modifier): accessible within
same class and package within which its class is defined.
2. The return type: The data type of the value returned by the method or void if does not
return a value.
3. Method Name: the rules for field names apply to method names as well, but the
convention is a little different.
4. Parameter list: Comma separated list of the input parameters are defined, preceded
with their data type, within the enclosed parenthesis. If there are no parameters, you
must use empty parentheses ().
5. Exception list: The exceptions you expect by the method can throw, you can specify
these exception(s).
6. Method body: it is enclosed between braces. The code you need to be executed to
perform your intended operations.
Example:
5
There are Four (4) Principles that makes a language object-oriented. These are
Encapsulation, Data Abstraction, Polymorphism and Inheritance. These are also called as four
pillars of Object Oriented Programming.
2. Inheritance
In OOP, computer programs are designed in such a way where everything is an object
that interact with one another. Inheritance is one such concept where the properties of one
class can be inherited by the other. It helps to reuse the code and establish a relationship
between different classes. It is the mechanism in java by which one class is allow to inherit
the features (fields and methods) of another class.
6
As we can see in the image, a child
inherits the properties from his father.
Similarly, in Java, there are two
classes:
1. Parent class (Super or Base class)
2. Child class (Subclass or Derived
class)
A class which inherits the properties is
known as Child Class whereas a class
whose properties are inherited is
known as Parent class.
3. Encapsulation
Encapsulation simply means binding object state (fields) and behavior (methods)
together. It is the mechanism that binds together code and the data it manipulates.
7
Let us try to understand the above code. I have created a class Employee which has
a private variable name. We have then created a getter and setter methods through which we
can get and set the name of an employee. Through these methods, any class which wishes
to access the name variable has to do it using these getter and setter methods.
4. Polymorphism
Polymorphism means taking many forms, where ‘poly’ means many and ‘morph’ means
forms. It is the ability of a variable, function or object to take on multiple forms. In other words,
polymorphism allows you define one interface or method and have multiple implementations.
Example:
Let’s consider this real world scenario in cricket, we know that there are different
types of bowlers i.e. Fast bowlers, Medium pace bowlers and Spinners. As you can see
in the above figure, there is a parent class - BowlerClass and it has three child
classes: FastPacer, MediumPacer and Spinner.
Bowler class has bowlingMethod() where all the child classes are inheriting this method.
As we all know that a fast bowler will going to bowl differently as compared to medium pacer
and spinner in terms of bowling speed, long run up and way of bowling, etc. Similarly a medium
pacer’s implementation of bowlingMethod() is also going to be different as compared to other
bowlers. And same happens with spinner class. The point of above discussion is simply that
a same name tends to multiple forms. All the three classes above inherited
the bowlingMethod() but their implementation is totally different from one another.
8
Languages used in Procedural Programming:
FORTRAN, COBOL, ALGOL, BASIC, Pascal, and C.
Example:
9
Difference between Object-Oriented Programming and Procedural Programming
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, program is In object oriented programming,
divided into small parts program is divided into small parts
called functions. called objects.
Procedural programming follows top Object oriented programming
down approach. follows bottom up approach.
There is no access specifier in Object oriented programming have
procedural programming. access specifiers like private, public,
protected and etc.
Adding new data and function is not Adding new data and function is easy.
easy.
Procedural programming does not have Object oriented programming provides
any proper way for hiding data so it data hiding so it is more secure.
is less secure.
In procedural programming, overloading Overloading is possible in object
is not possible. oriented programming.
In procedural programming, function is In object oriented programming, data is
more important than data. more important than function.
Procedural programming is based Object oriented programming is based
on unreal world. on real world.
ASSESSMENT
Direction: Do not write anything in this learning material. Please provide a separate paper for
your answers and indicate your name, course & year, and section.
1. What are the four basic principles of OOP? Provide a brief description of each and give
atleast one example.
2. In your own understanding, state the distinction between Procedural-Oriented
Programming and Object-Oriented Programming.
3. In a real world setting, cite an example of an object and a class. Discuss briefly your
examples.
4. Determine the names of the highlighted terms in the source code.
REFERENCES:
[1] Herbert, Schildt. (2019). Java A Beginner’s Guide Eight Edition, McGraw-Hill
Education, New York, USA.
[2] Eck, David J. (2006). Introduction to Programming using Java, Creative Commons,
California, USA.
[3] Gosling, J., Joy, B., Steele, G., Bracha, G., & Buckley, A. (2015). The Java Language
Specification, Java SE 8th Edition, Oracle America, Inc., California, USA.
Suggested Readings:
[1] https://www.edureka.co/blog/object-oriented-programming/
[2] https://www.javatpoint.com/java-tutorial
[3] https://www.tutorialspoint.com/java/index.htm
Reminder:
Submit this learning material securely packaged (please provide an extra plastic envelope
intended for the second set of learning materials) to the campus security personnel or as advised
by your subject teacher, on the prescribed date and time.
11