1.2. Programming Paradigms - Intermediate
1.2. Programming Paradigms - Intermediate
https://bit.ly/pmt-edu
Specification:
https://bit.ly/pmt-edu
The procedural programming paradigm
Programs written in the procedural programming paradigm are formed from sequences of
instructions that are executed in the order in which they appear. Procedures like functions
and subroutines form parts of the program and can be called from anywhere within the
program or by other procedures.
Most of the programs that you may have written are likely to have been procedural.
Designing a program from the top down makes maintaining the program easier as
navigation of different elements of the overall solution is improved. If all of a program’s
code is contained within the same module, finding the specific line of code that needs
fixing can be incredibly difficult - especially in large projects.
When a program is split into modules, testing can be carried out on the individual modules
before they are combined to form the overall solution.
https://bit.ly/pmt-edu
Hierarchy charts
A hierarchy chart graphically represents the structure of a structured program. Each
procedure is displayed as a rectangle which is connected to any other procedures that are
used within it.
Example
SUBROUTINE Main()
name ← INPUT
yearBorn ← INPUT
FUNCTION calculateAge(year)
age ← calculateAge(yearBorn)
yearNow ← getYear() FUNCTION getYear()
IF age > 17 THEN
age ← yearNow - year RETURN system.year
OUTPUT name + “can drive”
RETURN age END FUNCTION
ELSE
END FUNCTION
OUTPUT name + “can’t drive”
END IF
END SUBROUTINE
Objects
Rather than storing data with constants and variables like procedural programs,
object-oriented programs use objects as containers of both data and instructions.
New objects are created from classes. The process of creating an object is called
instantiation. It is possible for one program to have many different objects of the same
class which will all have identical methods and procedures, however the actual values of
their properties are unique to the object. For example, a program could have three
different objects of the class car which all have a property called manufacturer. All
three cars could have different manufacturers.
https://bit.ly/pmt-edu
Classes & class definitions
An object is defined as an instance of a class. A class is like a blueprint for objects, they
specify what properties (data) and methods (instructions) objects of their type will have.
For example, a class for creating car objects might have the properties EngineSize and
Colour and the methods StartEngine, ApplyHandbrake and TurnOnLights.
A class can be expressed on paper as a class definition which lists a class’ name,
properties and methods. Exam questions often ask you to write or interpret class
definitions.
The class definition for the car class could look like this:
Car = Class {
Private:
Manufacturer: String
Model: String
EngineCapacity: Float
IsTaxed: Boolean
Public:
Function GetManufacturer
Function GetModel
Function GetEngineCapacity
Function GetIsTaxed
Procedure SetDetails
}
A method or property that is listed as private can only be accessed from within an object.
Public methods allow an interface for accessing and modifying a class’ private properties.
Encapsulation
Encapsulation is the name given to the process of combining methods and procedures to
form an object in object-oriented programming. An object is said to encapsulate its
contents, forming a single entity which encompasses all of the object’s properties and
methods.
https://bit.ly/pmt-edu
Inheritance
A class can inherit another class. Inheritance allows one class to share the properties and
methods of another class, while having its own properties and methods too.
The example above shows the class description for DeLorean. The brackets after class
(shown in red) indicate that the class inherits the Car class and therefore has all of the
properties and methods that the Car class has as well as its own properties and methods
that do not exist in the Car class such as ReactorOutput.
Polymorphism
Polymorphism occurs when objects are processed differently depending on their class.
For example, if two objects with classes Car and Lorry both had a
ConsumeFuelForOneMile method, it might decrease the Car’s FuelLevel property
less than it would the Lorry’s. The method has taken the object’s class into account and
treated them differently based on their class.
Overriding
An overridden method has the same name as a method in an inherited class but different
implementation. The word override after the SetDetails method in the class description
above indicates that the implementation of the SetDetails method in the DeLorean
class differs from the implementation of the method with the same name in the Car class.
Association
If two objects are associated, they can be described as having a “has a” relationship. For
example, objects of the classes Car and Driver could be associated as a car has a
driver. An associated object forms part of its container object as a property. Just like a
property of an object can be a string, it can be an object of another class.
There are two specific types of association that you need to know about: aggregation and
composition.
https://bit.ly/pmt-edu
Aggregation is the weaker of the two kinds of association. When an object is associated
with another by aggregation, it will still exist if its containing object is destroyed.
For example: a car’s passengers are associated with the car by aggregation. If the
car is scrapped, the passengers still exist independently.
For example: a car’s wheels are associated with the car by composition. The
wheels form an integral part of the car and scrapping the car destroys the wheels.
The use of classes allows code to be reused throughout the same program and even in
other programs. This improves the space efficiency of code.
We recommend that you use online documentation to find out what the syntax for this
looks like in the programming language that you will use when you sit the exam.
Virtual methods are methods in a base class that can be overridden by derived classes.
This allows subclasses to provide their specific implementation while maintaining a
https://bit.ly/pmt-edu
consistent method interface. Virtual methods facilitate polymorphism, enabling method
behaviour to vary based on the object that invokes the method.
Virtual methods belong to the class itself rather than any instance of the class. They can
be called on the class directly and do not require an instance to be invoked.
There are three design principles used in object-oriented programming that you need to be
aware of. These are: encapsulate what varies, favour composition over inheritance and
program to interfaces, not implementation.
Class diagrams
Class diagrams can be used to visually represent the relationships that exist between
classes. Classes are represented by boxes with different connectors representing different
kinds of relationship.
Inheritance diagrams
A special type of class diagram, the inheritance diagram, shows the different inheritance
relationships that exist between classes in an object-oriented program.
https://bit.ly/pmt-edu
Association
Association is shown in class diagrams with diamond headed arrows.
Aggregation Composition
Aggregation (the weaker of the two types Composition (the stronger relationship) is
of association) is shown with an unfilled shown with a filled diamond headed arrow.
diamond headed arrow.
Student
- Name: String
- Age: Integer
+ GetName
+ GetAge
+ SetDetails
A plus sign indicates that a property or method is public and a minus indicates that the
property or method is private.
https://bit.ly/pmt-edu