Professional Documents
Culture Documents
Computer Science Revision Notes Paper 2 Option D
Computer Science Revision Notes Paper 2 Option D
Concept of OOP
Object: An abstract entity with attributes (that describe the entity) and methods it can perform, e.g.
car object's attributes= wheels, windows, brakes etc. Methods= start up, change gear, turn on
headlights etc.
An object is an instance/ instantiation of a class. A class is like a template for an object- code for
the methods and attributes but doesn't take up any memory until an object is instantiated from the
template, with its own copy of the class variables and methods. You can create multiple
instantiated objects from the same class.
Class Name
Data / variables / attributes
Actions/ methods
Aggregation (“has a” relationship): One object belongs to another and no other object, but both
are independent and can exist without the other. e.g. Car has passengers, but passengers can still
exist and function normally without the car
Composition (“has a”): Like aggregation but the object can't function or exist without the other.
e.g. there can't be a certain department in a hospital if the hospital doesn't exist
Note: Sometimes in IB aggregations and composition are both referred to as aggregation. e.g. could say a
student having hands is an example of an aggregation
Inheritance (“is a”): One object/subclass is a specialised, altered form of another object. It
inherits all the attributes of the parent class, but also has additional ones e.g. Current account is a
type of bank account, bus is a vehicle so has an engine like all vehicles, student is a person so has
a name
Best to reduce dependencies to increase the ability to reuse code. If we depend on another class
to provide useful methods, have to have the parent class/object available whenever we use the
object.
Data types
Integer: Stores whole numbers (Java int)
Real: Stores decimal numbers. Floating point. (Java double or float)
String: Text data, characters (Java String)
Boolean: True/ false value (Java boolean)
Long: Stores a 64-bit integer, can be positive or negative
Data items are passed to and from methods as parameters, which are restricted by pass-by-value
of class relationship. Methods can return at most one data item, e.g. a method that can only return
int or is a void (doesn’t return anything).
Pass-by-value: Means that a value is copied from the main program into the parameter to be used
by the method. If the value (from the parameter) is changed in the method, it doesn’t affect the
original values in the main program. e.g. in Java, the method would have to return the new value,
and then you’d set the original value to the returned value- only then will it change.??
Pass-by-reference: Does not have the value copied into the parameter, only the memory location
(reference/ pointer) is copied. Method can change the variable values in the main program.
[don’t take my word on this one]
Features of OOP
Encapsulation: Data and methods/actions are limited to the object in which they are defined-
places all attributes and methods relating to a certain object/entity together. Private variables can
be used and only accessed outside the class by specific get and set methods.
Advantages
Class can restrict the way data and methods are altered or called- increases security, since
it ensures variables aren't accidentally changed by another part of the program. Especially
important for team projects.
Can hide the way data is stored- increase security
Since relevant attributes/methods are all together, it provides a clearer understanding of
each section of the problem = more efficient programming (faster, less errors etc)
Inheritance: A parent object holds common data and methods for a group of related child objects-
it is extended to create a new object with more properties and methods. The new class can over-
ride old properties and methods in the original class.
Advantages
Extensibility: Sub-classes inherit methods, can extend them. No need to re-write methods
Reusability: Changes and upgrades to data and methods in super-class are inherited by
the sub-class, don't need to change each sub-class manually. Less maintenance
Polymorphism: Can have several methods with the same name but different processes- different
versions of the method essentially. If in the same class, must have different parameter lists so the
compiler can tell which one to use according to what values are passed in the parameters. If not in
the same class, compiler selects method in the class currently in use.
e.g. makeSound() is different for different sub-classes of Animal, add() to savings account is
different to add() in current account- current account could take an additional value for
interest rate in its parameters.
Advantages:
External programs can use the subclass methods without knowing the implementation
details or which classes implement them
Don't have to write up different names of methods
Libraries of objects: Collections of classes already written in code for standard functions, ready to
be used by the programmer.
Advantage: Reuse of code. Essential functions are common in many applications so don't need to
be written over again. e.g. GUI components (control Objects) stored in javax.swing library
Disadvantages of OOP
May unnecessarily increase complexity, time and maintenance costs if the project is small with few
developers and modules and/or won't be re-used or altered once written.
Programming teams
“Divide and conquer” - large projects done in teams of developers to get the project done in time
before the deadline
Advantages:
More developers = larger, more ambitious projects can be undertaken in a smaller space of
time rather than one person writing out all the code
Different members = various ideas and larger collection of knowledge rather than one
person needing to know everything there is to know about an email server, for example
Members can specialise on certain parts of the project, can focus fully on it. e.g. testing,
documentation
Disadvantages (things to consider when working in a team):
Needs effective communication and common language between members, especially with
international projects
Decisions may take longer since members aren't aware of the whole project workings
Modularity
Dividing computer programs into sub-programs (or objects/classes) that can be coded and tested
on their own before being combined to build the final program. Need few dependencies between
sub-programs
Advantages:
Easier to debug each small piece to test its specific functionality before testing the whole
system once it's put together
Can distribute work among programmers, who can work concurrently on different modules=
speedier completion
Reusability: Sub-program can be reused in another program that needs the same
functionality. Old, reliable, proven modules (libraries) can be reused in further projects
Structure easier to understand, making changes is easier as you improve individual
modules then plug in the improvements
Program development
Class: A template/blueprint or set of instructions for the methods and variables in a particular
object.
Identifier: Pointer that explicitly identifies an object, class, method or variable. Allows you to refer
to the item from other places in the program. E.g. a reference in Java
Primitive data type: Used to store simple values, building blocks of more complex abstract data
types. Built into the programming language, e.g. int, char, Boolean, long, double in Java.
Reference class String is not actually primitive, but it is a basic data type- simple value.
Variables
Provide named storage for values
Instance variable: Variable in a class from which every instantiated object gets its own
copy. e.g. if a class called “People” has an instance variable “Age”, every object created
from this class would have its own variable “Age”
Parameter variable: Variable passed into a function called to perform operations. In Java,
it’s the argument passed in the brackets with the method caller
Local variable: Variable declared inside a function. Only known and accessible by the
function it's declared in
Methods
Collection of statements grouped together to perform an operation. A function essentially.
Accessor: “Get” method. Returns the value of a private field.
Mutator: “Set” method. Sets the value of a private field.
Constructor: Used to set the initial values for the class’s variables. The constructor method
is called first when an object is instantiated
Signature: Part of the method declaration, combination of the method name and parameter
list. Needed because of overloaded methods (same name) that need to be differentiated by
their parameters
Return value: Procedures don't return any value (void), functions do
Modifiers
Private: Specifies that an object/variable/method can only be accessed in its own class
Protected: Specifies that an object/variable/method can only be accessed within its own
class but also by a subclass
Public: Object/variable/method can be accessed by all classes
Extends: Using this means subclasses can inherit all properties of the superclass, except
for private properties (explained more in inheritance)
Static: Value of the variable is the same for all objects/ instances of a class, belongs to the
class, not the objects themselves. Only created/declared once. e.g. if you change a static
variable's value in one instance, it will change for all instances. Methods are also class
methods independent from the object
Loops
while (X<10 && found=false){ //can be any conditions. “||” means “OR”
//some code
X++;
}
Arrays
String[] names = new String[10]; //10 values, index name[0] to name[9]
name[0] = “Bob”; ….
Compile-time initialization...
String[] names = {“Bob”, “Jim”, “Phil”};