Comprog Handout 1 Sem-2
Comprog Handout 1 Sem-2
The naming conventions used in Procedural Programming and OOP are likened below:
Procedural Programming Object-oriented Programming
Variable Objects
User-defined Data Types Classes
Structured Members Instance Variables
Functions Methods
Function Call Message Passing
Here are some of the differences between Procedural Programming and OOP.
Procedural Programming Object-oriented Programming
Emphasizes the procedures rather than the data Emphasizes the data rather than procedures
Data is not secured Data is secured
Uses a top-down approach Uses a bottom-up approach
It does not model the real-world entities It models the real-world entities
Decomposes programs into functions or procedures Decomposes programs into objects
Advantages of OOP
• Modularized programs by using classes and objects.
• Reduced code duplication and code reusability as linking code and object allows related objects to
share common code.
• Provides data security as the data is encapsulated along with functions, making the data from non-
member functions inaccessible and unmodifiable.
• Reduced complexity of the program development through the use of inheritance.
• Reduced time as creation and implementation of OOP code is easy.
OOP Concepts
Class
It is a group or collection of objects with common properties. It is the basic unit of programming in OOP. It is
also a user-defined data type, meaning it is created by the user to store complex data.
In layman’s terms, it is the same way that a blueprint exists before any houses are built or a recipe exists
before any bread is baked from it. A class definition exists before any objects are created from it.
A class definition describes what attributes its objects will have and what those objects can do. An attribute
details the characteristics that define an object, which serves as properties of the object.
Object
It is a specific and concrete instance of a class. It is an identifiable entity with some characteristics and
behavior, which can be a person, place, or table of data. It is any real-world entity that has its existence and
has both features called data members and operations called function members.
Using a smartphone to further visualize the difference between data members and function members, the
features of a smartphone, such as its color, weight, and price, are considered the data members, while its
operation to make a video call, take photographs, or record audio are its function members.
Method
Other than defining properties, classes define the methods their objects can use. A method is a self-
contained block of program code that carries out actions similar to a procedure in Procedural Programming.
Here are some examples that interconnect the three (3) concepts.
Class Object Method
Automobile Make, Model, Year, Color Forward, Backward, Gas Status
Dog Breed, Name, Age, Vaccine Walking, Eating, Name Recognition
Understanding the object’s class helps in understanding the characteristics of the object in the sense that if a
friend purchased an automobile (class), it is a given that it has a model name (object.)
Additionally, objects can be compared to nouns, while methods are similar to verbs.
Data Abstraction
It is the act of including only essential details of an entity without including the background details.
For example, a smartphone with Bluetooth and camera features. Data abstraction does not care how
Bluetooth or the camera works; it is only interested in using the smartphone and its features rather than
their internal working. That information is hidden from the user and is called abstracted data.
Data Encapsulation
It is the process of wrapping up data and functions into a class. It performs data hiding, which insulates data
from outside programs. The data would not be accessible directly to the outside world except for the
functions defined inside the class definition.
Inheritance
It is the ability to create classes that share the attributes and methods of existing classes but with more
specific features.
The class that inherits the properties of the other class is called a base class, parent class, or superclass. The
class that inherits the properties from the other class is called a derived class, child class, or subclass.
For example:
Employee
Employees are the base class, while the others below them are the derived class. The employee base class inherits
properties from the derived classes, with each derived class having its own properties.
Polymorphism
Poly means many, while morph means forms. It describes the feature of languages that allows the same
word or symbol to be interpreted correctly in different situations based on the context. Examples of this
include operator overloading and function overloading.
Operator Loading is the process of making an operator perform tasks in different instances. For example,
using an addition (+) operator. As an arithmetic operator, it performs addition, but if used in a String such as
"Just"+"ice", it combines the two (2) strings and acts as a String concatenation operator resulting in
"Justice".
Function Overloading is the process of two (2) or more functions with the same name but different return
types or numbers of arguments. For example, "Paint() Items" as a single interface but has many
methods, such as paint() furniture, paint() vehicle, and paint()building.
Methods
A method is a program module containing a series of statements that carry out a task, which can be called
an unlimited number of times. A familiar example is the main() method, which executes automatically when
a program is run. A program’s main() method can execute additional methods, which can also execute
others.
To execute a method, a programmer calls it or invokes it. In other words, a calling method, or client method,
invokes a called method.
For example, a class is created to display the organization’s name in a single line of output.
public class CompanyInfo {
public static void main(String[] args) {
System.out.println("Smart Solutions Electronics");
}
}
For additional context, a method header, or method declaration, provides information about how other
methods can interact with it. In this example, public static void main() is the method header.
• public – an access modifier that allows any other class to use it and not just in the class in which
the method resides. Other access modifiers include private, protected, and package if left
unspecified.
• static – used when any method can be used without instantiating an object or not requiring an
object when they are called.
• void – a return type used when a method returns no data. A return type describes the data type the
method sends back to its calling method.
• Method Name – can be any legal identifier for classes and variables. It must be one word with no
spaces and cannot be a Java keyword. Methods perform an action, so their names usually contain a
verb, such as display or compute.
• Parentheses – contains data to be sent to the method. When a main() method is written in a class,
the parentheses in its header surround String[] args.
A method body contains the statements that carry out the work of the method. It is found between a pair of
curly braces. The body of a method is called its implementation.
Assume three (3) lines of output are added to this application to display the business hours of the company;
this is how it will look like:
public class CompanyInfo {
public static void main(String[] args) {
System.out.println("Smart Solutions Electronics");
System.out.println("Monday – Friday 7 AM to 5 PM");
System.out.println("Saturday 7 AM to 12 NN");
System.out.println("Sunday Closed");
}
}
But instead of adding three (3) println() statements, a method can be called to execute the statements.
Using the displayHours() method, the program will now look like this:
In declaring objects, a two-step process is involved in creating an object that is an instance of a class. First, a
type and an identifier are supplied, just like declaring any variable; second, computer memory is allocated
for that object.
For example, an integer as int someValue; can be declared this way, and an Employee can be declared as
Employee someEmployee; wherein someEmployee can be any legal identifier, but objects start with a
lowercase letter.
In declaring int someValue, the compiler is notified that an integer named someValue will exist, and
computer memory will be reserved for it at the same time. In declaring Employee someValue, the compiler
is notified that the identifier someEmployee will be used, but computer memory will not be reserved for it.
A new operator should be used to allocate the memory for an object such as this.
Two (2) statements that complete the process by setting aside enough memory to hold an Employee are as
follows:
Employee someEmployee;
someEmployee = new Employee;
This can be shortened by declaring and reserving memory for someEmployee in one statement:
Employee someEmployee = new Employee();
Wherein:
• Employee is the object’s type and class, while someEmployee is the object’s name.
• Employee now also becomes a reference type as opposed to built-in types like int, which are
primitive types.
• someEmployee also becomes a reference to an object – the name for a memory address where the
object is held.
• The equal sign (=) is the assignment operator, assigning a value to someEmployee in the declaration.
• The new operator is allocating an unused portion of computer memory for someEmployee.
• Employee() after the new operator is the name of the method that constructs an Employee object. It
is a constructor.
A constructor is a special type of method that creates and initializes objects. A constructor can be user-
specified, but Java also writes one whenever a user does not write one. The name of the constructor is the
same as the name of the class whose objects it constructs.
After an object has been instantiated, the methods can be accessed using the object’s identifier, a dot, and a
method call.
public class DeclareTwoEmployees {
public static void main(String[] args) {
Employee clerk = new Employee();
Employee driver = new Employee();
clerk.setEmpNum(345);
driver.setEmpNum(567);
System.out.println("The clerk's number is " +
clerk.getEmpNum() + " and the driver's number is " +
driver.getEmpNum());
}
}
The program above shows the DeclareTwoEmployees application that instantiates two (2) Employee
objects, clerk and driver, with each using setEmpNum() and getEmpNum() methods one time.
References:
Farerel, J. (2023). Java programming, 10th edition. Cengage.
Ladwa, H. (2021). Object-oriented programming with Java.
Mitchell, B.. (2022). What is functional programming and why use it? [Web Article]. Retrieved on October 24, 2024, from
https://www.codingdojo.com/blog/what-is-functional-programming
Scarler Topics (2022). OOP vs functional vs procedural. [Web Article]. Retrieved on October 30, 2024, from
https://www.scaler.com/topics/java/oop-vs-functional-vs-procedural/